gpt4 book ai didi

ios - 如何在 iOS 14 @main 文件中获取 UIWindow 值?

转载 作者:行者123 更新时间:2023-12-01 17:46:05 26 4
gpt4 key购买 nike

我可以访问 didFinishLaunchingWithOptions 通过下面的实现。但是,我需要 界面窗口 多变的。我不知道如何得到它。我正在使用 Xcode 12 测试版。 iOS14,SwiftUI 生命周期。


import SwiftUI

@main
struct SSOKit_DemoApp: App {

@UIApplicationDelegateAdaptor(AppDelegate.self) var appDelegate

var body: some Scene {
WindowGroup {
ContentView()
}
}
}

class AppDelegate: NSObject, UIApplicationDelegate {

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey : Any]? = nil) -> Bool {

print("hello world!!!")
return true
}
}

最佳答案

从 iOS 13 开始,可以安全地假设获取对关键窗口的引用的正确方法是通过 UIWindowSceneDelegate .

@main
struct DemoApp: App {

var window: UIWindow? {
guard let scene = UIApplication.shared.connectedScenes.first,
let windowSceneDelegate = scene.delegate as? UIWindowSceneDelegate,
let window = windowSceneDelegate.window else {
return nil
}
return window
}

[...]
}

关于ios - 如何在 iOS 14 @main 文件中获取 UIWindow 值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62652555/

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