Xcode11 SceneDelegate适配方案

Xcode11之后,我们创建的工程中,会多出SceneDelegate.hSceneDelegate.m文件。

在iOS13之前,App生命周期和UI生命周期是交由AppDelegate来管理的;

在iOS13之后,为了为了iPadOS的多窗口支持,交由AppDelegate和SceneDelegate来管理;

那么问题来了,我们不可能设置工程最低支持iOS13,如果才能保留SceneDelegate并且兼容之前的代码呢?

思路:添加支持版本定义 API_AVAILABLE(ios(13.0))

代码如下:

// 1. 在SceneDelegate.h中更改

#import <UIKit/UIKit.h>

UIKIT_EXTERN API_AVAILABLE(ios(13.0)) @interface SceneDelegate : UIResponder <UIWindowSceneDelegate>

@property (strong, nonatomic) UIWindow * window;

@end

// 2. 在AppDelegate.m中更改
#import "AppDelegate.h"

@interface AppDelegate ()

@end

@implementation AppDelegate


- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    // Override point for customization after application launch.
    return YES;
}

#pragma mark - UISceneSession lifecycle


- (UISceneConfiguration *)application:(UIApplication *)application configurationForConnectingSceneSession:(UISceneSession *)connectingSceneSession options:(UISceneConnectionOptions *)options API_AVAILABLE(ios(13.0)) {
    // 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 API_AVAILABLE(ios(13.0)) {
    // 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.
}

@end

©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。