SwiftUI – Open a specific View when user opens a Push Notification

You need some sort of shared state that you can modify that SwiftUI knows to react to. An ObservableObject is perfect for this: class AppState: ObservableObject { static let shared = AppState() @Published var pageToNavigationTo : String? } Then, to listen to it and respond to it, you can do a couple different methods in … Read more

SwiftUI Text Markdown dynamic string not working

Short Answer Wrap the string in AttributedString(markdown: my_string_here): let string: String = “[Apple Link](http://www.apple.com)” Text(try! AttributedString(markdown: string)) Extension extension String { func toMarkdown() -> AttributedString { do { return try AttributedString(markdown: self) } catch { print(“Error parsing Markdown for string \(self): \(error)”) return AttributedString(self) } } } Long Answer SwiftUI Text has multiple initializers. For … Read more

How to detect Light\Dark mode change in iOS 13?

SwiftUI With a simple environment variable on the \.colorScheme key: struct ContentView: View { @Environment(\.colorScheme) var colorScheme var body: some View { Text(colorScheme == .dark ? “Its Dark” : “Its. not dark! (Light)”) } } UIKit As it described in WWDC 2019 – Session 214 around 23:30. As I expected, this function is getting called … Read more

How to make a button (or any other element) show SwiftUI’s DatePicker popup on tap?

I’m having this problem too. I couldn’t sleep for a few days thinking about the solution. I have googled hundred times and finally, I found a way to achieve this. It’s 1:50 AM in my timezone, I can sleep happily now. Credit goes to chase’s answer here Demo here: https://media.giphy.com/media/2ILs7PZbdriaTsxU0s/giphy.gif The code that does the … Read more

Custom Button in SwiftUI List

In standard variant List intercepts and handles content area of tap detection, in your custom style it is defined, by default, by opaque area, which is only text in your case, so corrected style is Update for: Xcode 13.3 / iOS 15.4 It looks like Apple broken something, because listRowBackground now works only inside List … Read more

use @main in Xcode 12

Following @the.blaggy answer, here is how I managed to run my project on iOS 13: Create a SceneDelegate if you do not have one SceneDelegate.swift class SceneDelegate: UIResponder, UIWindowSceneDelegate { var window: UIWindow? func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions) { let contentView = ContentView() // Use a UIHostingController as window root … Read more

Is it possible to make a modal non-dismissible in SwiftUI?

iOS 15 and later: Use .interactiveDismissDisabled(true) on the sheet, that’s all. Prev iOS 15: You can try to do this by using a highPriorityGesture. Of course the blue Rectangle is only for demonstration but you would have to use a view which is covering the whole screen. struct ModalViewNoClose : View { @Environment(\.presentationMode) var presentationMode … Read more

Localization with String interpolation in SwiftUI

Apparently, a LocalizedStringKey will automatically generate the localization key depending on the type of the values interpolated. For example, if you have the following Texts Text(“title key”) Text(“name key \(“Club”)”) Text(“count key \(8)”) Text(“price key \(6.25)”) Your Localizable.strings file should look like “title key” = “Sandwiches”; “name key %@” = “Name: %@”; “count key %lld” … Read more