Get Even More Visitors To Your Blog, Upgrade To A Business Listing >>

Add a Scene Delegate to your existing project with Storyboard in Swift

To add a scene delegate, first, create a new Swift file that you’ll call "SceneDelegate" containing a subclass of UIResponder, just like the AppDelegate, and that conforms to UIWindowSceneDelegate. 

As your app might supports other versions than iOS 13, make this class only available for iOS 13. This is what you should have :

If you are working a project that is storyboard based,

please set storyboard initial view controller

SceneDelegate.swift

import UIKit



@available(iOS 13.0, *)

class SceneDelegate: UIResponder, UIWindowSceneDelegate {


    var window: UIWindow?


    func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions) {

        

        let storyboard = UIStoryboard(name: "Main", bundle: nil)

        let initialViewController = storyboard.instantiateViewController(withIdentifier: "ViewController")


        let mainNavigationController = UINavigationController(rootViewController: initialViewController)

                self.window?.rootViewController = mainNavigationController

        

                 guard let _ = (scene as? UIWindowScene) else { return }

        

                if let windowScene = scene as? UIWindowScene {

                    self.window = UIWindow(windowScene: windowScene)

                    

                    self.window!.rootViewController = mainNavigationController

                    self.window!.makeKeyAndVisible()

                }

    }


    func sceneDidDisconnect(_ scene: UIScene) {

        // Called as the scene is being released by the system.

        // This occurs shortly after the scene enters the background, or when its session is discarded.

        // Release any resources associated with this scene that can be re-created the next time the scene connects.

        // The scene may re-connect later, as its session was not necessarily discarded (see `application:didDiscardSceneSessions` instead).

    }


    func sceneDidBecomeActive(_ scene: UIScene) {

        // Called when the scene has moved from an inactive state to an active state.

        // Use this method to restart any tasks that were paused (or not yet started) when the scene was inactive.

    }


    func sceneWillResignActive(_ scene: UIScene) {

        // Called when the scene will move from an active state to an inactive state.

        // This may occur due to temporary interruptions (ex. an incoming phone call).

    }


    func sceneWillEnterForeground(_ scene: UIScene) {

        // Called as the scene transitions from the background to the foreground.

        // Use this method to undo the changes made on entering the background.

    }


    func sceneDidEnterBackground(_ scene: UIScene) {

        // Called as the scene transitions from the foreground to the background.

        // Use this method to save data, release shared resources, and store enough scene-specific state information

        // to restore the scene back to its current state.

    }

    

}


 AppDelegate.swift


import UIKit



@UIApplicationMain

@available(iOS 13.0, *)


class AppDelegate: UIResponder, UIApplicationDelegate {


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

        // Override point for customization after application launch.

        return true

    }


    // MARK: UISceneSession Lifecycle


    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)

    }


    func application(_ application: UIApplication, didDiscardSceneSessions sceneSessions: SetUISceneSession>) {

        // 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.

    }



}


Just go in your info.plist file and add these lines :

UIApplicationSceneManifestUIApplicationSupportsMultipleScenesUISceneConfigurationsUIWindowSceneSessionRoleApplicationUISceneConfigurationNameDefault ConfigurationUISceneDelegateClassName$(PRODUCT_MODULE_NAME).SceneDelegateStoryboard NameMain


This post first appeared on IOS Developer Live, please read the originial post: here

Share the post

Add a Scene Delegate to your existing project with Storyboard in Swift

×

Subscribe to Ios Developer Live

Get updates delivered right to your inbox!

Thank you for your subscription

×