ios - 为什么我的 AdMob 插页式广告在 Xcode Simulator 中运行良好,但在我的测试设备上却不行?

标签 ios xcode swift admob interstitial

所以我有这个应用程序,它有一个 admob 横幅广告和一个插页式广告。横幅在所有屏幕上运行良好。

插页式广告只应在一个屏幕上加载——当我在 Xcode 模拟器中测试它时它加载正常。但是,当我在我的 iPhone 4S 和 iPod Touch 5th Gen 上进行测试时,插页式广告无法加载,并且出现以下错误:

<Google> Request Error: Received invalid response. interstitialDidFailToReceiveAdWithError: Request Error: Received invalid response.

有没有人以前遇到过这个问题或者可能知道为什么会这样?

这是我正在使用的代码:

   // 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 …

最佳答案

您正试图在插页式广告有机会加载之前展示它。您的 timer 在一秒钟后触发 func presentInterstitial。将此值更改为更大的数字以提供加载间隙时间。

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()
    }

关于ios - 为什么我的 AdMob 插页式广告在 Xcode Simulator 中运行良好,但在我的测试设备上却不行?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32426616/

相关文章:

ios - 半透明的 UINavigationBar

iphone - 存储静态位置和信息 iOS

ios - 变量显示为 nil - swift 4 IOS

ios - 具有分层类别的 UITableView -Swift iOS

iOS 版本 : This build is invalid in iTunes connect

swift - 发送到类的静态函数无法识别的选择器

ios - 如何在 Cordova 中进行临时构建(带有分发证书/配置文件的 iOS 平台)?

ios - Xcode 6 LaunchScreen.xib 错误的 iOS7 iPhone 5 窗口大小

Xcode 7.0 和 7.1,代码覆盖开启,单元测试崩溃 "cannot merge previous GCDA file: corrupt arc tag"

swift - iPad 的 Launch 功能与 iPhone 的不同(在 Swift 3 中)?