Yasin Uğur Yıldız
Yasin Uğur Yıldız

Reputation: 199

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

Upvotes: 1

Views: 1898

Answers (5)

Chen Lim
Chen Lim

Reputation: 758

In case someone else runs into this, the delegate signatures changed (somewhere between v7 and v11). It's now - (void)bannerViewDidReceiveAd:(nonnull GADBannerView *)bannerView;

Upvotes: 0

sadat
sadat

Reputation: 4352

In my case.. change from

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

to

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

Upvotes: 4

Hairsh Kuramsetty
Hairsh Kuramsetty

Reputation: 101

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.

Upvotes: 0

Burcu Kutluay
Burcu Kutluay

Reputation: 610

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.

Upvotes: 1

Tony
Tony

Reputation: 1611

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

Upvotes: 0

Related Questions