xcode11新增SceneDelegate文件

好久之前跟新到Xcode11,跟新完成后打开以前老项目并未有什么太大变化,也就没有在意,今天新建一个项目,创建完成后,发现多了个SceneDelegate的.m和.h文件,这是什么鬼?它有什么用呢?


xcode11新增SceneDelegate文件_第1张图片
新创建项目目录

AppDelegate和SceneDelegate是iPadOS带来的新的多窗口支持的结果,并且有效地将应用程序委托的工作分成两部分。
原来在iOS13中,AppDelegate的文件结构发生了变化:
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的方式适配:

  1. 打开info.plist文件,删除Application Scene Manifest选项。


    xcode11新增SceneDelegate文件_第2张图片

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 *)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 {
    if(@available(iOS 13, *)){
    }else
    {
           ViewController  *startVC=[[ViewController alloc]init];
            UINavigationController *nav = [[UINavigationController alloc]initWithRootViewController:startVC];
            self.window.rootViewController = nav;
            [self.window makeKeyAndVisible];
    }
    return YES;
}
  1. AppDelegate两个关于Scene的方法也添加版本控制;

你可能感兴趣的:(xcode11新增SceneDelegate文件)