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

Accessing AppState in AppDelegate with SwiftUI’s new iOS 14 life cycle

Use shared instance for AppState class AppState: ObservableObject { static let shared = AppState() // << here !! // Singe source of truth… @Published var user = User() } so you can use it everywhere struct MyApp: App { @UIApplicationDelegateAdaptor(AppDelegate.self) var appDelegate @StateObject var appState = AppState.shared // … other code } and func application(_ … Read more

SwiftUI 2.0 List with children – how to make the tappable area of the disclosure button cover the whole list item

Here is a demo of approach (of course in your project you’d move expand/collapse state into view model) struct DemoDisclosureGroups: View { let items: [Bookmark] = [.example1, .example2, .example3] @State private var flags: [Bool] = [false, false, false] var body: some View { List { ForEach(Array(items.enumerated()), id: \.1.id) { i, group in DisclosureGroup(isExpanded: $flags[i]) { … Read more

Detect app launch from WidgetKit widget extension

To detect an app launch from a WidgetKit widget extension where the parent application supports scenes you’ll need to implement scene(_:openURLContexts:), for launching from a background state, and scene(_:willConnectTo:options:), for launching from a cold state, in your parent application’s SceneDelegate. Also, add widgetURL(_:) to your widget’s view. Widget’s View: struct WidgetEntryView: View { var entry: … Read more

Clear background for form sections in SwiftUI?

Even in SwiftUI 2 Form is built on top of UIKit, specifically UITableView. You need to remove the default UITableViewCell‘s background (only once, preferably in the App init): UITableViewCell.appearance().backgroundColor = UIColor.clear and change the background using: Section { VStack { Button(“Button 1”) {} Spacer() Button(“Button 2”) {} } } .listRowBackground(Color.clear)

SwiftUI @State and .sheet() ios13 vs ios14

Your code have expectation of view update/creation order, but in general it is undefined (and probably changed in iOS 14). There is explicit way to pass information inside sheet – use different sheet creator, ie. .sheet(item:… Here is working reliable example. Tested with Xcode 12 / iOS 14 struct ContentView: View { @State private var … Read more

SwiftUI: What is @AppStorage property wrapper

AppStorage @AppStorage is a convenient way to save and read variables from UserDefaults and use them in the same way as @State properties. It can be seen as a @State property which is automatically saved to (and read from) UserDefaults. You can think of the following: @AppStorage(“emailAddress”) var emailAddress: String = “[email protected]” as an equivalent … Read more