iOS SceneDelegate应用

Xcode 11 建新工程默认会创建通过UIScene管理多个UIWindow的应用,工程中除了AppDelegate外还会有一个SceneDelegate,这是为了实现iPadOS支持多窗口的结果。AppDelegate.h不再有window属性,window属性被定义在了SceneDelegate.h中,AppDelegate中有新增的关于scene的代理方法,SceneDelegate中也有相应的代理方法。因此,当我们用Xcode11针对不同版本的iOS开发应用时,就要做一些适配。

创建好一个工程后,可以看到相关Xcode开发界面变成了下面这样:
在这里插入图片描述
工程配置general

在这里插入图片描述
info.plist文件

AppDelegate.h中多了两个默认的代理方法:

#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属性,添加以下代码即可:

    - (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项删除:
在这里插入图片描述
general

在这里插入图片描述
info.plist

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值