Admob interstitial不起作用

时间:2017-06-01 10:28:57

标签: swift admob gadinterstitial

我正在处理Admob插页式广告,特别是当我的应用加载ViewController时,我正在尝试显示插页式广告。我使用了官方Admob非页内广告指南提供的代码,但它不起作用:https://developers.google.com/admob/ios/interstitial?hl=it。我也在这里关注了这个视频:https://youtu.be/ahmQQ3OeY0Y?t=787(分钟13.04停止视频)。如果你看代码与指南中的相同。我的目标是在RequestViewController出现时显示广告,以便尝试在viewDidAppear功能中展示广告。无论如何它不起作用,控制台显示此错误:

  

要在此设备上投放测试广告,请致电:request.testDevices =   @ [kGADSimulatorID]

AppDelegate.swift

import UIKit
import UserNotifications
import GoogleMobileAds

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate,GADInterstitialDelegate {

var window: UIWindow?

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
    // Override point for customization after application launch.

        GADMobileAds.configure(withApplicationID: "ca-app-pub-*******")
    }
}

这是我展示广告的ViewController:

RequestviewController.swift

class RequestViewController: UIViewController {

var myInterstitial : GADInterstitial?

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

override func viewDidAppear(_ animated: Bool) {

    //display ad

    myInterstitial = createAndLoadInterstitial()

    if (myInterstitial?.isReady)!{
        print("\n\n\n\n\nReady")
        myInterstitial?.present(fromRootViewController: self)
    }else { print("\n\n\n\nAd wasn't ready") }
}

func createAndLoadInterstitial()->GADInterstitial {
    let interstitial = GADInterstitial(adUnitID: "ca-app-pub-3940256099942544/4411468910")  //test Ad unit id
    interstitial.delegate = self
    interstitial.load(GADRequest())
    return interstitial
}
func interstitialDidReceiveAd(ad: GADInterstitial!) {
    print("interstitialDidReceiveAd")
}

func interstitial(ad: GADInterstitial!, didFailToReceiveAdWithError error: GADRequestError!) {
    print(error.localizedDescription)
}

func interstitialDidDismissScreen(ad: GADInterstitial!) {
    print("\n\n\n\n\n\ninterstitialDidDismissScreen")
    myInterstitial = createAndLoadInterstitial()
}

2 个答案:

答案 0 :(得分:0)

你需要把它写成,

appDelegate.createAndLoadInterstitial()

取代,

appDelegate.myInterstitial?.present(fromRootViewController: self)

答案 1 :(得分:0)

看起来根本问题是RequestViewController的这一部分:

myInterstitial = createAndLoadInterstitial()

if (myInterstitial?.isReady)!{
    print("\n\n\n\n\nReady")
    myInterstitial?.present(fromRootViewController: self)
}else { print("\n\n\n\nAd wasn't ready") }

插页式广告是异步加载的,这意味着在广告加载并准备好显示之前,对load()的调用将会返回。如果您的应用在load()中呼叫createAndLoadInterstitial,然后立即检查isReady,则广告实际上没有时间加载,isReady将始终为假。

处理此问题的一种好方法是在第一个视图控制器中创建插页式广告,然后在过渡到RequestViewController之前将其显示为。这会给广告加载时间,并且在同一过渡期间仍会显示它。

FWIW,ViewControllers之间的转换时间是应用程序流程中使用插页式广告的好地方,在那里做得很好。

另外,该行:

  

要在此设备上投放测试广告,请致电:request.testDevices = @ [kGADSimulatorID]

不是错误。 Android和iOS SDK始终将其打印到日志中,以便发布者知道如何为特定设备或模拟器获取测试广告。如果您在硬件设备而不是iOS模拟器上运行,则会在该行中看到设备的唯一标识符,然后您可以在代码中使用该标识符来获取测试广告。