Search code examples
iosswiftxcodeswift3swift4

After deleting Main Storyboard.Add StoryBoard Programatically with Backward compatibility


My target for app is iOS 10.0As soon as I change my Target, I get a bunch of errors in SceneDelegate file. For backward compatibility, I added “@available(iOS 13.0, *)” for Scene Delegate class.

I Started with an OnboardingController. Which is totally programatic. So I deleted MainStoryboard and removed it from ”Main Interface” and ”Application Scene Manifest” in info.

Now, I have to set the rootView in both AppDelegate and SceneDelegate. If I don’t set window in SceneDelegate I am getting only Black screen in iOS 13.0+ Devices and if I don’t set in AppDelegate I get just Black screen in <13.0 Devices Since I am calling both files my viewdidload() in Viewcontroller is called twice.

Following is what my AppDelegate

class AppDelegate: UIResponder, UIApplicationDelegate {

    var window: UIWindow?

    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
        window = UIWindow(frame: UIScreen.main.bounds)



        let viewController  = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "navC") as! UINavigationController

        window?.rootViewController = viewController
        window?.makeKeyAndVisible()
        return true
    }
}

Following is my SceneDelegate

@available(iOS 13.0, *)
class SceneDelegate: UIResponder, UIWindowSceneDelegate {


    var window: UIWindow?


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

            let viewController  = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "HomeCollectionVC") as! HomeCollectionVC
            window?.rootViewController = viewController
            window?.makeKeyAndVisible()

}
...
}

am I missing something?


Solution

  • This can be found easily by searching, but here is an example...

    Note: reference to SO user Matt

    SceneDelegate.swift

    import UIKit
    
    // entire class is iOS 13+
    @available(iOS 13.0, *)
    class SceneDelegate: UIResponder, UIWindowSceneDelegate {
    
        var window: UIWindow?
    
        func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions) {
    
            guard let windowScene = (scene as? UIWindowScene) else { return }
            window = UIWindow(frame: windowScene.coordinateSpace.bounds)
            window?.windowScene = windowScene
    
            let viewController  = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "HomeCollectionVC") as! HomeCollectionVC
            window?.rootViewController = viewController
            window?.makeKeyAndVisible()
    
        }
    
        func sceneDidDisconnect(_ scene: UIScene) {
        }
    
        func sceneDidBecomeActive(_ scene: UIScene) {
        }
    
        func sceneWillResignActive(_ scene: UIScene) {
        }
    
        func sceneWillEnterForeground(_ scene: UIScene) {
        }
    
        func sceneDidEnterBackground(_ scene: UIScene) {
        }
    
    }
    

    AppDelegate.swift

    import UIKit
    
    @UIApplicationMain
    class AppDelegate: UIResponder, UIApplicationDelegate {
    
        var window : UIWindow?
        func application(_ application: UIApplication,
                         didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey : Any]?)
            -> Bool {
                if #available(iOS 13, *) {
                    // do only pure app launch stuff, not interface stuff
                } else {
                    self.window = UIWindow()
    
                    let viewController  = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "navC") as! UINavigationController
    
                    window?.rootViewController = viewController
                    window?.makeKeyAndVisible()
    
                }
                return true
        }
    
        // MARK: UISceneSession Lifecycle
    
        // iOS 13+ only
        @available(iOS 13.0, *)
        func application(_ application: UIApplication, configurationForConnecting connectingSceneSession: UISceneSession, options: UIScene.ConnectionOptions) -> UISceneConfiguration {
            // Called when a new scene session is being created.
            // Use this method to select a configuration to create the new scene with.
            return UISceneConfiguration(name: "Default Configuration", sessionRole: connectingSceneSession.role)
        }
    
        // iOS 13+ only
        @available(iOS 13.0, *)
        func application(_ application: UIApplication, didDiscardSceneSessions sceneSessions: Set<UISceneSession>) {
            // Called when the user discards a scene session.
            // If any sessions were discarded while the application was not running, this will be called shortly after application:didFinishLaunchingWithOptions.
            // Use this method to release any resources that were specific to the discarded scenes, as they will not return.
        }
    
    
    }