【问题标题】: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?如何使用 AdMob 横幅摆脱消息“'windows' 在 iOS 15.0 中已弃用:在相关窗口场景上使用 UIWindowScene.windows”?
【发布时间】:2021-12-07 12:59:38
【问题描述】:

我想在我的函数中删除“'windows' was deprecated in iOS 15.0: Use UIWindowScene.windows on a related window scene instead”消息(顺便说一句,我的函数运行良好。)。

涉及的代码行:

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>) {}
}

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

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

谢谢

【问题讨论】:

    标签: swiftui admob rootviewcontroller uiwindowscene


    【解决方案1】:

    您可以使用以下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
            
        }
    }
    

    【讨论】:

    • Ummmm 你忘了声明filteredScene???我得到一个“在范围内找不到 'filteredScene'”
    • 感谢您的收获!现在正确,我刚刚通过 iPad 复制了我正在使用的代码(经过一些简化)并忘记将其删除。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-01-16
    • 2021-09-20
    • 2021-01-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多