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