我們把Xcode升級(jí)到11.0以后漆弄,新建工程的時(shí)候就會(huì)多一個(gè)SceneDelegate 文件业簿,這是蘋果兼容iPadOS新添加的文件乓序。
1隔箍、 Xcode 11 默認(rèn)是會(huì)創(chuàng)建通過 UIScene 管理多個(gè) UIWindow 的應(yīng)用谓娃,工程中除了 AppDelegate 外會(huì)多一個(gè) SceneDelegate
2、AppDelegate和SceneDelegate這是iPadOS帶來的新的多窗口支持的結(jié)果蜒滩,并且有效地將應(yīng)用程序委托的工作分成兩部分滨达。
那AppDelegate 和 SceneDelegate到底有什么區(qū)別
- 在iOS 13以后
AppDelegate
的功能發(fā)生了變化奶稠,把一部分功能交給了SceneDelegate
處理
在iOS13 以前AppDelegate
的功能,全權(quán)處理App生命周期和UI生命周期捡遍,如下圖锌订。
iOS13 以前AppDelegate功能圖
- 在iOS 13以后
AppDelegate
的功能 和SceneDelegate
功能對照圖
iOS 13以后AppDelegate功能圖
SceneDelegate功能圖
SceneDelegate適配
方案一
如果我們不開發(fā)iPadOS多窗口APP,SceneDelegate窗口管理我們可以不需要直接刪掉就好了画株。
1.刪除掉info.plist中Application Scene Manifest選項(xiàng)辆飘,同時(shí),文件SceneDelegate可刪除可不刪
2.相關(guān)代碼注釋掉
//注釋下面兩個(gè)方法
func application(_ application: UIApplication, configurationForConnecting connectingSceneSession: UISceneSession, options: UIScene.ConnectionOptions) -> UISceneConfiguration {
return UISceneConfiguration(name: "Default Configuration", sessionRole: connectingSceneSession.role)
}
func application(_ application: UIApplication, didDiscardSceneSessions sceneSessions: Set<UISceneSession>) {
}
//在AppDelegate 中添加window 屬性
var window: UIWindow?
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
// Override point for customization after application launch.
window = UIWindow.init()
window?.frame = UIScreen.main.bounds
window?.makeKeyAndVisible()
window?.rootViewController = UIViewController.init()
return true
}
方案二
即要用iOS 13中新的SceneDelegate谓传,又可以在iOS 13一下的設(shè)備中完美運(yùn)行蜈项。那就添加版本判斷,利用@available
1.SceneDelegate中添加@available(iOS 13, *)
import UIKit
@available(iOS 13, *)
class SceneDelegate: UIResponder, UIWindowSceneDelegate {
var window: UIWindow?
func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions) {
let windowScene:UIWindowScene = scene as! UIWindowScene
window = UIWindow.init(windowScene: windowScene)
window?.backgroundColor = UIColor.green
window?.frame = UIScreen.main.bounds
window?.makeKeyAndVisible()
let tt = UIViewController.init()
tt.view.backgroundColor = UIColor.brown
window?.rootViewController = tt
guard let _ = (scene as? UIWindowScene) else { return }
}
}
2.AppDelegate中同樣聲明window屬性续挟,AppDelegate中兩個(gè)關(guān)于Scene的類也添加版本控制紧卒,Swift中可以用擴(kuò)展單獨(dú)拎出來,如下:
@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {
var window: UIWindow?
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
// Override point for customization after application launch.
if #available(iOS 13, *) {
}else {
window = UIWindow.init()
window?.frame = UIScreen.main.bounds
window?.makeKeyAndVisible()
window?.rootViewController = UIViewController.init()
}
return true
}
}
@available(iOS 13.0, *)
extension AppDelegate {
// MARK: UISceneSession Lifecycle
func application(_ application: UIApplication, configurationForConnecting connectingSceneSession: UISceneSession, options: UIScene.ConnectionOptions) -> UISceneConfiguration {
return UISceneConfiguration(name: "Default Configuration", sessionRole: connectingSceneSession.role)
}
func application(_ application: UIApplication, didDiscardSceneSessions sceneSessions: Set<UISceneSession>) {
}
}
本文參考1:https://blog.csdn.net/weixin_38735568/article/details/101266408
本文參考2:http://www.reibang.com/p/801de3beee22