0

I just started a new project of iOS. I created the project using xcode 11 and iOS 13. When I created project i found out that in order to set our own rootController we have to use sceneDelegate instead of AppDelegate. I want to ask if there is any possibility to use old method of setting rootControllers in AppDelegate instead of using sceneDelegate.

 import UIKit

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {


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

        let loginController: HomeViewController = HomeViewController(nibName: "HomeViewController", bundle: nil)

       //        let navController: UINavigationController = UINavigationController(rootViewController: loginController)
               self.window?.rootViewController = loginController
               self.window?.makeKeyAndVisible()
        // Override point for customization after application launch.
        return true
    }
}
Hasnain ahmad
  • 301
  • 1
  • 10
  • 33

3 Answers3

3

Follow these steps to use AppDelegate and opt-out for SceneDelegate

  1. Go to Info.plist and remove Application Scene Manifest entry from Info.plist.
  2. Remove SceneDelegate.swift
  3. Add var window: UIWindow? in your AppDelegate.swift file
  4. Delete the UISceneSession Lifecycle code from AppDelegate.swift

    import UIKit
    
    @UIApplicationMain
    class AppDelegate: UIResponder, UIApplicationDelegate {
    
    
    var window : UIWindow?
    
    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
        // Override point for customization after application launch.
         let mainStoryBoard = UIStoryboard(name: "Main", bundle: Bundle.main)
         let loginController = mainStoryBoard.instantiateViewController(withIdentifier: "HomeViewController")
         self.window?.rootViewController = loginController
         self.window?.makeKeyAndVisible()
         return true
    }
    }
    

Make sure to give "HomeViewController" storyboardID to your view controller.

This is how your AppDelegate.swift file should look now. You are good to go!

Keshu R.
  • 5,045
  • 1
  • 18
  • 38
1

If you don't want to use sceneDelegate then you can remove all sceneDelegate and also remove 'Application Scene Manifest' from info.plist

set UIWindow variable

var window: UIWindow?

Make sure you have removed Application Scene Manifest this from info.plist and change the background colour of your view controller.

Your device's Dark Mode is enable if you want to remove dark mode from app add this key into your info.plist

User Interface Style = Light
Harshal Valanda
  • 5,331
  • 26
  • 63
1

Why don't you use the SceneDelegate

func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions) {
            guard let windowScene = (scene as? UIWindowScene) else { return }
            self.window = UIWindow(frame: windowScene.coordinateSpace.bounds)
           //Make sure to do this else you won't get 
           //the windowScene object using UIApplication.shared.connectedScenes
            self.window?.windowScene = windowScene 
            let storyBoard: UIStoryboard = UIStoryboard(name: storyBoardName, bundle: nil)
            window?.rootViewController = storyBoard.instantiateInitialViewController()
            window?.makeKeyAndVisible()
        }