Why is my admob interstitial working fine in xcode simulator but not on my test devices (iphone 4s & ipod 5th gen)?

我是研究僧i 提交于 2019-12-02 00:16:09

问题


So I have this app that has both an admob banner ad and an interstitial ad. The banner runs fine on all screens. The interstitial ad is only supposed to load on one screen -- and it loads fine when I test it in the xcode simulator. But when I test on my iPhone and iPod, the interestital doesn't load and I get this error: Request Error: Received invalid response. interstitialDidFailToReceiveAdWithError: Request Error: Received invalid response. Anyone ever run into this before or might know why this is happening? Here's the code I'm working with:

   // interstitial ad variable...
var interstitial: GADInterstitial?
var timer:NSTimer?
var loadRequestAllowed = true
let screen = UIScreen.mainScreen().bounds
let IS_IPAD = UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiom.Pad
var buttonHeight:CGFloat = 0.0
var buttonOffsetY:CGFloat = 0.0
let statusbarHeight:CGFloat = 20.0
var iAdSupported = false


       // load the interstitial
    //Admob Interstitial
    buttonHeight = IS_IPAD ? 30 : 20
    buttonOffsetY = statusbarHeight
    if !iAdSupported {
        interstitial = createAndLoadInterstitial()
    } // closes if !iAdSupported



//        presentInterstitial()
    timer = NSTimer.scheduledTimerWithTimeInterval(1, target:self, selector: Selector("presentInterstitial"), userInfo: nil, repeats: false)
    println("the timer made presentInterstitial run")




 //Interstitial func
func createAndLoadInterstitial()->GADInterstitial {
    println("createAndLoadInterstitial")
    var interstitial = GADInterstitial(adUnitID: "ca-app-pub-4471013071748494/2225255714")

    interstitial.delegate = self
    interstitial.loadRequest(GADRequest())

    return interstitial
} // closes createAndLoadInterstitial …

func presentInterstitial() {
    if let isReady = interstitial?.isReady {
        interstitial?.presentFromRootViewController(self)
        println("presentInterstitial function ran")
    } // closes if let isReady
} // closes presentInterstitial …



//Interstitial delegate
func interstitial(ad: GADInterstitial!, didFailToReceiveAdWithError error: GADRequestError!) {
    println("interstitialDidFailToReceiveAdWithError:\(error.localizedDescription)")
    //  ResultsBtn?.enabled = false
    interstitial = createAndLoadInterstitial()
} // closes interstitial …

回答1:


You are attempting to present your interstitial before it has a chance to load. Your timer fires func presentInterstitial after one second. Change this to a larger number to give the interstitial time to load.

import UIKit
import GoogleMobileAds

class ViewController: UIViewController, GADInterstitialDelegate {

    var admobInterstitial : GADInterstitial?
    var timer : NSTimer?

    override func viewDidLoad() {
        super.viewDidLoad()

        admobInterstitial = createAndLoadInterstitial()
        timer = NSTimer.scheduledTimerWithTimeInterval(5.0, target:self, selector: Selector("presentInterstitial"), userInfo: nil, repeats: false)
    }

    func createAndLoadInterstitial()->GADInterstitial {
        var interstitial = GADInterstitial(adUnitID: "yourAdMobADUnitID")
        interstitial.delegate = self
        interstitial.loadRequest(GADRequest())
        return interstitial
    }

    func presentInterstitial() {
        if let isReady = admobInterstitial?.isReady {
            admobInterstitial?.presentFromRootViewController(self)
        }
    }

    func interstitial(ad: GADInterstitial!, didFailToReceiveAdWithError error: GADRequestError!) {
        println("interstitialDidFailToReceiveAdWithError:\(error.localizedDescription)")
        admobInterstitial = createAndLoadInterstitial()
    }

On a side note, you should not give a local variable the same name as a global variable.



来源:https://stackoverflow.com/questions/32426616/why-is-my-admob-interstitial-working-fine-in-xcode-simulator-but-not-on-my-test

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!