iOS13以后获取UIWindow的方式有调整,需要通过sceneDelegate获取;示例代码如下:
class func currentWindow() -> UIWindow? {
guard #available(iOS 13.0, *) else {
if let appDelegate = UIApplication.shared.delegate,
let window = appDelegate.window {
return window
}
}
guard let scene = UIApplication.shared.connectedScenes.first,
let sceneDelegate = scene.delegate as? UIWindowSceneDelegate,
let window = sceneDelegate.window else {
return UIApplication.shared.windows.last
}
return window
}