如何使用 AdMob 横幅删除消息“'windows' 在 iOS 15.0 中被弃用:在相关的 window 场景中使用 UIWindowScene.windows 而不是”?

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?

我想在我的函数中删除“'windows' 在 iOS 15.0 中被弃用:在相关 window 场景上使用 UIWindowScene.windows 代替”消息(顺便说一句,我的功能工作得很好。)。

涉及的代码行:

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

谢谢

您可以使用以下方式 UIApplication.shared.currentUIWindow()?.rootViewController

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

        return window
        
    }
}