iAd didFailToReceiveAdWithError 有时不工作,显示白框

iAd didFailToReceiveAdWithError not working sometimes, displays white box

我正在使用 Swift 和 SpriteKit,一切都在我的 GameViewController 和 GameScene 中完成。

这是我的 GameViewController 中有关广告的所有代码:

class GameViewController: UIViewController, ADBannerViewDelegate, GADBannerViewDelegate {

var adBannerView: ADBannerView!
var gadBannerView: GADBannerView!
var bannerDisplayed = false
var bannerNow = "iAd"

override func viewDidLoad() {
    super.viewDidLoad()

    if let scene = GameScene.unarchiveFromFile("GameScene") as? GameScene {
        // Configure the view.
        let skView = self.view as! SKView
        skView.showsFPS = false
        skView.showsNodeCount = false

        /* Sprite Kit applies additional optimizations to improve rendering performance */
        skView.ignoresSiblingOrder = true

        /* Set the scale mode to scale to fit the window */
        scene.scaleMode = .AspectFill

        if((UIDevice.currentDevice().userInterfaceIdiom == .Phone) && (UIScreen.mainScreen().bounds.height <= 480)) {
            println("<= iPhone 4S")
            scene.size.height = skView.bounds.size.height * 2
            scene.size.width = skView.bounds.size.width * 2
        }

        NSNotificationCenter.defaultCenter().addObserver(self, selector: "hideADBanner", name: "hideAd", object: nil)
        NSNotificationCenter.defaultCenter().addObserver(self, selector: "showADBanner", name: "showAd", object: nil)

        self.adBannerView = self.loadAds()

        skView.presentScene(scene)

        println(gadBannerView)

    }
}

func loadAds()->ADBannerView{
    var adBannerView2 = ADBannerView(frame: CGRect.zeroRect)
    adBannerView2.center = CGPoint(x: adBannerView2.center.x, y: view!.bounds.size.height - adBannerView2.frame.size.height / 2)
    adBannerView2.delegate = self
    adBannerView2.hidden = true

    bannerNow = "iAd"

    self.view?.addSubview(adBannerView2)
    return adBannerView2
}

func bannerViewActionShouldBegin(banner: ADBannerView!, willLeaveApplication willLeave: Bool) -> Bool {
    println("left for ad")
    return true
}

func bannerViewActionDidFinish(banner: ADBannerView!) {
}

func bannerViewDidLoadAd(banner: ADBannerView!) {
    println("ad did load.")
    println("Delegate: \(adBannerView.delegate)")
    adBannerView.hidden = false
    //gadBannerView?.removeFromSuperview()
}

func bannerView(banner: ADBannerView!, didFailToReceiveAdWithError error: NSError!) {
    //adBannerView.removeFromSuperview()
    //self.gadBannerView = self.createGADBanner()
}

func createGADBanner()->GADBannerView {
    var ad = GADBannerView()
    ad = GADBannerView(frame: CGRectMake(0, 0, self.view.frame.size.width, 50))
    ad.delegate = self
    ad.rootViewController = self
    ad.adUnitID = "xxxxxxxxxxx"

    bannerNow = "GAD"

    var reqAd = GADRequest()
    //reqAd.testDevices = [GAD_SIMULATOR_ID] // If you want test ad's
    ad.loadRequest(reqAd)
    self.view.addSubview(ad)
    println(ad)
    return ad
}

func adViewDidReceiveAd(view: GADBannerView!) {
    println("adViewDidReceiveAd:\(view)");
    bannerDisplayed = true
    relayoutViews()
}

func adView(bannerView: GADBannerView!, didFailToReceiveAdWithError error: GADRequestError!) {
    gadBannerView.removeFromSuperview()
}


func relayoutViews() {
    if (bannerDisplayed) {
        var bannerFrame = gadBannerView!.frame
        bannerFrame.origin.x = 0
        bannerFrame.origin.y = self.view.bounds.size.height - bannerFrame.size.height

        gadBannerView!.frame = bannerFrame
    }
}

func hideADBanner() {
    println(self.gadBannerView)
    println(bannerNow)
    //adBannerView?.hidden = true
    //gadBannerView?.hidden = true
    adBannerView?.removeFromSuperview()
    gadBannerView?.removeFromSuperview()
}

func showADBanner() {
    self.adBannerView = self.loadAds()
    //adBannerView?.hidden = false
    //gadBannerView?.hidden = false
}

所以我的计划是在 iAd 不工作时从 AdMob 获取广告,但问题是我的 iAd "always works",尽管我已经在我的 iPhone。

当 iAd 加载广告时它工作正常,调用函数 bannerViewDidLoadAd() 并显示广告。虽然有时当我加载 iAd 时它说我已经输入了 bannerViewDidLoadAd() 但它只显示一个白色 "box" 并且底部有 iAd 徽标。现在,这不是我想要的,因为 AdMob 永远不会显示。 println 总是发出相同的委托,所以看起来至少有效。

此视图永远不会刷新,因为正如我所说,一切都在 GameScene 中处理,它只是删除和添加节点。

谢谢!

你用 bool 的不必要的函数使事情变得过于复杂,看起来你实际上已经在 Interface Builder 中创建了两个 ADBannerViewsself.adBannerView,一旦以编程方式,var adBannerView: ADBannerView!.

我创建了一个带有注释的示例,以帮助您一路走下去。这里发生的事情是我们正在创建一个 ADBannerView,在我们的 viewDidLoad 中设置一次,然后 hiding/showing 我们的 ADBannerView 取决于它是否从 iAd 网络接收广告或不。我省略了 AdMob 实施,因为它现在应该非常简单。

import UIKit
import iAd // Import iAd

class ViewController: UIViewController, ADBannerViewDelegate { // Include the delegate for our ADBannerView
    var adBannerView = ADBannerView() // Create our ADBannerView
    // Create your GADBannerView here

    override func viewDidLoad() {
        super.viewDidLoad()
        loadAds()
    }

    func loadAds() {
        // Setup our ADBannerView
        adBannerView = ADBannerView(frame: CGRect.zeroRect)
        adBannerView.center = CGPoint(x: view.bounds.size.width / 2, y: view.bounds.size.height - adBannerView.frame.size.height / 2)
        adBannerView.delegate = self
        adBannerView.hidden = true
        view.addSubview(adBannerView)

        // Setup your GADBannerView here in the same manner
    }

    func bannerViewDidLoadAd(banner: ADBannerView!) {
        println("bannerViewDidLoadAd")
        // We received an ad from iAd. Lets show our banner
        adBannerView.hidden = false
        // Hide your GADBannerView here
    }

    func bannerViewActionDidFinish(banner: ADBannerView!) {
        println("bannerViewActionDidFinish")
    }

    func bannerView(banner: ADBannerView!, didFailToReceiveAdWithError error: NSError!) {
        println("didFailToReceiveAdWithError: \(error)")
        // We failed to receive an ad from iAd. Lets hide our banner and print the error
        adBannerView.hidden = true
        // Show your GADBannerView here
    }