0

I made an app in Swift with SwiftUI framework and SwiftUI App life cycle.

I use that class to manage the interstitial ad:

class Interstitial : NSObject, GADFullScreenContentDelegate, UIApplicationDelegate{
    var interstitial : GADInterstitialAd?
    
    let adUnitID: String = "ca-app-pub-3940256099942544/4411468910"
    
    override init() {
        super.init()
        loadInterstitial()
    }
    
    func loadInterstitial(){
        let request = GADRequest()
        GADInterstitialAd.load(withAdUnitID: adUnitID, request: request) { [self] (ad, err) in
            if(err !=  nil){
                print(String(describing:err?.localizedDescription))
            }
            else {
                interstitial = ad
                interstitial?.fullScreenContentDelegate = self
            }
        }
    }
    
    func showAd(){
        let root = UIApplication.shared.windows.first?.rootViewController
        if interstitial != nil {
            interstitial!.present(fromRootViewController: root!)
        }
        else {
            nc.post(name: Notification.Name("UserClosedAd"), object: nil)
        }
    }
    
    func adDidDismissFullScreenContent(_ ad: GADFullScreenPresentingAd) {
        print("ad closed")
        nc.post(name: Notification.Name("UserClosedAd"), object: nil)
        return
    }
    func ad(_ ad: GADFullScreenPresentingAd, didFailToPresentFullScreenContentWithError error: Error) {
        print(error.localizedDescription)
        print("ad failed to be presented")
        nc.post(name: Notification.Name("UserClosedAd"), object: nil)
        return
    }
    func adDidPresentFullScreenContent(_ ad: GADFullScreenPresentingAd) {
        print("ad presented")
        return
    }
}

and I show the ad in this way:

let pub = NotificationCenter.default
        .publisher(for: NSNotification.Name("UserClosedAd"))
var interstitial : Interstitial = Interstitial()

var body: some View {
    Button(action:
            {
                self.interstitial.showAd()
            }) {
        Image(systemName: "text.bubble")
            .foregroundColor(.red)
        Text("Show ad")
    }
    .font(.system(size: 30, weight: .semibold, design: .rounded))
    .padding(30)
    .onAppear(perform: {
        interstitial.loadInterstitial()
    })
    .onReceive(pub, perform: { _ in
        newView = true
        print("Ad closed")
    })
}

The problem is that sometime the callback work, but other not. When I use the app on a real device, or simulator, that is not connected to Xcode, the callback never works.

What could be the problem?

Thanks in advance, Onnwen

1 Answer 1

0

try following:

struct TestView: View {
    @ObservedObject var interstitial: FoolGADInterstitial = FoolGADInterstitial(adUnitId: "xxxx")

    var body: some View {
        Text("123")
            .onAppear(perform: onAppear)
    }
    
    func onAppear() {
        #if PLUSVERSION
        #else
        guard manager.shouldShowInterstitialAd else { return }
        interstitial.showAd()
        #endif
    }
}

class FoolGADInterstitial: NSObject, GADFullScreenContentDelegate, ObservableObject {
    var adUnitId: String = ""
    var interstitial : GADInterstitialAd? = nil
    
    init(adUnitId: String) {
        super.init()
        self.adUnitId = adUnitId
        loadInterstitial()
    }
    
    func loadInterstitial() {
        GADInterstitialAd.load(withAdUnitID: adUnitId, request: GADRequest()) { [weak self] (ad, err) in
            if let err = err {
                foolPrint("load error, \(err.localizedDescription)")
                return
            }
            guard let self = self else { return }
            self.interstitial = ad
            self.interstitial?.fullScreenContentDelegate = self
        }
    }

    func showAd() {
        guard let ad = self.interstitial, let root = UIApplication.shared.windows.last?.rootViewController else { return }
        foolPrint("showAd done.")
        ad.present(fromRootViewController: root)
    }
    
    func adDidDismissFullScreenContent(_ ad: GADFullScreenPresentingAd) {
        foolPrint("Ad dismissed")
        loadInterstitial()
    }
    
    func ad(_ ad: GADFullScreenPresentingAd, didFailToPresentFullScreenContentWithError error: Error) {
        foolPrint("present failed, \(error.localizedDescription)")
    }
}

and FYI: https://github.com/googleads/googleads-mobile-ios-examples/blob/master/Swift/admob/InterstitialExample/InterstitialExample/ViewController.swift

2
  • Thanks you very much, that's works, but only when I click the button for the second time. At the first time callbacks never work... do you know what that might be?
    – Onnwen
    Commented Apr 7, 2021 at 10:09
  • Because ads will take time for downloading from admob. you can retry if self.interstitial = nil after 0.2s.
    – foolbear
    Commented Apr 7, 2021 at 10:17

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.