Xcode11 新建工程中的SceneDelegate

級別: ★☆☆☆☆
標籤:「iOS13」「Xcode11」「SceneDelegate」
做者: dac_1033
審校: QiShare團隊php


Xcode 11 建新工程默認會建立經過 UIScene 管理多個 UIWindow 的應用,工程中除了 AppDelegate 外還會有一個 SceneDelegate,這是爲了實現iPadOS支持多窗口的結果。AppDelegate.h再也不有window屬性,window屬性被定義在了SceneDelegate.h中,AppDelegate中有新增的關於scene的代理方法,SceneDelegate中也有相應的代理方法。所以,當咱們用Xcode11針對不一樣版本的iOS開發應用時,就要作一些適配。git

建立好一個工程後,能夠看到相關Xcode開發界面變成了下面這樣:github

工程配置general

info.plist文件

AppDelegate.h中多了兩個默認的代理方法:windows

#pragma mark - UISceneSession lifecycle

/*
1.若是沒有在APP的Info.plist文件中包含scene的配置數據,或者要動態更改場景配置數據,須要實現此方法。 UIKit會在建立新scene前調用此方法。
2.方法會返回一個UISceneConfiguration對象,其包含其中包含場景詳細信息,包括要建立的場景類型,用於管理場景的委託對象以及包含要顯示的初始視圖控制器的情節提要。 若是未實現此方法,則必須在應用程序的Info.plist文件中提供場景配置數據。

總結下:默認在info.plist中進行了配置, 不用實現該方法也沒有關係。若是沒有配置就須要實現這個方法並返回一個UISceneConfiguration對象。
配置參數中Application Session Role 是個數組,每一項有三個參數:
Configuration Name:   當前配置的名字;
Delegate Class Name:  與哪一個Scene代理對象關聯;
StoryBoard name: 這個Scene使用的哪一個storyboard。
注意:代理方法中調用的是配置名爲Default Configuration的Scene,則系統就會自動去調用SceneDelegate這個類。這樣SceneDelegate和AppDelegate產生了關聯。
*/
- (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];
}

// 在分屏中關閉其中一個或多個scene時候回調用
- (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中的默認代理方法以下:數組

- (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的配置數據***,AppDelegate中關於Scene的代理方法、SceneDelegate的類是否刪除均可以。 若是使用純代碼來實現顯示界面,須要在AppDelegate.h中手動添加window屬性,添加如下代碼便可:bash

- (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中作兩套配置。 下面是純代碼實現界面顯示的代碼:微信

//// 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 {
    
    //在這裏手動建立新的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項刪除:session

general

info.plist


小編微信:可加並拉入《QiShare技術交流羣》。app

關注咱們的途徑有:
QiShare(簡書)
QiShare(掘金)
QiShare(知乎)
QiShare(GitHub)
QiShare(CocoaChina)
QiShare(StackOverflow)
QiShare(微信公衆號)ide

推薦文章:
iOS App啓動優化(二)—— 使用「Time Profiler」工具監控App的啓動耗時
iOS App啓動優化(一)—— 瞭解App的啓動流程
iOS WKWebView的基本使用 Swift 5.1 (4) - 集合類型 iOS 解析一個自定義協議
iOS13 DarkMode適配(二)
iOS13 DarkMode適配(一)
2019蘋果秋季新品發佈會速覽
奇舞週刊

相關文章
相關標籤/搜索