Xcode 11 建新工程默認(rèn)會創(chuàng)建通過 UIScene 管理多個 UIWindow 的應(yīng)用,工程中除了 AppDelegate 外還會有一個 SceneDelegate,這是為了實現(xiàn)iPadOS支持多窗口的結(jié)果。AppDelegate.h不再有window屬性,window屬性被定義在了SceneDelegate.h中,AppDelegate中有新增的關(guān)于scene的代理方法锥债,SceneDelegate中也有相應(yīng)的代理方法。因此痊臭,當(dāng)我們用Xcode11針對不同版本的iOS開發(fā)應(yīng)用時哮肚,就要做一些適配。
創(chuàng)建好一個工程后广匙,可以看到相關(guān)Xcode開發(fā)界面變成了下面這樣:
AppDelegate.h中多了兩個默認(rèn)的代理方法:
#pragma mark - UISceneSession lifecycle
/*
關(guān)于Scene的配置已默認(rèn)在info.plist中進行允趟, 如果已在info.plist中配置過,不用實現(xiàn)該方法也沒有關(guān)系鸦致。如果沒有配置就需要實現(xiàn)這個方法并返回一個UISceneConfiguration對象拼窥。
配置參數(shù)中Application Session Role 是個數(shù)組,每一項有三個參數(shù):
Configuration Name: 當(dāng)前配置的名字;
Delegate Class Name: 與哪個Scene代理對象關(guān)聯(lián);
StoryBoard name: 這個Scene使用的哪個storyboard蹋凝。
注意:代理方法中調(diào)用的是配置名為Default Configuration的Scene,則系統(tǒng)就會自動去調(diào)用SceneDelegate這個類总棵。這樣SceneDelegate和AppDelegate產(chǎn)生了關(guān)聯(lián)鳍寂。
*/
- (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];
}
// 在分屏中關(guān)閉其中一個或多個scene時候回調(diào)用
- (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中的默認(rèn)代理方法如下:
- (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的配置數(shù)據(jù)迄汛,AppDelegate中關(guān)于Scene的代理方法、SceneDelegate的類是否刪除都可以骤视。
如果使用純代碼來實現(xiàn)顯示界面鞍爱,需要在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提供的生命周期方法沉填,并且需要針對ios13需要在Scene中配置和ios13以下在AppDelegate中做兩套配置疗隶。
下面是純代碼實現(xiàn)界面顯示的代碼:
//// 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 {
//在這里手動創(chuàng)建新的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項刪除: