iOS13中AppDelegate的職責發現了改變:
iOS13之前,Appdelegate的職責全權處理App生命周期和UI生命周期;
iOS13之后,Appdelegate的職責是:
1、處理 App 生命周期
2、新的 Scene Session 生命周期
Appdelegate不在負責UI生命周期,所有UI生命周期交給SceneDelegate處理:
因此初始化window方法需要改變:
現在不再Appdelegate的- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
進行初始化,轉交給SceneDelegate的willConnectToSession:
方法進行根控制器設置
- (void)scene:(UIScene *)scene willConnectToSession:(UISceneSession *)session options:(UISceneConnectionOptions *)connectionOptions {
UIWindowScene *windowScene = (UIWindowScene *)scene;
self.window = [[UIWindow alloc] initWithWindowScene:windowScene];
self.window.frame = windowScene.coordinateSpace.bounds;
self.window.rootViewController = [UITabBarController new];
[self.window makeKeyAndVisible];
}
想繼續全部使用AppDelegate也是可以的,刪除SceneDelegate
1,直接刪除SceneDelegate.h、SceneDelegate.m文件
2,info.plist文件中刪除Application Scene Manifest
8FD21B88-3FEC-47BA-9B84-AD396C7390D8.png
3,刪除SceneDelegate在AppDelegate中的代理
#pragma mark - UISceneSession lifecycle
- (UISceneConfiguration *)application:(UIApplication *)application configurationForConnectingSceneSession:(UISceneSession *)connectingSceneSession options:(UISceneConnectionOptions *)options {
// Called when a new scene session is being created.
// Use this method to select a configuration to create the new scene with.
return [[UISceneConfiguration alloc] initWithName:@"Default Configuration" sessionRole:connectingSceneSession.role];
}
- (void)application:(UIApplication *)application didDiscardSceneSessions:(NSSet<UISceneSession *> *)sceneSessions {
// 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.
}