swiftadmobbanner-adsadbannerviewgadbannerview

Banner Google AdMob is Not Working in Swift


I am using Google AdMob banner in my app. I created a dummy project and implemented the banner functions there and it is working fine there but when I used it in my actual application, it is giving me error. The error is:

bannerView: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: aIdaYsjKHfCJwuIP4KOQoAc

Network: (null)

** Mediation line items **

My code is:

import UIKit
import GoogleMobileAds

class DashBoardVC: UIViewController, GADBannerViewDelegate {

 var bannerView: GADBannerView!

 override func viewDidLoad() {
        super.viewDidLoad()
        
      bannerView = GADBannerView(adSize: GADAdSizeBanner)
      bannerView.adUnitID = "ca-app-pub-3940256099942544/2934735716"
      bannerView.rootViewController = self
      bannerView.load(GADRequest())
      bannerView.delegate = self
   }

   func bannerViewDidReceiveAd(_ bannerView: GADBannerView) {
      
      addBannerViewToView(bannerView)
    }
    
    func bannerView(_ bannerView: GADBannerView, didFailToReceiveAdWithError error: Error) {

      print("bannerView:didFailToReceiveAdWithError: \(error)")
    }

    func addBannerViewToView(_ bannerView: GADBannerView) {
        bannerView.translatesAutoresizingMaskIntoConstraints = false
        view.addSubview(bannerView)
        
        view.addConstraints(
          [NSLayoutConstraint(item: bannerView,
                              attribute: .bottom,
                              relatedBy: .equal,
                              toItem: bottomLayoutGuide,
                              attribute: .top,
                              multiplier: 1,
                              constant: 0),
           NSLayoutConstraint(item: bannerView,
                              attribute: .centerX,
                              relatedBy: .equal,
                              toItem: view,
                              attribute: .centerX,
                              multiplier: 1,
                              constant: 0)
          ])
       }
}

I had searched for the solutions on stack overflow and there they are saying that sometimes it takes 1 to 2 hours if you create a new banner adUnitID but now its been 3 days and still getting this error.

I am not understanding why is it doing so in actual app but on dummy project, it's been working fine.

Does anyone knows why is it giving error?


Solution

  • I got the answer. The reason it was doing so is I was testing it on simulator and not the actual device. Today, I tested it on IPhone 12 pro max, the ads were showing there. I think there is some issue with simulator that it doesn't load the ads.