好久之前跟新到Xcode11赞警,跟新完成后打開以前老項目并未有什么太大變化验辞,也就沒有在意麦牺,今天新建一個項目钮蛛,創(chuàng)建完成后鞭缭,發(fā)現多了個SceneDelegate的.m和.h文件,這是什么鬼魏颓?它有什么用呢岭辣?
AppDelegate和SceneDelegate是iPadOS帶來的新的多窗口支持的結果,并且有效地將應用程序委托的工作分成兩部分甸饱。
原來在iOS13中沦童,AppDelegate的文件結構發(fā)生了變化:
iOS13以前:AppDelegate處理App生命周期和UI生命周期;
iOS13以后:處理 App 生命周期和新的 Scene Session 生命周期叹话,在AppDelegate.h文件中沒有了window屬性偷遗,而是在SceneDelegate中,可見AppDelegate不管理window而是交給SceneDelegate驼壶。
一.初始化window方法需要改變:
現在不再Appdelegate.m:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
轉交給SceneDelegate.m:
- (void)scene:(UIScene *)scene willConnectToSession:(UISceneSession *)session options:(UISceneConnectionOptions *)connectionOptions
- (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;
ViewController *startVC=[[ViewController alloc]init];
UINavigationController *nav = [[UINavigationController alloc]initWithRootViewController:startVC];
self.window.rootViewController = nav;
[self.window makeKeyAndVisible];
}
二.SceneDelegate適配
場景一:不需要支持多個scene氏豌,需要兼容iOS13以下,按以往的Appdelegate管理window的方式適配:
-
打開info.plist文件热凹,刪除Application Scene Manifest選項泵喘。
image
2.刪掉SceneDelegate文件,注釋以下代碼:
- (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.
}
3.在AppDelegate中新增window屬性般妙,代碼:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
ViewController *startVC=[[ViewController alloc]init];
UINavigationController *nav = [[UINavigationController alloc]initWithRootViewController:startVC];
self.window.rootViewController = nav;
[self.window makeKeyAndVisible];
return YES;
}
場景2: 支持多個scene纪铺,需要兼容iOS13以下:利用@available添加版本判斷。
1.SceneDelegate中添加@available(iOS 13, *)碟渺;
2.AppDelegate中同樣聲明window屬性鲜锚,代碼:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
// Override point for customization after application launch.
if(@available(iOS 13, *)){
}else
{
self.window = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];
[self.window setBackgroundColor:[UIColor whiteColor]];
self.window.rootViewController = [ViewController new];
[self.window makeKeyAndVisible];
}
return YES;
}
- AppDelegate兩個關于Scene的方法也添加版本控制;
4.SceneDelegate也添加版本控制苫拍;
- (void)scene:(UIScene *)scene willConnectToSession:(UISceneSession *)session options:(UISceneConnectionOptions *)connectionOptions {
//在這里手動創(chuàng)建新的window
if (@available(iOS 13.0, *)) {
if (scene) {
UIWindowScene *windowScene = (UIWindowScene *)scene;
self.window = [[UIWindow alloc] initWithWindowScene:windowScene];
[self.window setWindowScene:windowScene];
[self.window setBackgroundColor:[UIColor whiteColor]];
self.window.frame = CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, [UIScreen mainScreen].bounds.size.height);
self.window.rootViewController = [ViewController new];
[self.window makeKeyAndVisible];
}
}
}