Xcode自動新增了一個SceneDelegate文件,查找了一下官方文檔WWDC2019:Optimizing App Launch 發現,iOS13中appdelegate的職責發現了改變: iOS13之前,Appdelegate的職責全權處理App生命周期和UI生命周期; iOS13之后,Appdelegate的職責是: 1、處理 App 生命周期 2、新的 Scene Session 生命周期 那UI的生命周期呢?交給新增的Scene Delegate處理, Appdelegate不在負責UI生命周期,所有UI生命周期交給SceneDelegate處理
除了與以前版本一樣,要刪除Main storyboard file base name之外, 還要在項目Info.plist中, 刪除SceneDelegate的StoryboardName

初始化window方法需要改變: 現在不再在Appdelegate的- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions進行初始化, 而是在SceneDelegate中初始化了
func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions) { // Use this method to optionally configure and attach the UIWindow `window` to the provided UIWindowScene `scene`. // If using a storyboard, the `window` property will automatically be initialized and attached to the scene. // This delegate does not imply the connecting scene or session are new (see `application:configurationForConnectingSceneSession` instead). // MARK ------ iOS13之前,Appdelegate的職責全權處理App生命周期和UI生命周期,iOS13之后,Appdelegate的職責是: 1、處理 App 生命周期 2、新的 Scene Session 生命周期 那UI的生命周期呢?交給新增的Scene Delegate處理, Appdelegate不在負責UI生命周期,所有UI生命周期交給SceneDelegate處理 guard let windowScene = (scene as? UIWindowScene) else { return } self.window = UIWindow.init(windowScene: windowScene) self.window?.rootViewController = UINavigationController.init(rootViewController: ViewController.init()) self.window?.backgroundColor = UIColor.white self.window?.makeKeyAndVisible() }
