級(jí)別: ★☆☆☆☆
標(biāo)簽:「iOS13」「Xcode11」「SceneDelegate」
作者: dac_1033
審校: QiShare團(tuán)隊(duì)
Xcode 11 建新工程默認(rèn)會(huì)創(chuàng)建通過 UIScene 管理多個(gè) UIWindow 的應(yīng)用,工程中除了 AppDelegate 外還會(huì)有一個(gè) SceneDelegate觉增,這是為了實(shí)現(xiàn)iPadOS支持多窗口的結(jié)果兵拢。AppDelegate.h不再有window屬性,window屬性被定義在了SceneDelegate.h中逾礁,AppDelegate中有新增的關(guān)于scene的代理方法说铃,SceneDelegate中也有相應(yīng)的代理方法。因此嘹履,當(dāng)我們用Xcode11針對(duì)不同版本的iOS開發(fā)應(yīng)用時(shí)腻扇,就要做一些適配。
創(chuàng)建好一個(gè)工程后砾嫉,可以看到相關(guān)Xcode開發(fā)界面變成了下面這樣:
AppDelegate.h中多了兩個(gè)默認(rèn)的代理方法:
#pragma mark - UISceneSession lifecycle
/*
1.如果沒有在APP的Info.plist文件中包含scene的配置數(shù)據(jù)幼苛,或者要?jiǎng)討B(tài)更改場(chǎng)景配置數(shù)據(jù),需要實(shí)現(xiàn)此方法焕刮。 UIKit會(huì)在創(chuàng)建新scene前調(diào)用此方法舶沿。
2.方法會(huì)返回一個(gè)UISceneConfiguration對(duì)象,其包含其中包含場(chǎng)景詳細(xì)信息配并,包括要?jiǎng)?chuàng)建的場(chǎng)景類型括荡,用于管理場(chǎng)景的委托對(duì)象以及包含要顯示的初始視圖控制器的情節(jié)提要。 如果未實(shí)現(xiàn)此方法荐绝,則必須在應(yīng)用程序的Info.plist文件中提供場(chǎng)景配置數(shù)據(jù)一汽。
總結(jié)下:默認(rèn)在info.plist中進(jìn)行了配置避消, 不用實(shí)現(xiàn)該方法也沒有關(guān)系低滩。如果沒有配置就需要實(shí)現(xiàn)這個(gè)方法并返回一個(gè)UISceneConfiguration對(duì)象召夹。
配置參數(shù)中Application Session Role 是個(gè)數(shù)組,每一項(xiàng)有三個(gè)參數(shù):
Configuration Name: 當(dāng)前配置的名字;
Delegate Class Name: 與哪個(gè)Scene代理對(duì)象關(guān)聯(lián);
StoryBoard name: 這個(gè)Scene使用的哪個(gè)storyboard恕沫。
注意:代理方法中調(diào)用的是配置名為Default Configuration的Scene监憎,則系統(tǒng)就會(huì)自動(dòng)去調(diào)用SceneDelegate這個(gè)類。這樣SceneDelegate和AppDelegate產(chǎn)生了關(guān)聯(lián)婶溯。
*/
- (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];
}
// 在分屏中關(guān)閉其中一個(gè)或多個(gè)scene時(shí)候回調(diào)用
- (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.
}
SceneDelegate.m中的默認(rèn)代理方法如下:
- (void)scene:(UIScene *)scene willConnectToSession:(UISceneSession *)session options:(UISceneConnectionOptions *)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).
}
- (void)sceneDidDisconnect:(UIScene *)scene {
// Called as the scene is being released by the system.
// This occurs shortly after the scene enters the background, or when its session is discarded.
// Release any resources associated with this scene that can be re-created the next time the scene connects.
// The scene may re-connect later, as its session was not neccessarily discarded (see `application:didDiscardSceneSessions` instead).
}
- (void)sceneDidBecomeActive:(UIScene *)scene {
// Called when the scene has moved from an inactive state to an active state.
// Use this method to restart any tasks that were paused (or not yet started) when the scene was inactive.
}
- (void)sceneWillResignActive:(UIScene *)scene {
// Called when the scene will move from an active state to an inactive state.
// This may occur due to temporary interruptions (ex. an incoming phone call).
}
- (void)sceneWillEnterForeground:(UIScene *)scene {
// Called as the scene transitions from the background to the foreground.
// Use this method to undo the changes made on entering the background.
}
- (void)sceneDidEnterBackground:(UIScene *)scene {
// Called as the scene transitions from the foreground to the background.
// Use this method to save data, release shared resources, and store enough scene-specific state information
// to restore the scene back to its current state.
}
1. 不需要多窗口(multiple windows)
如果需要支持iOS 13 及之前多個(gè)版本的iOS鲸阔,且又不需要多個(gè)窗口的功能,可以刪除項(xiàng)目info.plist文件中的Application Scene Manifest的配置數(shù)據(jù)迄委,AppDelegate中關(guān)于Scene的代理方法褐筛、SceneDelegate的類是否刪除都可以。
如果使用純代碼來實(shí)現(xiàn)顯示界面叙身,需要在AppDelegate.h中手動(dòng)添加window屬性渔扎,添加以下代碼即可:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
self.window = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];
[self.window setBackgroundColor:[UIColor whiteColor]];
ViewController *con = [[ViewController alloc] init];
UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:con];
[self.window setRootViewController:nav];
[self.window makeKeyAndVisible];
return YES;
}
2. 支持多窗口
iOS 13項(xiàng)目info.plist中的配置項(xiàng)Application Scene Manifest是針對(duì)iPad multiple windows功能推出的。在保留Application Scene Manifest配置項(xiàng)不予刪除時(shí)(其中信轿,項(xiàng)目是否支持多窗口功能是個(gè)可勾選項(xiàng))晃痴,AppDelegate的生命周期方法不再起作用,需要在SceneDelegate中使用UIScene提供的生命周期方法财忽,并且需要針對(duì) iOS 13 需要在Scene中配置和 iOS 13 以下在AppDelegate中做兩套配置倘核。
下面是純代碼實(shí)現(xiàn)界面顯示的代碼:
//// AppDelegate.m中
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
// Override point for customization after application launch.
if (@available(iOS 13.0, *)) {
} else {
self.window = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];
[self.window setBackgroundColor:[UIColor whiteColor]];
ViewController *con = [[ViewController alloc] init];
UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:con];
[self.window setRootViewController:nav];
[self.window makeKeyAndVisible];
}
return YES;
}
//// SceneDelegate.m中
- (void)scene:(UIScene *)scene willConnectToSession:(UISceneSession *)session options:(UISceneConnectionOptions *)connectionOptions {
//在這里手動(dòng)創(chuàng)建新的window
if (@available(iOS 13.0, *)) {
UIWindowScene *windowScene = (UIWindowScene *)scene;
self.window = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];
[self.window setWindowScene:windowScene];
[self.window setBackgroundColor:[UIColor whiteColor]];
ViewController *con = [[ViewController alloc] init];
UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:con];
[self.window setRootViewController:nav];
[self.window makeKeyAndVisible];
}
}
注意:如果不使用storyboard,需要將配置中的storyboard項(xiàng)刪除:
推薦文章:
iOS App啟動(dòng)優(yōu)化(二)—— 使用“Time Profiler”工具監(jiān)控App的啟動(dòng)耗時(shí)
iOS App啟動(dòng)優(yōu)化(一)—— 了解App的啟動(dòng)流程
iOS WKWebView的基本使用
Swift 5.1 (4) - 集合類型
iOS 解析一個(gè)自定義協(xié)議
iOS13 DarkMode適配(二)
iOS13 DarkMode適配(一)
2019蘋果秋季新品發(fā)布會(huì)速覽