如何摆脱消息 "'windows' 在 iOS 15.0 中被弃用:在相关的窗口场景上使用 UIWindowScene.windows 代替"使用 AdMob 横幅? [英] How to get rid of message " 'windows' was deprecated in iOS 15.0: Use UIWindowScene.windows on a relevant window scene instead" with AdMob banner?

查看:35
本文介绍了如何摆脱消息 "'windows' 在 iOS 15.0 中被弃用:在相关的窗口场景上使用 UIWindowScene.windows 代替"使用 AdMob 横幅?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想摆脱"'windows' 在 iOS 15.0 中被弃用:在相关的窗口场景上使用 UIWindowScene.windows 代替"我的函数中的消息(顺便说一下,我的函数运行良好.).

I want to get rid of the " 'windows' was deprecated in iOS 15.0: Use UIWindowScene.windows on a relevant window scene instead" message in my function (my function works very well by the way.).

涉及的代码行:

banner.rootViewController = UIApplication.shared.windows.first?.rootViewController

我该怎么做?

我的功能:

struct AdView : UIViewRepresentable {
    func makeUIView(context: UIViewRepresentableContext<AdView>) -> GADBannerView {
        let banner = GADBannerView(adSize: kGADAdSizeBanner)
        banner.adUnitID = "ca-app-pub-3940256099942544/2934735716" // test ad
                banner.rootViewController = UIApplication.shared.windows.first?.rootViewController
        banner.load(GADRequest())
        return banner
    }
    
    func updateUIView(_ uiView: GADBannerView, context: UIViewRepresentableContext<AdView>) {}
}

我试过了,但它不起作用:

I tried that but it's not working :

banner.rootViewController = UIApplication.shared.connectedScenes.first?.rootViewController

谢谢

推荐答案

您可以使用以下作为 UIApplication.shared.currentUIWindow()?.rootViewController

public extension UIApplication {
    func currentUIWindow() -> UIWindow? {
        let connectedScenes = UIApplication.shared.connectedScenes
            .filter({
                $0.activationState == .foregroundActive})
            .compactMap({$0 as? UIWindowScene})
        
        let window = connectedScenes.first?
            .windows
            .first { $0.isKeyWindow }

        return window
        
    }
}

这篇关于如何摆脱消息 &quot;'windows' 在 iOS 15.0 中被弃用:在相关的窗口场景上使用 UIWindowScene.windows 代替"使用 AdMob 横幅?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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