Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting Error "No Ad To Show" When Implementing AdMob into Swift App

I am trying to do the (seemingly) simple task of integrating Native AdMob ads into my iOS app running on Swift. Let me first show you my storyboards and code for integration and then we'll move on to things I've tried to fix it.

Set-Up: Storyboards and Code

In my App Delegate, I configure Firebase a Google Mobile Ads...

import Firebase

class AppDelegate:  UIResponder, UIApplicationDelegate {

    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
            // Override point for customization after application launch.
            FirebaseApp.configure()
            GADMobileAds.configure(withApplicationID: "ca-app-pub-8482280186158418~7106629637")
            GADMobileAds.sharedInstance().start(completionHandler: nil)
    }
}

I have also added my GADApplicationIdentifier to my Info.plist. In my storyboard, I have a ViewController that contains a UICollectionViewCell that contains a GADUnifiedNativeAdView and the native elements are linked through outlets.

Screenshot of Storyboard

Over in my ViewController, in the viewDidLoad, I load 5 native ads.

override func viewDidLoad() {
        super.viewDidLoad()
        // Load Ads
        let adUnitID = "ca-app-pub-{adUnitId}"
        let numAdsToLoad = 5
        let options = GADMultipleAdsAdLoaderOptions()
        options.numberOfAds = numAdsToLoad
        
        let imageOptions = GADNativeAdImageAdLoaderOptions()
        imageOptions.disableImageLoading = false

        let mediaOptions = GADNativeAdMediaAdLoaderOptions()
        mediaOptions.mediaAspectRatio = .square
        
        let adOptions = GADNativeAdViewAdOptions()
        adOptions.preferredAdChoicesPosition = .topLeftCorner
        
        adLoader = GADAdLoader(adUnitID: adUnitID, rootViewController: self, adTypes: [.unifiedNative], options: [options, imageOptions, mediaOptions])
        adLoader.delegate = self
        adLoader.load(GADRequest())
}

Over in the GADUnifiedNativeAdLoaderDelegate, I receive the ad and ad them to an array.

    var nativeAds = [GADUnifiedNativeAd]()

    extension ViewController: GADUnifiedNativeAdLoaderDelegate {
            
        
        func adLoader(_ adLoader: GADAdLoader,
                      didFailToReceiveAdWithError error: GADRequestError) {
          print("NATIVE AD - didFailToReceiveAdWithError: \(error)")
    
        }
    
        func adLoader(_ adLoader: GADAdLoader, didReceive nativeAd: GADUnifiedNativeAd) {
          print("NATIVE AD - didReceive: \(nativeAd)")
    
          nativeAds.append(nativeAd)
        }
        
        func adLoaderDidFinishLoading(_ adLoader: GADAdLoader) {
          print("NATIVE AD - adLoaderDidFinishLoading")
            libraryCollection.reloadData()
        }
    }

In my UICollectionView's delegate, in cellForItemAt I set the cell's class

func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {

     if isAdCell {
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "adCell", for: indexPath) as! AdCollectionViewCell
        if nativeAds.count == 5 {
        cell.nativeAd = nativeAds[0]
        cell.nativeAd.rootViewController = self
                    
        }
        cell.setAdData()
        return cell
}

Finally, in my UICollectionViewCell's class, I set the ad data

import Firebase

class AdCollectionViewCell: UICollectionViewCell {

     @IBOutlet var adView: GADUnifiedNativeAdView
     var nativeAd = GADUnifiedNativeAd()

     func setAdData() {
          adView.nativeAd = nativeAd
          (adView.headlineView as! UILabel).text = nativeAd.headline
          adView.callToActionView?.isUserInteractionEnabled = false
          (adView.callToActionView as! UILabel).text = nativeAd.callToAction
          adView.mediaView?.mediaContent = nativeAd.mediaContent
          adView.mediaView?.contentMode = .scaleAspectFill
        
    }

}

The Error

Now, whenever I use the test native ad unit id provided by Google AdMob, ca-app-pub-3940256099942544/3986624511, everything works just fine! I get no errors and the system prints

NATIVE AD - didReceive: <GADUnifiedNativeAd: 0x283bbdc00>
NATIVE AD - didReceive: <GADUnifiedNativeAd: 0x283bbd7a0>
NATIVE AD - didReceive: <GADUnifiedNativeAd: 0x283bc73a0>
NATIVE AD - didReceive: <GADUnifiedNativeAd: 0x283ba9880>
NATIVE AD - didReceive: <GADUnifiedNativeAd: 0x283ba9810>
NATIVE AD - adLoaderDidFinishLoading

But the problem comes when I try to use my own native ad unit id. Upon loading, the system prints the error

NATIVE AD - didFailToReceiveAdWithError: Error Domain=com.google.admob Code=1 "Request Error: No ad to show." UserInfo={NSLocalizedDescription=Request Error: No ad to show., gad_response_info=  ** Response Info **
    Response ID: ymbzXou2CcrohQb16bzgBA
    Network: (null)

  ** Mediation line items **
    Entry (1)
    Network: GADMAdapterGoogleAdMobAds
    Credentials:
{
}
    Error: Error Domain=com.google.admob Code=1 "Request Error: No ad to show." UserInfo={NSLocalizedDescription=Request Error: No ad to show.}
    Latency: 0.095
}

for each ad that I load. And I have tried MANY things to fix this that I have found all over the internet...

Troubleshooting

As I mentioned, I have scoured the internet for help and troubleshooting options. Here is what I have done.

  • Created a new native ad unit id
  • Waited over a week to confirm the error with the unit id
  • Checked my AdMob and AdSense account information
  • Checked my AdMob and AdSense payment information
  • Made sure the GADApplicationIdentifier in the Info.plist is correct
  • Added all of the requirements to the App Transport Security Settings in the Info.plist as laid out by AdMob. (Allow Arbitrary Loads, Allows Arbitrary Loads for Media, Allow Arbitrary Loads in Web Content)
  • Double, triple, quadruple, quintuple checked the code and followed the documentation provided by AdMob
  • Followed the CodeLab provided by AdMob
like image 388
Jacob Cavin Avatar asked Jun 24 '20 15:06

Jacob Cavin


People also ask

Why are my ads not showing up on AdMob?

Test your implementation code to check that ads can show. If your ad implementation code is not correct, ads won’t show. Have you reviewed your error codes? Learn more about the most common error codes for Android and iOS . Review the AdMob community post on error codes for more troubleshooting tips.

What happens if my account is not approved by AdMob?

You’ll be notified via email if your account is not approved by AdMob. Is your app or ad unit new? Wait up to one hour after you create an app or ad unit. In some cases, it can take a few days for ads to appear in new apps or ad units.

Why won’t my ads show up on Google Ads?

When an ad unit in an app sends an ad request to Google’s servers, the server either responds with an ad or with an error code to indicate the issue. Have you implemented mediation correctly? Review the instructions in the Google Developers mediation guide ( Android, iOS, Unity ). If not, ads won’t show.

How do I test my AdMob mediation setup?

Verify your mediation setup with the Mediation Test Suite. It’ll test whether you’ve correctly configured your app and ad units to display ads from third-party networks via AdMob mediation. Each ad network has its own integration instructions ( Android, iOS, Unity ).


1 Answers

I had the same problem with error-code 1. Also after i had published my app in appstore. The test-ads worked fine. After i could linked my app with the app store (8 days after publishing) it worked.

like image 72
Steirersoft Avatar answered Nov 15 '22 11:11

Steirersoft