简体   繁体   English

我们如何等待和收听 admob 插页式广告在 SwiftUI 视图中被关闭?

[英]How do we wait and listen for an admob interstitial ad to be dismissed in a SwiftUI View?

I am looking for a solution for pausing app activity, such as music, while an interstitial ad is being shown, and then listening for the "adDidDismissFullScreenContent" notification before restarting music, etc. The showAd() func is working, but because I am using SwiftUI I can't use the GADFullScreenContentDelegate directly.我正在寻找一种解决方案,用于在显示插页式广告时暂停应用程序活动(例如音乐),然后在重新启动音乐等之前收听“adDidDismissFullScreenContent”通知。showAd() 函数正在工作,但因为我使用 SwiftUI 我不能直接使用 GADFullScreenContentDelegate 。

struct ContentView: View {
    var fullScreenAd: InterstitialAd?
    
    init() {
        fullScreenAd = InterstitialAd()
    }
    
    var body: some View {
    
        Button("Show Ad") {
            if fullScreenAd != nil {
                fullScreenAd!.showAd()
                // TODO: Pause activity until the ad has been dismissed.
            }
        }
    }
}

final class InterstitialAd: NSObject, GADFullScreenContentDelegate {
    var interstitial: GADInterstitialAd?
    var adID = "ca-app-pub-3940256099942544/4411468910"
    
    override init() {
        super.init()
        self.loadInterstitial()
    }
    
    func loadInterstitial() {
        let request = GADRequest()
        GADInterstitialAd.load(withAdUnitID: adID,
                               request: request,
                               completionHandler: { [self] ad, error in
            if let error = error {
                print("Failed to load interstitial")
                return
            }
            interstitial = ad
            interstitial?.fullScreenContentDelegate = self
        })
    }
    
    func showAd() {
        if self.interstitial != nil {
            guard let firstScene = UIApplication.shared.connectedScenes.first as? UIWindowScene else {
                return
            }
            guard let firstWindow = firstScene.windows.first else {
                return
            }
            let root = firstWindow.rootViewController
            self.interstitial?.present(fromRootViewController: root!)
        }
        else{
            print("Not Ready")
        }
    }
    
    // Tells the delegate that the ad dismissed full screen content.
    func adDidDismissFullScreenContent(_ ad: GADFullScreenPresentingAd) {
        print("Ad did dismiss full screen content.")
        self.loadInterstitial()
    }
}

I found a solution by using Notification Center, as follows:我通过通知中心找到了一个解决方案,如下:

Step 1: Extend notifications第 1 步:扩展通知

extension NSNotification.Name {
    static let onAdDidDismiss = Notification.Name("onAdDidDismiss")
}

Step 2: Post a notification from inside the InterstitialAd class第 2 步:从 InterstitialAd class 内部发布通知

    func adDidDismissFullScreenContent(_ ad: GADFullScreenPresentingAd) {
        NotificationCenter.default.post(name: .onAdDidDismiss, object: nil)
        self.loadInterstitial()
    }

Step 3: handle OnReceive() on any view as needed第 3 步:根据需要在任何视图上处理 OnReceive()

 VStack {
        ...
        }
        .onReceive(NotificationCenter.default.publisher(for: .onAdDidDismiss)) { _ in
            musicIsPlaying = true
        }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM