Home > Net >  App migrated to UIKit lifecycle doesn't call SceneDelegate
App migrated to UIKit lifecycle doesn't call SceneDelegate

Time:06-29

We migrated our app from the SwiftUI to the UIKit lifecycle and app containment, creating a standard AppDelegate, SceneDelegate, and updating the required info.plist properties. I didn't follow this tutorial, but if you're unfamiliar, this is exactly what we did as well. https://mokacoding.com/blog/how-to-migrate-from-swiftui-to-uikit-life-cycle/.

Our issue is that physical iPhone devices that had the app with the SwiftUI lifecycle installed, black screens and are unresponsive on launch. Debugging proves that this is because the SceneDelegate setup functions are never called at all. iPhone simulators, building to Mac, etc, works fine.

Deleting the app and reinstalling resolves this problem, but we can't ask that from our installed base.

Is there any way to force installed apps to clear their cache or whatever it is that controls launch configurations?

Here is the relevant code.

AppDelegate, this is being called

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
    return true
}

SceneDelegate, this is NOT being called

func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions) {
    guard let scene = scene as? UIWindowScene else { return }

    window = UIWindow(windowScene: scene)
    let viewController: UIViewController
    if isLoggedIn {
        viewController = MainViewController()
    } else {
        viewController = UIHostingController(
            rootView: LandingView().injectingEnvironment()
        )
    }
    window!.rootViewController = viewController
    window!.makeKeyAndVisible()
}

info.plist

<key>UIApplicationSceneManifest</key>
<dict>
    <key>UIApplicationSupportsMultipleScenes</key>
    <true/>
    <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>
            </dict>
        </array>
    </dict>
</dict>

CodePudding user response:

Try to remove scene configurations manifest from Info.plist and create it programmatically in AppDelegate, like

class AppDelegate: NSObject, UIApplicationDelegate {


    func application(_ application: UIApplication, configurationForConnecting connectingSceneSession: UISceneSession, options: UIScene.ConnectionOptions) -> UISceneConfiguration {
        let configuration = UISceneConfiguration(name: nil, sessionRole: connectingSceneSession.role)
        if connectingSceneSession.role == .windowApplication {
            configuration.delegateClass = SceneDelegate.self
        }
        return configuration
    }
}
  • Related