Search code examples
iosswiftiad

iAd in my app doesn't work


I added iAd banner in my app, and the code is like this:

@IBOutlet var adBannerview: ADBannerView?

and codes that in viewDidLoad:

    self.canDisplayBannerAds = true
    self.adBannerview?.delegate = self
    self.adBannerview?.hidden = true

then:

 override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
    func bannerViewWillLoadAd(banner: ADBannerView!) {


    }
   func bannerViewDidLoadAd(banner: ADBannerView!) {
    self.adBannerview?.hidden = false


    }
   func bannerViewActionDidFinish(banner: ADBannerView!) {


    }

    func bannerViewActionShouldBegin(banner: ADBannerView!, willLeaveApplication: Bool) -> Bool {

        return true

    }

   func bannerView(banner: ADBannerView!, didFailToReceiveAdWithError: NSError!) {
    self.adBannerview?.hidden = true


    }



}

Now, my app is sale on app store, I downloaded that, but when I open it, found there's no ads on the bottom, is that something wrong with my code? But when I run the app on simulator, it says "Connected to iAd". Appreciate if you help!


Solution

  • You're actually attempting to implement two iAd ADBannerView's here. You can use either the self.canDisplayBannerAds = true, which you have in your viewDidLoad function, and remove your other iAd code (@IBOutlet var adBannerview: ADBannerView?, self.adBannerview?.delegate = self, self.adBannerview?.hidden = true). self.canDisplayBannerAds = true will create an iAd banner for you and manage itself whether it receives an ad from iAd or not. This is the no hassle way of implementing iAd banners into your application but doesn't allow for much control.

    Alternatively, you could remove self.canDisplayBannerAds = true and implement the iAd ADBannerView yourself. This allows for more control but does require more than just one line of code to get it up and running, but it is still very simple to implement. You can check my answer here for some guidance if you choose to go this route.

    Also, you have all of your ADBannerView's delegate methods inside your view controllers didReceiveMemoryWarning function. They need to be placed outside of this and any other function.