简体   繁体   中英

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.

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

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

Step 2: Post a notification from inside the InterstitialAd class

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

Step 3: handle OnReceive() on any view as needed

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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