Xcode11 新建工程中的SceneDelegate

級別: ★☆☆☆☆
標(biāo)簽:「iOS13」「Xcode11」「SceneDelegate」
作者: dac_1033
審校: QiShare團隊


Xcode 11 建新工程默認(rèn)會創(chuàng)建通過 UIScene 管理多個 UIWindow 的應(yīng)用,工程中除了 AppDelegate 外還會有一個 SceneDelegate,這是為了實現(xiàn)iPadOS支持多窗口的結(jié)果。AppDelegate.h不再有window屬性,window屬性被定義在了SceneDelegate.h中,AppDelegate中有新增的關(guān)于scene的代理方法,SceneDelegate中也有相應(yīng)的代理方法。因此,當(dāng)我們用Xcode11針對不同版本的iOS開發(fā)應(yīng)用時,就要做一些適配。

創(chuàng)建好一個工程后,可以看到相關(guān)Xcode開發(fā)界面變成了下面這樣:

工程配置general
info.plist文件

AppDelegate.h中多了兩個默認(rèn)的代理方法:

#pragma mark - UISceneSession lifecycle

/*
1.如果沒有在APP的Info.plist文件中包含scene的配置數(shù)據(jù),或者要動態(tài)更改場景配置數(shù)據(jù),需要實現(xiàn)此方法。 UIKit會在創(chuàng)建新scene前調(diào)用此方法。
2.方法會返回一個UISceneConfiguration對象,其包含其中包含場景詳細(xì)信息,包括要創(chuàng)建的場景類型,用于管理場景的委托對象以及包含要顯示的初始視圖控制器的情節(jié)提要。 如果未實現(xiàn)此方法,則必須在應(yīng)用程序的Info.plist文件中提供場景配置數(shù)據(jù)。

總結(jié)下:默認(rèn)在info.plist中進(jìn)行了配置, 不用實現(xiàn)該方法也沒有關(guān)系。如果沒有配置就需要實現(xiàn)這個方法并返回一個UISceneConfiguration對象。
配置參數(shù)中Application Session Role 是個數(shù)組,每一項有三個參數(shù):
Configuration Name:   當(dāng)前配置的名字;
Delegate Class Name:  與哪個Scene代理對象關(guān)聯(lián);
StoryBoard name: 這個Scene使用的哪個storyboard。
注意:代理方法中調(diào)用的是配置名為Default Configuration的Scene,則系統(tǒng)就會自動去調(diào)用SceneDelegate這個類。這樣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)閉其中一個或多個scene時候回調(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 及之前多個版本的iOS,且又不需要多個窗口的功能,可以刪除項目info.plist文件中的Application Scene Manifest的配置數(shù)據(jù),AppDelegate中關(guān)于Scene的代理方法、SceneDelegate的類是否刪除都可以。
如果使用純代碼來實現(xiàn)顯示界面,需要在AppDelegate.h中手動添加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項目info.plist中的配置項Application Scene Manifest是針對iPad multiple windows功能推出的。在保留Application Scene Manifest配置項不予刪除時(其中,項目是否支持多窗口功能是個可勾選項),AppDelegate的生命周期方法不再起作用,需要在SceneDelegate中使用UIScene提供的生命周期方法,并且需要針對 iOS 13 需要在Scene中配置和 iOS 13 以下在AppDelegate中做兩套配置。
下面是純代碼實現(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 {
    
    //在這里手動創(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項刪除:

general
info.plist

推薦文章:
iOS App啟動優(yōu)化(二)—— 使用“Time Profiler”工具監(jiān)控App的啟動耗時
iOS App啟動優(yōu)化(一)—— 了解App的啟動流程
iOS WKWebView的基本使用
Swift 5.1 (4) - 集合類型
iOS 解析一個自定義協(xié)議
iOS13 DarkMode適配(二)
iOS13 DarkMode適配(一)
2019蘋果秋季新品發(fā)布會速覽

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

相關(guān)閱讀更多精彩內(nèi)容

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