DEV Community

Kurt Frey
Kurt Frey

Posted on

3

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.

Heroku

Build apps, not infrastructure.

Dealing with servers, hardware, and infrastructure can take up your valuable time. Discover the benefits of Heroku, the PaaS of choice for developers since 2007.

Visit Site

Top comments (0)

A Workflow Copilot. Tailored to You.

Pieces.app image

Our desktop app, with its intelligent copilot, streamlines coding by generating snippets, extracting code from screenshots, and accelerating problem-solving.

Read the docs

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay