How do I get the keyWindow reference in a swift app?

后端 未结 8 987
爱一瞬间的悲伤
爱一瞬间的悲伤 2021-02-12 03:55

In Objective-C in the viewDidLoad method of a UIViewController I do this to get the keyWindow reference in my iOS app:

 UIWindow * keyWindow = [[UIApplication sh         


        
8条回答
  •  青春惊慌失措
    2021-02-12 04:03

    For Swift 5:

    var keyWindow: UIWindow? {
        return UIApplication.shared.connectedScenes.filter({$0.activationState == .foregroundActive}).map({$0 as? UIWindowScene}).compactMap({$0}).first?.windows.filter({$0.isKeyWindow}).first
    }
    

提交回复
热议问题