iOS13新增SceneDelegate文件适配

1、iOS13新增SceneDelegate文件,AppDelegate的文件结构发生的变化:

  • iOS13以前:AppDelegate处理App生命周期和UI生命周期。
  • iOS13以后:新增SceneDelegate文件,处理 App 生命周期和新的 Scene Session 生命周期,在AppDelegate.h文件中没有了window属性,而是在SceneDelegate中,可见AppDelegate不管理window而是交给SceneDelegate。

2、原因:AppDelegate和SceneDelegate是iPadOS带来的新的多窗口支持的结果,并且有效地将应用程序委托的工作分成两部分。

3、SceneDelegate文件适配

  • 由于这些是ios13新增,所以SceneDelegate在iOS13以下的系统是不支持。 所以xcode11创建的项目如要做一下处理:

场景一:如果App不需要支持多个scene,同时兼容ios13以下,可以删除info.plist文件中的Application Scene Manifest的配置数据。在AppDelegate.h中添加window属性,同时删除UISceneSession的生命周期方法。和以前的使用方式一样。

步骤:
1、打开info.plist文件,删除Application Scene Manifest选项。
在这里插入图片描述
2、在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;
}

3、删除 SceneDelegate 文件,注释掉 AppDelegate.m 文件中两个UISceneSession的生命周期方法:

#pragma mark - UISceneSession lifecycle
//注释掉这两个UISceneSession的生命周期的代理方法
- (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<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.
}

场景二:App不需要支持scene,如果不删除Application Scene Manifest这个配置,则需要针对ios13需要在Scene中配置和ios13以下在AppDelegate中做两套配置。

场景三: 支持多个scene,原先AppDelegate的生命周期方法不再起作用。需要在SceneDelegate中使用UIScene提供的生命周期方法。需要兼容iOS13以下:利用@available添加版本判断。
1.SceneDelegate中添加@available(iOS 13, *);
2.AppDelegate中同样声明window属性。

开启支持多scene步骤:
1、点击TARGETS 的 General选项,在Deployment Info 中勾选 Supports multiple windows支持分屏。
在这里插入图片描述
2、ios13中对info.plist文件进行了修改,多了一个参数用于配置分屏Application Scene Manifest,由于上面勾选了分屏,enable Multipe Windows 由之前的 NO 设置成了 YES。
在这里插入图片描述
3、兼容iOS13以下:AppDelegate中同样声明window属性。利用@available添加版本判断

//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;
}

4、在SceneDelegate 文件中添加@available(iOS 13, *)做版本判断。

//场景加载完成
- (void)scene:(UIScene *)scene willConnectToSession:(UISceneSession *)session options:(UISceneConnectionOptions *)connectionOptions {
    //ios13在SceneDelegate中不使用storyboard创建
    if (@available(iOS 13.0, *)) {
        if (scene) {
            //初始化 window 对象
            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];
        }
    } else {    
    }
}

5、AppDelegate两个关于Scene的方法也添加版本控制。

//返回一个UISceneConfiguration对象
- (UISceneConfiguration *)application:(UIApplication *)application configurationForConnectingSceneSession:(UISceneSession *)connectingSceneSession options:(UISceneConnectionOptions *)options {
    //在 iOS13 之前的版本需要做版本控制
    if (@available(iOS 13.0, *)) {
        return [[UISceneConfiguration alloc] initWithName:@"Default Configuration" sessionRole:connectingSceneSession.role];
    } else {
        return nil;
    }
}
- (void)application:(UIApplication *)application didDiscardSceneSessions:(NSSet<UISceneSession *> *)sceneSessions {
    if (@available(iOS 13.0, *)) {
        //在分屏中关闭其中一个或多个scene时候会调用。
        NSLog(@"%s",__func__);
    } else {
    }
}

4、SceneDelegate 的生命周期

//场景加载完成
- (void)scene:(UIScene *)scene willConnectToSession:(UISceneSession *)session options:(UISceneConnectionOptions *)connectionOptions {
    NSLog(@"场景加载完成");
}

//场景已经断开连接
- (void)sceneDidDisconnect:(UIScene *)scene {
    NSLog(@"场景已经断开连接");
}

//已经从后台进入前台
- (void)sceneDidBecomeActive:(UIScene *)scene {
    NSLog(@"已经从后台进入前台");
}

//即将从前台进入后台
- (void)sceneWillResignActive:(UIScene *)scene {
    NSLog(@"即将从前台进入后台");
}

//即将从后台进入前台
- (void)sceneWillEnterForeground:(UIScene *)scene {
    NSLog(@"即将从后台进入前台");
}

//已经从前台进入后台
- (void)sceneDidEnterBackground:(UIScene *)scene {
    NSLog(@"已经从前台进入后台");
}

  • 2
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值