How do I use the Google Mobile Ads SDK in SwiftUI, or use the UIKit UIViewController within a SwiftUI view?

后端 未结 1 1396
自闭症患者
自闭症患者 2021-02-11 09:40

I have a SwiftUI view that I want to open a rewarded ad from the Google Mobile Ads SDK when I press a button. The instructions for loading the ads (https://developers.google.com

1条回答
  •  栀梦
    栀梦 (楼主)
    2021-02-11 10:09

    I have used a very simple approach. I have a delegate class which loads the rewarded Ad and informs the view that its loaded, hence the view presents it. When user watches the full Ad, same delegate gets the success callback and it informs the view about it.

    The code for delegate looks like:-

    class RewardedAdDelegate: NSObject, GADRewardedAdDelegate, ObservableObject {
    @Published var adLoaded: Bool = false
    @Published var adFullyWatched: Bool = false
    
    var rewardedAd: GADRewardedAd? = nil
    
    func loadAd() {
        rewardedAd = GADRewardedAd(adUnitID: "ca-app-pub-3940256099942544/1712485313")
            rewardedAd!.load(GADRequest()) { error in
              if error != nil {
                self.adLoaded = false
              } else {
                self.adLoaded = true
              }
            }
    }
    
    /// Tells the delegate that the user earned a reward.
    func rewardedAd(_ rewardedAd: GADRewardedAd, userDidEarn reward: GADAdReward) {
        adFullyWatched = true
    }
    
    /// Tells the delegate that the rewarded ad was presented.
    func rewardedAdDidPresent(_ rewardedAd: GADRewardedAd) {
         self.adLoaded = false
    }
    
    /// Tells the delegate that the rewarded ad was dismissed.
    func rewardedAdDidDismiss(_ rewardedAd: GADRewardedAd) {}
    
    /// Tells the delegate that the rewarded ad failed to present.
    func rewardedAd(_ rewardedAd: GADRewardedAd, didFailToPresentWithError error: Error) {}
    }
    

    Now you need a view to initiate and present the RewardedAd:-

    struct RewardedAd: View {
    @ObservedObject var adDelegate = RewardedAdDelegate()
    
    var body: some View {
        if adDelegate.adLoaded && !adDelegate.adFullyWatched {
            let root = UIApplication.shared.windows.first?.rootViewController
            self.adDelegate.rewardedAd!.present(fromRootViewController: root!, delegate: adDelegate)
        }
        
        return Text("Load ad").onTapGesture {
            self.adDelegate.loadAd()
        }
    }
    }
    

    Explanation:- In the above view when user taps on Load Ad, we initiate the loading and then the delegate updates the Published Boolean. This informs our view that ad is Loaded and we call:-

    let root = UIApplication.shared.windows.first?.rootViewController self.adDelegate.rewardedAd!.present(fromRootViewController: root!, delegate: adDelegate)

    The Ad is now playing on screen and if user watches is completely, the delegate will get a success callback and it'll update another Published Boolean. This will inform us that we need to reward the user now (you can add your way to handle/reward the user).

    I hope this helps. Happy coding...

    0 讨论(0)
提交回复
热议问题