Search code examples
swiftuiviewadmobadstestflight

Showing a view instead of ads whenever ads fail to load


I recently sent my app to beta testing and I put ads (Google AdMob) in it. At first, when my testers used it, they used to see the ads, but when I did it, I didn't see them (on my phone, while on the simulator they work fine).
Now they just see them sometimes (while I still can't see them on my iPhone). I thought it was because I blocked some ads categories, so I checked and I noticed I had to allow the ads I wanted to show, but some testers still couldn't see them (or they could for a while and after a few minutes they would disappear).
So I thought of another solution: I added a view that would only show if the ads weren't loaded, tested on simulator ad everything was nice, sent it to beta and new errors came up: now they could see ads but after a while they would disappear showing the other view, and this is fine. But after some time the ad showed over the new view or they would randomly stop appearing (after they appeared).
So, what I would like to know is:

  1. (Not really important, just curiosity) Why don't I see ads when I test my own app on my iPhone?
  2. (Main question)How can I display another banner-like view when my ads don't load? Or even is there a way to always display them?

My code is the same you can find in the Google AdMob tutorial but I edited to add the other banner-like view:

   //This is to remove the banners when the user purchases the app or to show them if he has the free version
  override func viewWillAppear(_ animated: Bool) {
   if defaults.value(forKey: purchaseKey) == nil {

        bannerView = GADBannerView(adSize: kGADAdSizeSmartBannerPortrait)
        addBannerViewToView(bannerView)
        bannerView.delegate = self
        bannerView.adUnitID = "myID"
        bannerView.rootViewController = self
        bannerView.load(GADRequest())
    } else {
        bannerView.isHidden = true
        removeAdsView.isHidden = true
    }
}


 /// Tells the delegate an ad request loaded an ad.
func adViewDidReceiveAd(_ bannerView: GADBannerView) {
    print("adViewDidReceiveAd")
removeAdsView.isHidden = true
    addBannerViewToView(bannerView)
    bannerView.alpha = 0
    UIView.animate(withDuration: 1, animations: {
        bannerView.alpha = 1
    })
}

/// Tells the delegate an ad request failed.
func adView(_ bannerView: GADBannerView,
            didFailToReceiveAdWithError error: GADRequestError) {
    print("adView:didFailToReceiveAdWithError: \(error.localizedDescription)")
    removeAdsView.isHidden = false
}

Solution

  • Read your log, there must be some error descriptions from Google, in my case I added my device as test

            let request = GADRequest()
            request.testDevices = ["5bb6d1ce5935f98196d35d9e61ac74f6"]
            bannerView?.load(request)
    

    Also you should turn off ad filter on your iPhone, Settings -> Privacy -> Ad