iOS使用SceneDelegate

xcode11創(chuàng)建項(xiàng)目新增SceneDelegate文件,AppDelegate文件結(jié)構(gòu)也發(fā)生變化,在AppDelegate.h文件中沒(méi)有了window屬性,而是在SceneDelegate.h中,可見(jiàn)AppDelegate不管理window而是交給SceneDelegate。由于這些是ios13新增,所以SceneDelegate在ios13以下的系統(tǒng)是不支持。所以xcode11創(chuàng)建的項(xiàng)目如要做一下處理:

如果App不需要支持多個(gè)scene,同時(shí)兼容ios13以下,可以刪除info.plist文件中的Application Scene Manifest的配置數(shù)據(jù)。在AppDelegate.h中添加window屬性,同時(shí)刪除UISceneSession的生命周期方法。和以前的使用方式一樣??蓞⒖?a href="http://m.itdecent.cn/p/49c6770a94e0" target="_blank">iOS移除SceneDelegate
如果在ios13中支持多scene,原先AppDelegate的生命周期方法不再起作用。而是在SceneDelegate中使用UIScene提供的生命周期方法

開(kāi)啟支持多scene(僅iPad有效)

image.png
由于勾選分屏,在info.plist 中 Application Scene Manifest -> enable Multipe Windows 設(shè)置為YES.
image.png

默認(rèn)在info.plist中進(jìn)行了配置, 不用實(shí)現(xiàn)該方法也沒(méi)有關(guān)系。如果沒(méi)有配置就需要實(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。

AppDelegate.m中多了UISceneSession的生命周期的代理方法

//1.如果沒(méi)有在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)用此方法。
//參數(shù)options是一個(gè)UISceneConnectionOptions類,官方解釋:它包含了為什么要?jiǎng)?chuàng)建一個(gè)新的scene的信息。根據(jù)參數(shù)信息判斷是否要?jiǎng)?chuàng)建一個(gè)新的scene
- (UISceneConfiguration *)application:(UIApplication *)application configurationForConnectingSceneSession:(UISceneSession *)connectingSceneSession options:(UISceneConnectionOptions *)options {
    NSLog(@"1");
    // 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];
}
//方法會(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)該方法也沒(méi)有關(guān)系。如果沒(méi)有配置就需要實(shí)現(xiàn)這個(gè)方法并返回一個(gè)UISceneConfiguration對(duì)象。
//在分屏中關(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.
    NSLog(@"2");
}

在SceneDelegate中不使用storyboard創(chuàng)建,手動(dòng)創(chuàng)建新的window

- (void)scene:(UIScene *)scene willConnectToSession:(UISceneSession *)session options:(UISceneConnectionOptions *)connectionOptions {
   //在這里手動(dòng)創(chuàng)建新的window
   if (scene) {
       UIWindowScene *windowScene = (UIWindowScene *)scene;
       self.window = [[UIWindow alloc] initWithWindowScene:windowScene];
       self.window.frame = CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, [UIScreen mainScreen].bounds.size.height);
       self.window.rootViewController = [ViewController new];
       [self.window makeKeyAndVisible];
   }
}

SceneDelegate生命周期

- (void)scene:(UIScene *)scene willConnectToSession:(UISceneSession *)session options:(UISceneConnectionOptions *)connectionOptions {
    NSLog(@"場(chǎng)景加載完成");
}
- (void)sceneDidDisconnect:(UIScene *)scene {
    NSLog(@"場(chǎng)景已經(jīng)斷開(kāi)連接");
}
- (void)sceneDidBecomeActive:(UIScene *)scene {
    NSLog(@"已經(jīng)從后臺(tái)進(jìn)入前臺(tái)");
}
- (void)sceneWillResignActive:(UIScene *)scene {
    NSLog(@"即將從前臺(tái)進(jìn)入后臺(tái)");
}
- (void)sceneWillEnterForeground:(UIScene *)scene {
    NSLog(@"即將從后臺(tái)進(jìn)入前臺(tái)");
}
- (void)sceneDidEnterBackground:(UIScene *)scene {
    NSLog(@"已經(jīng)從前臺(tái)進(jìn)入后臺(tái)");
}

附上個(gè)小Demo

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

友情鏈接更多精彩內(nèi)容