gpt4 book ai didi

ios - 支持 iOS 12 和 13 时的 AppDelegate 和 SceneDelegate

转载 作者:行者123 更新时间:2023-12-04 20:51:17 26 4
gpt4 key购买 nike

我需要支持 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 版本中我会出现黑屏,但如果我这样做并在 viewDidLoad 中打印 HomeViewController的方法我可以看到它被调用了两次。

我更新了我的 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
}

最佳答案

您确实需要复制代码,但您需要确保它仅在正确的系统上运行。在 iOS 13 中,您不希望该应用程序委托(delegate) didFinishLaunching要运行的正文代码,因此请使用可用性检查来防止它。
同样,使用可用性从 iOS 12 中隐藏窗口场景的东西。

这是在 iOS 12 和 iOS 13 上都能正常运行的解决方案的基本草图:

AppDelegate.Swift

import UIKit
@UIApplicationMain
class AppDelegate : UIResponder, UIApplicationDelegate {
var window : UIWindow?
func application(_ application: UIApplication,
didFinishLaunchingWithOptions
launchOptions: [UIApplication.LaunchOptionsKey : Any]?)
-> Bool {
if #available(iOS 13, *) {
// do only pure app launch stuff, not interface stuff
} else {
self.window = UIWindow()
let vc = ViewController()
self.window!.rootViewController = vc
self.window!.makeKeyAndVisible()
self.window!.backgroundColor = .red
}
return true
}
}

SceneDelegate.swift
import UIKit
@available(iOS 13.0, *)
class SceneDelegate: UIResponder, UIWindowSceneDelegate {
var window : UIWindow?
func scene(_ scene: UIScene,
willConnectTo session: UISceneSession,
options connectionOptions: UIScene.ConnectionOptions) {
if let windowScene = scene as? UIWindowScene {
self.window = UIWindow(windowScene: windowScene)
let vc = ViewController()
self.window!.rootViewController = vc
self.window!.makeKeyAndVisible()
self.window!.backgroundColor = .red
}
}
}

ViewController.swift
import UIKit
class ViewController : UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
print("view did load")
self.view.backgroundColor = .green
}
}

请注意,处理其他重复项(例如应用程序激活)要简单得多,因为如果您支持窗口场景,则不会在 iOS 12 上调用应用程序委托(delegate)方法。所以问题仅限于这种情况,即您有窗口/在启动时执行的 Root View Controller 操作(例如,没有 Storyboard )。

关于ios - 支持 iOS 12 和 13 时的 AppDelegate 和 SceneDelegate,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58405393/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com