GADBannerViewDelegate doesn't fire adViewDidReceiveAd in Swift

你。 提交于 2020-01-25 12:05:11

问题


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);
}

回答1:


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.




回答2:


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




回答3:


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.



来源:https://stackoverflow.com/questions/34843019/gadbannerviewdelegate-doesnt-fire-adviewdidreceivead-in-swift

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!