如何获取 iOS 14 @main 文件中的 UIWindow 值?

2023-11-27

我可以访问didFinishLaunchingWithOptions通过下面的实现。但是我需要UIWindow多变的。我不知道如何得到它。我正在使用 Xcode 12 beta。 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
    }

    [...]
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

如何获取 iOS 14 @main 文件中的 UIWindow 值? 的相关文章

随机推荐