iOS13生命周期的改动

大家都知道,应用生命周期这个东西,一直到目前的iOS 12这个版本都是在AppDelegate里头(也就是UIApplicationDelegate协议里头)

managing_your_app_s_life_cycle

iOS 13以下生命周期

在这里插入图片描述

对应到方法就是一些AppDelegate里头的方法以及一些可以注册监听的通知.

然而,到了iOS 13(目前还是beta版本,本来不想写这篇文章的,但是都这么多beta版本了这个改动还是没改回去,感觉正式版也一样.应该不会吃书了)这些都失效了(恶心的是低版本还是走之前的)

iOS 13及以上的改动

在这里插入图片描述

原本AppDelegate(UIApplicationDelegate)控制生命周期的行为移交给了SceneDelegate(UIWindowSceneDelegate)
如果直接使用Xcode 11(当然,目前是beta版)创建一个新的App.我们会发现.Appdelegate.m内代码少了一些生命周期的东西,多了一些其他的代码.并且多了一个SceneDelegate的类

注意.AppDelegate.h中没有了window,window移到了SceneDelegate.h

// appdelegate.m
#pragma mark - UISceneSession lifecycle
- (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.
}
//.h
@property (strong, nonatomic) UIWindow * window;
//.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.
}

苹果文档如是说

  • In iOS 13 and later, use UISceneDelegate objects to respond to life-cycle events in a scene-based app.

  • In iOS 12 and earlier, use the UIApplicationDelegate object to respond to life-cycle events.

不使用场景

当然,咱们也可以不使用场景.旧的不在info.plist中添加UIApplicationSceneManifest.或者是Xcode11以上的删除UIApplicationSceneManifest(Application Scene Manifest).然后就走的以前的Appdelegate的那些方法了.

如果要使用的话,记住需要判断一下版本.建议是用一个统一的类来处理这个问题.在内部转换成统一的通知名.方法等.方便处理

需要注意的点是,不使用场景的情况下别忘了在AppDelegate.h中添加window属性

@property (strong, nonatomic) UIWindow * window;

补充说明一下,如果不使用iPad的多窗口的话建议大家不要使用场景.

发布了268 篇原创文章 · 获赞 59 · 访问量 19万+

猜你喜欢

转载自blog.csdn.net/qq_18683985/article/details/100588757
今日推荐