swiftswiftuiadmobswift5uiapplication

View won't show when calling by function in SwiftUI


I have this problem. I'm trying to show Ads in my app using AdMob. It all works fine (i did some debug) but the view containing the video is not showing... I can't figure out why it's not showing on the top of my content.

Here is the the class that calls and opens the view with ads:

    import SwiftUI
import GoogleMobileAds
import UIKit
    
class Rewarded: NSObject, GADRewardedAdDelegate, ObservableObject{
    @Published var adLoaded: Bool = false
    var rewardedAd:GADRewardedAd = GADRewardedAd(adUnitID: "ca-app-pub-3940256099942544/1712485313")
    
    var rewardFunction: (() -> Void)? = nil
    
    override init() {
        super.init()
        LoadRewarded()
    }
    
    func LoadRewarded(){
        let req = GADRequest()
        self.rewardedAd.load(req) { error in
            if error != nil {
                self.adLoaded = false
                print("Ad not loaded")
            } else {
                self.adLoaded = true
                print("Ad loaded")
            }
        }
    }
    
    func showAd(rewardFunction: @escaping () -> Void){
        if self.rewardedAd.isReady {
            print("Ad is ready to show")
            self.rewardFunction = rewardFunction
            if let root = UIApplication.shared.windows.first?.rootViewController {
                self.rewardedAd.present(fromRootViewController: root, delegate: self)
                print("Presenting")
            } else {
                print("Failed to open ad")
            }
        } else {
           print("Ad not ready to show")
       }
    }
    
    func rewardedAd(_ rewardedAd: GADRewardedAd, userDidEarn reward: GADAdReward) {
        if let rf = rewardFunction {
            rf()
        }
    }
    
    func rewardedAdDidDismiss(_ rewardedAd: GADRewardedAd) {
        self.rewardedAd = GADRewardedAd(adUnitID: "ca-app-pub-3940256099942544/1712485313")
        LoadRewarded()
    }
}

And here is the SwiftUI portion of the code that calls the function of the class:

Button {
                            self.rewardAd.showAd(rewardFunction: {
                                // GIVE REWARD HERE
                            })
                        } label: {
                            Text("Watch Ad")
                                    .foregroundColor(Color.white)
                                .font(.custom(customFont, size: 17))
                                    .fontWeight(.bold)
                        }

Also the console doesn't print any error...

Does anybody know why it is not showing?

Thank you all


Solution

  • This works for me. I had a heck of a time getting this to work and tried following all of the examples I could find, finally landed on this.

        func showAd(rewardFunction: @escaping () -> Void){
            if let ad = rewardedAd {
                self.rewardFunction = rewardFunction
                if let root = UIApplication.shared.windows.first?.rootViewController {
                    ad.present(fromRootViewController: root, userDidEarnRewardHandler: {
                            let reward = ad.adReward
                            // reward the user
                           })
                }
                else {
                    print("Error presenting ad")
                }
                
            }
            else{
               print("Ad Not Ready")
           }
        }