Search code examples
iosiphoneipadswiftiad

ADInterstitialAd doesn't work on ipad swift


I implemented an ADInterstitialAd and it works fine on iphones but it doesn't work on ipad. Here is my code:

   func close(sender: UIButton) {

    closeButton.removeFromSuperview()
    interAdView.removeFromSuperview()
}

func loadAd() {
    println("load ad")
    interAd = ADInterstitialAd()
    interAd.delegate = self
}

func interstitialAdDidLoad(interstitialAd: ADInterstitialAd!) {
    println("ad did load")

    interAdView = UIView()

    interAdView.frame = CGRect(x: 0, y: 0, width: self.frame.size.width, height: self.frame.size.height)
    self.view?.addSubview(interAdView)

    interAd.presentInView(interAdView)
    UIViewController.prepareInterstitialAds()

    interAdView.addSubview(closeButton)
}

func interstitialAdDidUnload(interstitialAd: ADInterstitialAd!) {
    closeButton.removeFromSuperview()
    interAdView.removeFromSuperview()
}

func interstitialAd(interstitialAd: ADInterstitialAd!, didFailWithError error: NSError!) {
    println("failed to receive")
    println(error.localizedDescription)


    closeButton.removeFromSuperview()
    interAdView.removeFromSuperview()

}

it prints 'ad did load' but on ipad it only shows the closebutton and not the adview and it doesn't print a error description. I imported the iAd framework and included the ADInterstitialAdDelegate in my class.


Solution

  • The presentInView method will return a boolean to tell you if it presented ok. So I would start by checking that.

    If that returns NO then check that the view you are presenting the ad into is actually the size you expect it to be. The ad won't present if the view isn't big enough. You may be presenting the ad too early in the view lifecycle and the view hasn't been sized to fit the screen yet?

    Some other notes:

    You don't need the UIViewController.prepareInterstitialAds() call because that method isn't for people using ADInterstitialAd directly. You prepare Ads just but creating an instance of ADInterstitialAd.

    The ADInterstitialAd object is a single use object so make sure you get a fresh one each time.

    Also UIViewController.prepareInterstitialAds() is part of an simpler api where you don't have to manage ADInterstitialAd yourself. If you don't have any special requirements you may prefer to use that api.