iAd didFailToReceiveAdWithError not working someti

2019-08-09 11:33发布

问题:

I'm using Swift and SpriteKit, and everything is made inside my GameViewController and GameScene.

This is all code concerning the ads inside my 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
}

So my plan is to get ads from AdMob when iAd is not working, but the problem is that my iAd "always works", even though I've changed the fill-rate inside the Developer tools in my iPhone.

When iAd loads the ad it works fine, the function bannerViewDidLoadAd() is called and the ad is displayed. Although sometimes when I load iAd it says that I've entered bannerViewDidLoadAd() but it only displays a white "box" with the iAd logo in the bottom. Now, this is not what I want because then AdMob is never gonna show. The println always put out the same delegate so it seems like that works at least.

This view never refreshes because everything, as I said, is handled in the GameScene where it's just removing and adding nodes.

Thank you!

回答1:

You're over complicating things immensely with bool's, unnecessary functions, and it looks like you've actually created two ADBannerViews once in Interface Builder, self.adBannerView, and once programmatically, var adBannerView: ADBannerView!.

I've created an example with comments to help you along the way. Whats happening here is we are creating an ADBannerView, setting it up once in our viewDidLoad, and then hiding/showing our ADBannerView depending on if it receives an ad from the iAd network or not. I've left out the AdMob implementation as it should be pretty straight forward now.

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
    }