如何在 iOS 15 上使用 UIWindowScene.windows? [英] How to use UIWindowScene.windows on iOS 15?

查看:218
本文介绍了如何在 iOS 15 上使用 UIWindowScene.windows?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

目前,在 iOS 14.6 中,我可以使用以下代码在我的应用中调用一个显示共享表的函数:

Currently, with iOS 14.6, I can call a function in my app that displays a share sheet using the following code:

func share(link: URL) {
    let activityView = UIActivityViewController(activityItems: [link], applicationActivities: nil)
    UIApplication.shared.windows.first?.rootViewController?.present(activityView, animated: true, completion: nil)
}

从 iOS 15 测试版开始,Xcode 告诉我'windows' 在 iOS 15.0 中被弃用:在相关的窗口场景中使用 UIWindowScene.windows".我如何更新它,以便我的共享表可以在这个新版本中正常工作?谢谢!

Since iOS 15 beta, Xcode tells me "'windows' was deprecated in iOS 15.0: Use UIWindowScene.windows on a relevant window scene instead". How can I update this so that my share sheet will work properly in this new version? Thanks!

推荐答案

这对我有用并且没有产生任何警告:

This worked for me and didn't produce any warnings:

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

来源:https://newbedev.com/how-to-resolve-keywindow-was-deprecated-in-ios-13-0

这篇关于如何在 iOS 15 上使用 UIWindowScene.windows?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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