1

I am using Xcode 7, Swift 2.0. This problem happens both in the simulator, and in my actual app which is available on the App Store. Many times (not always), when I perform a segue in my app the adBanner goes plain white for a bit before loading a new ad. I'm confused because an ad is available, even when it's white!

Here is my code:

I initialize the ADBannerView:

var adBanner = ADBannerView(adType: ADAdType.Banner)

In my viewDidLoad:

self.canDisplayBannerAds = true
self.adBanner.delegate = self
self.adBanner.hidden = true
self.adBanner.alpha = 0
self.adBanner.frame.origin.y = self.view.frame.height-self.adBanner.frame.height
self.view.addSubview(self.adBanner)

My viewDidDisappear:

override func viewDidDisappear(animated: Bool) {

        super.viewDidDisappear(true)

        adBanner.removeFromSuperview()

        if( deviceType.isEqualToString("iPhone") )
        {

            adBanner.delegate = nil

        }

    }

In my class:

    func bannerViewDidLoadAd(banner: ADBannerView!)
    {

        self.adBanner.hidden = false
        UIView.animateWithDuration(0.5, animations: {self.adBanner.alpha = 1})

    }

    func bannerView(banner: ADBannerView!, didFailToReceiveAdWithError error: NSError!)
    {
        self.adBanner.hidden = true
        UIView.animateWithDuration(0.5, animations: {self.adBanner.alpha = 0})

    }

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

        return willLeave

    }

    func bannerViewWillLoadAd(banner: ADBannerView!) {

    }

When running the app with Xcode, I occasionally get this message despite the fact that I have implemented the delegate method:

ADBannerView: Unhandled error (no delegate or delegate does not implement didFailToReceiveAdWithError:): Error Domain=ADErrorDomain Code=5 "The operation couldn’t be completed. Banner view is visible but does not have content" UserInfo=0x9632d30 {ADInternalErrorCode=5, NSLocalizedFailureReason=Banner view is visible but does not have content}

Edit: The problem is using canDisplayBannerAds results in delegate methods not being called. More info here: Hiding iAd ADBannerView in Swift when ad fails to load - no delegate or delegate does not implement didFailToReceiveAdWithError

Community
  • 1
  • 1
yaboi
  • 301
  • 1
  • 7
  • 20

1 Answers1

3

Here is some working code for you that just worked for me. This does not need self.candisplaybannerads = true as I had some issues with that. The ad automatically changes the size according to the screen size and is located at the bottom of the screen. In my spritekit game it did not have any problems with becoming white when transitioning.

import iAd

class viewController: UIViewController, ADBannerViewDelegate {

var AdBanner = ADBannerView()

override func viewDidLoad() {
    super.viewDidLoad()

        /* Ad Banner Settings */

        AdBanner = ADBannerView()
        AdBanner.frame = CGRectZero
        AdBanner.delegate = self
        self.AdBanner.frame = CGRectMake(0, self.view.frame.size.height-self.AdBanner.frame.size.height, self.AdBanner.frame.size.width, self.AdBanner.frame.size.height)
        AdBanner.backgroundColor = UIColor.clearColor()
        self.view.addSubview(AdBanner)

}

/* All iAd Functions */

func bannerViewActionShouldBegin(banner: ADBannerView!, willLeaveApplication willLeave: Bool) -> Bool {
/* whatever you need */
    return true
}

func bannerViewActionDidFinish(banner: ADBannerView!) {
/* whatever you need */
}

func bannerViewDidLoadAd(banner: ADBannerView!) {
    AdBanner.hidden = false
}


func bannerView(banner: ADBannerView!, didFailToReceiveAdWithError error: NSError!) {
    NSLog("Error Loading Ad")
/* whatever you need */
    AdBanner.hidden = true
}
func bannerViewWillLoadAd(banner: ADBannerView!) {
/* whatever you need */
}
Devapploper
  • 6,062
  • 3
  • 20
  • 41
  • 1
    Thank you! It seems the problem is the canDisplayBannerAds line of code, very strange. What I ended up doing was just implementing one adBanner in my appDelegate. More info on the can be found here: http://stackoverflow.com/questions/28514758/swift-iad-more-than-10-instances-of-adbannerview-warning-and-cgaffinetransform – yaboi Oct 21 '15 at 01:56