背景:自从Xcode11发布以来,当使用新Xcode创建一个新的ios项目时,sceneDelegate会被默认创建 |
那么,这个多出的类是做什么的呢?
简单来说,是ios13之后AppDelegate这个类的职责发生了改变!
在ios之前,Appdelegate的职责全权处理App生命周期和UI生命周期,如图:
而在ios13之后,Appdelegate的职责发生了相应的改变!
1、处理 App 生命周期
2、新的 Scene Session 生命周期
而UI的生命周期就交给新增的SceneDelegate来处理,如图!
这样的话,我们在Xcode11新建iOS项目时,如果你跟往常一样在Appdelegate的- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions方法中创建根控制器,就会崩溃报错!那解决办法有2个!
1,把原先在didFinishLaunchingWithOptions写代码移到SceneDelegate的willConnectTo方法中,如图:
2,如果您所用到的应用程序并不需要这个功能,可以相应的删除以下代码,这样就跟xcode11之前的版本一样啦~
但就目前看来,适配SceneDelegate还是很有必要的,比如用到的swiftUI,小组件等功能,都用到了SceneDelegate的类,所以我们还是未雨绸缪的先了解一下~
我们先看一下在appdelegate做了什么
//会返回一个创建场景时需要的UISceneConfiguration对象
func application(_ application: UIApplication, configurationForConnecting connectingSceneSession: UISceneSession, options: UIScene.ConnectionOptions) -> UISceneConfiguration {
print("__7__configurationForConnecting")
// Called when a new scene session is being created.
// Use this method to select a configuration to create the new scene with.
return UISceneConfiguration(name: "Default Configuration", sessionRole: connectingSceneSession.role)
}
//当用户通过“应用切换器”关闭一个或多个场景时会被调用
func application(_ application: UIApplication, didDiscardSceneSessions sceneSessions: Set<UISceneSession>) {
print("__8__didDiscardSceneSessions")
// 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.
}
在以下图片中,当需要设置多场景的时候,mutiple windows属性应设置为Yes,在application session role中的configuration name应与代码中UIsceneConfiguration的配置名字相同,而delegate class name应与所在的类名相同!
好啦~再来看一下sceneDelegate中的代码
//当场景添加到app中时,函数会被调用,这里是配置场景的最理想地方,相当于application的didFinishLaunchingWithOptions!(一个delegate配置所有场景)
func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions) {
print("__1__willConnectTo")
// 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).
guard let _ = (scene as? UIWindowScene) else { return }
// Create the SwiftUI view that provides the window contents.
let contentView = ContentView()
// // Use a UIHostingController as window root view controller.
if let windowScene = scene as? UIWindowScene {
let window = UIWindow(windowScene: windowScene)
window.rootViewController = UIHostingController(rootView: contentView)
self.window = window
window.makeKeyAndVisible()
}
}
//当场景与app断开连接是调用(注意,以后它可能被重新连接)
func sceneDidDisconnect(_ scene: UIScene) {
print("__2__sceneDidDisconnect")
// 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 necessarily discarded (see `application:didDiscardSceneSessions` instead).
}
//当用户开始与场景进行交互(例如从应用切换器中选择场景)时,会调用
func sceneDidBecomeActive(_ scene: UIScene) {
print("__3__sceneDidBecomeActive")
// 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.
}
//当用户停止与场景交互(例如通过切换器切换到另一个场景)时调用
func sceneWillResignActive(_ scene: UIScene) {
print("__4__sceneWillResignActive")
// 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).
}
//当场景变成活动窗口时调用,即从后台状态变成开始或恢复状态
func sceneWillEnterForeground(_ scene: UIScene) {
print("__5__sceneWillEnterForeground")
// Called as the scene transitions from the background to the foreground.
// Use this method to undo the changes made on entering the background.
}
//当场景进入后台时调用,即该应用已最小化但仍存活在后台中
func sceneDidEnterBackground(_ scene: UIScene) {
print("__6__sceneDidEnterBackground")
// 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.
}
注:如果适配了SceneDelegate,在ios13以下的application的判断app状态的方法失效
官网支持多场景:
https://developer.apple.com/documentation/uikit/uiscenedelegate/supporting_multiple_windows_on_ipad
参考文章:
1,https://blog.csdn.net/potato512/article/details/106542809
2,https://www.jianshu.com/p/6d6573fbd60b