【发布时间】:2020-02-12 18:25:12
【问题描述】:
我需要支持 iOS 12 和 iOS 13。
我应该在AppDelegate 和SceneDelegate 之间复制代码吗?
例如:
func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions) {
guard let windowScene = (scene as? UIWindowScene) else { return }
let window = UIWindow(windowScene: windowScene)
window.rootViewController = HomeViewController()
window.makeKeyAndVisible()
self.window = window
}
和
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
let window = UIWindow(frame: UIScreen.main.bounds)
window.rootViewController = HomeViewController()
window.makeKeyAndVisible()
self.window = window
return true
}
如果我不这样做,在 1 个版本中我最终会出现黑屏,但如果我这样做并在 HomeViewController 的 viewDidLoad 方法中打印,我可以看到它被调用了两次。
我更新了我的didFinishLaunchingWithOptions,我可以在iOS13 中看到它仍然被调用了两次。
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
guard #available(iOS 12, *) else { return true }
let window = UIWindow(frame: UIScreen.main.bounds)
window.rootViewController = HomeViewController()
window.makeKeyAndVisible()
self.window = window
return true
}
【问题讨论】:
-
您也可以完全跳过 SceneDelegate,如果您支持 iOS 12,则无需继承。
-
请注意,如果您计划构建现代 Catalyst 应用程序,可能需要采用场景代理。 macOS 上的 Segmented bar 等功能需要您采用 Scene Delegate。
标签: ios swift appdelegate uiscenedelegate