iAd横幅在segue

时间:2015-10-16 23:07:30

标签: ios iphone xcode swift iad

我正在使用Xcode 7,Swift 2.0。这个问题在模拟器和我在App Store上可用的实际应用程序中都会发生。很多次(并非总是),当我在我的应用中执行segue时,adBanner会在加载新广告之前略微变白。我很困惑,因为广告可用,即使它是白色的!

这是我的代码:

我初始化ADBannerView:

var adBanner = ADBannerView(adType: ADAdType.Banner)

在我的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)

我的viewDidDisappear:

override func viewDidDisappear(animated: Bool) {

        super.viewDidDisappear(true)

        adBanner.removeFromSuperview()

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

            adBanner.delegate = nil

        }

    }

在我班上:

    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!) {

    }

当使用Xcode运行应用程序时,我偶尔会收到此消息,尽管我已经实现了委托方法:

ADBannerView:未处理的错误(没有委托或委托没有实现didFailToReceiveAdWithError :):错误域= ADErrorDomain代码= 5"操作无法完成。横幅视图可见,但没有内容" UserInfo = 0x9632d30 {ADInternalErrorCode = 5,NSLocalizedFailureReason =横幅视图可见,但没有内容}

编辑:问题是使用canDisplayBannerAds结果未调用委托方法。更多信息:Hiding iAd ADBannerView in Swift when ad fails to load - no delegate or delegate does not implement didFailToReceiveAdWithError

1 个答案:

答案 0 :(得分:3)

以下是一些适用于我的工作代码。这不需要self.candisplaybannerads = true,因为我遇到了一些问题。广告会根据屏幕尺寸自动更改尺寸,并位于屏幕底部。在我的spritekit游戏中,转换时变成白色并没有任何问题。

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 */
}