问题
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