如何解决:iOS 13.0中不推荐使用"keyWindow" [英] How to resolve: 'keyWindow' was deprecated in iOS 13.0

查看:958
本文介绍了如何解决:iOS 13.0中不推荐使用"keyWindow"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在将Core Data与Cloud Kit一起使用,因此必须在应用程序启动期间检查iCloud用户状态.出现问题时,我想向用户发出对话框,直到现在我都使用 UIApplication.shared.keyWindow?.rootViewController?.present(...)进行.

I'm using Core Data with Cloud Kit, and have therefore to check the iCloud user status during application startup. In case of problems I want to issue a dialog to the user, and I do it using UIApplication.shared.keyWindow?.rootViewController?.present(...) up to now.

在Xcode 11 beta 4中,现在有一条新的弃用消息告诉我:

In Xcode 11 beta 4, there is now a new deprecation message, telling me:

iOS 13.0中已弃用"keyWindow":不应用于支持多个场景的应用程序,因为它会返回所有已连接场景的关键窗口.

'keyWindow' was deprecated in iOS 13.0: Should not be used for applications that support multiple scenes as it returns a key window across all connected scenes

我该如何显示对话框?

推荐答案

这是我的解决方案:

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

用法例如:

keyWindow?.endEditing(true)

这篇关于如何解决:iOS 13.0中不推荐使用"keyWindow"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆