I've used the same code on all my apps to display iAd ads, but after downloading xcode 7 and converting my code to swift2, ads don't appear anymore on my device or on the xcode simulator. Does anyone know what changes need to be made?
import UIKit
import SpriteKit
import iAd
class GameViewController: UIViewController, ADBannerViewDelegate {
var adBanner: ADBannerView? = ADBannerView()
override func viewDidLoad() {
super.viewDidLoad()
adBanner?.delegate = self
adBanner?.hidden = true
self.canDisplayBannerAds = true
}
func bannerView(banner: ADBannerView!, didFailToReceiveAdWithError error: NSError!) {
adBanner?.hidden = true
}
func bannerViewDidLoadAd(banner: ADBannerView!) {
adBanner?.hidden = false
}
func bannerViewActionShouldBegin(banner: ADBannerView!, willLeaveApplication willLeave: Bool) -> Bool {
return willLeave
}
}
You are mixing up two approaches. There are two ways of displaying iAd banners. First one, just let iAd know that you want do show banners on your view controller and let iAd do the rest. This is done by calling the following method:
self.canDisplayBannerAds = true
Second approach is to load the banner first and then add it manually to your view. To do that, you first initialise the banner with the desired type and set its delegate:
var adBanner: ADBannerView? = ADBannerView(adType: ADAdType.Banner)
adBanner?.delegate = self
Then, when the banner is loaded, we have to add it to a view, for example:
func bannerViewDidLoadAd(banner: ADBannerView!) {
self.view.addSubview(banner)
}
There is no need to change the hidden
property before the banner i added to superview.
Have you considered iAd network to be down at the very moment? Always keep a copy of Apples own iAd Suite at hand: https://developer.apple.com/library/ios/samplecode/iAdSuite_Storyboard/Introduction/Intro.html
If their own dont't load, yours won't load either.
Right now I'm getting this self explanatory error message:
NSLocalizedFailureReason=Ad inventory unavailable
Hope that helps.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.