一、不使用SceneDelegate:
①在
Objective-C
項(xiàng)目中:移除SceneDelegate
1.先直接刪除SceneDelegate.h/.m文件
2.在AppDelegate.h添加@property (strong, nonatomic) UIWindow * window;屬性
#import <UIKit/UIKit.h>
@interface AppDelegate : UIResponder <UIApplicationDelegate>
@property (strong, nonatomic) UIWindow * window;
@end
3.移除UIScene代理
移除之前
#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 {
// 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.
}//移除
@end
移除之后
#import "AppDelegate.h"
@interface AppDelegate ()
@end
@implementation AppDelegate
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
// Override point for customization after application launch.
return YES;
}
@end
4.最后在info.plist文件中移除Application Scene Manifest
②在
Swift
項(xiàng)目中:移除SceneDelegate
1.這是新建一個(gè)Swift
項(xiàng)目是默認(rèn)的項(xiàng)目文件結(jié)構(gòu):
2.直接刪除SceneDelegate.swift
這個(gè)文件 隙畜;
3.在Info.plist
中刪除Application Scene Manifest
相關(guān):
4.在 AppDelegate.swift
中刪除和UISceneSession
相關(guān)的東西(紅框中的函數(shù)):
5.在AppDelegate.swift
中新建UIWindow
相關(guān):
@main
class AppDelegate: UIResponder, UIApplicationDelegate {
var window: UIWindow?
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
// Override point for customization after application launch.
let vc = ViewController()
window?.backgroundColor = .white;
window?.rootViewController = UINavigationController(rootViewController: vc)
window?.makeKeyAndVisible()
print("hello她倘,world");
return true
}
}
因?yàn)楝F(xiàn)在默認(rèn)啟動(dòng)程序時(shí)頁面的背景默認(rèn)時(shí)黑色的澳迫,可以在window這里設(shè)置背景為白色。
6.現(xiàn)在運(yùn)行項(xiàng)目就可以正常運(yùn)行了。
二神得、使用SceneDelegate奈籽,(多Scene饥侵,僅ipad有效)
xcode11創(chuàng)建項(xiàng)目新增SceneDelegate文件,AppDelegate文件結(jié)構(gòu)也發(fā)生變化衣屏,在AppDelegate.h文件中沒有了window屬性躏升,而是在SceneDelegate.h中,可見AppDelegate不管理window而是交給SceneDelegate狼忱。由于這些是ios13新增膨疏,所以SceneDelegate在ios13以下的系統(tǒng)是不支持。所以xcode11創(chuàng)建的項(xiàng)目如要做一下處理:
如果App不需要支持多個(gè)scene藕赞,同時(shí)兼容ios13以下成肘,可以刪除info.plist文件中的Application Scene Manifest的配置數(shù)據(jù)。在AppDelegate.h中添加window屬性斧蜕,同時(shí)刪除UISceneSession的生命周期方法双霍。和以前的使用方式一樣∨可參考iOS移除SceneDelegate
如果在ios13中支持多scene洒闸,原先AppDelegate的生命周期方法不再起作用。
而是在SceneDelegate中使用UIScene提供的生命周期方法
開啟支持多scene(僅iPad有效)
由于勾選分屏均芽,在info.plist 中 Application Scene Manifest
-> enable Multipe Windows
設(shè)置為YES.
默認(rèn)在info.plist中進(jìn)行了配置丘逸, 不用實(shí)現(xiàn)該方法也沒有關(guān)系。如果沒有配置就需要實(shí)現(xiàn)這個(gè)方法并返回一個(gè)UISceneConfiguration對(duì)象掀宋。
在上圖中配置參數(shù)中Application Session Role 是個(gè)數(shù)組深纲,每一項(xiàng)有三個(gè)參數(shù)仲锄,
Configuration Name: 當(dāng)前配置的名字
Delegate Class Name: 與哪個(gè)Scene代理對(duì)象關(guān)聯(lián),
StoryBoard name: 這個(gè)Scene使用的哪個(gè)storyboard湃鹊。
AppDelegate.m中多了UISceneSession的生命周期的代理方法
//1.如果沒有在APP的Info.plist文件中包含scene的配置數(shù)據(jù)儒喊,或者要?jiǎng)討B(tài)更改場(chǎng)景配置數(shù)據(jù),需要實(shí)現(xiàn)此方法币呵。 UIKit會(huì)在創(chuàng)建新scene前調(diào)用此方法怀愧。
//參數(shù)options是一個(gè)UISceneConnectionOptions類,官方解釋:它包含了為什么要?jiǎng)?chuàng)建一個(gè)新的scene的信息余赢。根據(jù)參數(shù)信息判斷是否要?jiǎng)?chuàng)建一個(gè)新的scene
- (UISceneConfiguration *)application:(UIApplication *)application configurationForConnectingSceneSession:(UISceneSession *)connectingSceneSession options:(UISceneConnectionOptions *)options {
NSLog(@"1");
// 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];
}
//方法會(huì)返回一個(gè)UISceneConfiguration對(duì)象芯义,其包含其中包含場(chǎng)景詳細(xì)信息,包括要?jiǎng)?chuàng)建的場(chǎng)景類型妻柒,用于管理場(chǎng)景的委托對(duì)象以及包含要顯示的初始視圖控制器的情節(jié)提要扛拨。 如果未實(shí)現(xiàn)此方法,則必須在應(yīng)用程序的Info.plist文件中提供場(chǎng)景配置數(shù)據(jù)举塔。
//總結(jié)下:默認(rèn)在info.plist中進(jìn)行了配置鬼癣, 不用實(shí)現(xiàn)該方法也沒有關(guān)系。如果沒有配置就需要實(shí)現(xiàn)這個(gè)方法并返回一個(gè)UISceneConfiguration對(duì)象啤贩。
//在分屏中關(guān)閉其中一個(gè)或多個(gè)scene時(shí)候回調(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.
NSLog(@"2");
}
在SceneDelegate中不使用storyboard創(chuàng)建拜秧,手動(dòng)創(chuàng)建新的window
- (void)scene:(UIScene *)scene willConnectToSession:(UISceneSession *)session options:(UISceneConnectionOptions *)connectionOptions {
//在這里手動(dòng)創(chuàng)建新的window
if (scene) {
UIWindowScene *windowScene = (UIWindowScene *)scene;
self.window = [[UIWindow alloc] initWithWindowScene:windowScene];
self.window.frame = CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, [UIScreen mainScreen].bounds.size.height);
self.window.rootViewController = [ViewController new];
[self.window makeKeyAndVisible];
}
}
SceneDelegate生命周期
- (void)scene:(UIScene *)scene willConnectToSession:(UISceneSession *)session options:(UISceneConnectionOptions *)connectionOptions {
NSLog(@"場(chǎng)景加載完成");
}
- (void)sceneDidDisconnect:(UIScene *)scene {
NSLog(@"場(chǎng)景已經(jīng)斷開連接");
}
- (void)sceneDidBecomeActive:(UIScene *)scene {
NSLog(@"已經(jīng)從后臺(tái)進(jìn)入前臺(tái)");
}
- (void)sceneWillResignActive:(UIScene *)scene {
NSLog(@"即將從前臺(tái)進(jìn)入后臺(tái)");
}
- (void)sceneWillEnterForeground:(UIScene *)scene {
NSLog(@"即將從后臺(tái)進(jìn)入前臺(tái)");
}
- (void)sceneDidEnterBackground:(UIScene *)scene {
NSLog(@"已經(jīng)從前臺(tái)進(jìn)入后臺(tái)");
}