DEV Community

Kurt Frey
Kurt Frey

Posted on

Transition from App Delegate to SwiftUI life cycle

Apple introduced the new SwiftUI life cycle some years ago. I still had some apps that used UIKit and the App Delegate life cycle.

Transitioning from one to the other seems trivial at first, makes you scratch your head shortly later only to realise that it actually is quite trivial.

Before you start you should redesign your views in SwiftUI and create a YourSwiftUIView which serves as the first view your users are presented with.

Firstly, remove @UIUIApplicationMain from your AppDelegate class.

Secondly, create a SwiftUI App struct:

import Foundation
import SwiftUI

@main
struct yourAppName: App {
        // Relevant if you use CoreData
    let persistenceController = PersistenceController.shared

    var body: some Scene {
        WindowGroup {
            YourSwiftUIView()
                    // Relevant if you use CoreData
                .environment(\.managedObjectContext, persistenceController.container.viewContext)
        }
    }
}
Enter fullscreen mode Exit fullscreen mode

Thirdly, remove UIApplicationSupportsMultipleScenes key from your InfoPlist. This key will have quite an entourage. Remove it all.

<dict>
    <key>UIApplicationSupportsMultipleScenes</key>
    <false/>
    <key>UISceneConfigurations</key>
    <dict>
        <key>UIWindowSceneSessionRoleApplication</key>
        <array>
            <dict>
                <key>UISceneConfigurationName</key>
                <string>Default Configuration</string>
                <key>UISceneDelegateClassName</key>
                <string>$(PRODUCT_MODULE_NAME).SceneDelegate</string>
                <key>UISceneStoryboardFile</key>
                <string>Main</string>
            </dict>
        </array>
    </dict>
</dict>
Enter fullscreen mode Exit fullscreen mode

Finally you can remove all the Storyboard Files and accompanying ViewControllers.

Top comments (0)