TEAM LICENSES: Save money and learn new skills through a Hacking with Swift+ team license >>

View won't show when calling by function in SwiftUI

Forums > SwiftUI

Hey there,

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

2      

Hacking with Swift is sponsored by RevenueCat.

SPONSORED Take the pain out of configuring and testing your paywalls. RevenueCat's Paywalls allow you to remotely configure your entire paywall view without any code changes or app updates.

Learn more here

Sponsor Hacking with Swift and reach the world's largest Swift community!

Archived topic

This topic has been closed due to inactivity, so you can't reply. Please create a new topic if you need to.

All interactions here are governed by our code of conduct.

 
Unknown user

You are not logged in

Log in or create account
 

Link copied to your pasteboard.