简体   繁体   中英

GADBannerViewDelegate doesn't fire adViewDidReceiveAd in Swift

I use GADBannerView in another class that named Tools. But adViewDidReceiveAd delegate method doesn't fire in Tools class. What can I do?

class Tools: NSObject, GADBannerViewDelegate {

    var viewController: UIViewController!

    func showAds(viewController: UIViewController) -> Void {

        self.viewController = viewController

        let request: GADRequest = GADRequest()
        request.testDevices = [kGADSimulatorID]

        let bannerView = GADBannerView(adSize: kGADAdSizeSmartBannerPortrait)
        bannerView.delegate = self
        bannerView.rootViewController = viewController
        bannerView.adUnitID = "ca-app-pub-xxx-xxx"
        bannerView.loadRequest(request)

    }

    func adViewDidReceiveAd(bannerView: GADBannerView!) {
        print("adViewDidReceiveAd");
        viewController.view.addSubview(bannerView)
    }
}

And, I called Tools in ViewController

override func viewDidLoad() {
    super.viewDidLoad()        
    Tools().showAds(self);
}

I had the same problem then I replaced the

private func adViewDidReceiveAd(_ bannerView: DFPBannerView) {}

with

func adViewDidReceiveAd(_ bannerView: GADBannerView) {}

then problem is solved. I hope it helps you, too.

Your code has a bug. After creating bannerView you did not add it into a viewcontroller. Thus it will be deallocated.

Actually, I faced the same issue and I checked that the failed delegate is calling in mine case. Kindly check the error and If you face the same and the error is 'No ad to display' then make sure that your app is uploaded to APP STORE and same has been verified by the Admob by linking it.

 func adView(_ bannerView: GADBannerView,
        didFailToReceiveAdWithError error: GADRequestError) {
    }

Once it has been done your adViewDidReceiveAd delegate will call.

In my case.. change from

func adViewDidReceiveAd(_ bannerView: GADBannerView) {
  print("adViewDidReceiveAd")
}

to

func bannerViewDidReceiveAd(_ bannerView: GADBannerView) {
  print(#function)
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM