viewDidLoad(AdMob)上的Swift插页式广告

Jam*_*esG 2 admob ios swift

我正在学习本教程

到目前为止,当我点击按钮时,我可以使用它.但是我似乎无法在viewDidLoad上显示插页式广告.

这就是我所拥有的:

override func viewDidLoad() {
  super.viewDidLoad()
  self.interstitialAd = GADInterstitial(adUnitID: "ADUnitID")

  let request = GADRequest()

  // Requests test ads on test devices.
  request.testDevices = ["2077ef9a63d2b398840261c8221a0c9b"]

  self.interstitialAd.loadRequest(request)
  self.interstitialAd = reloadInterstitialAd()
}
Run Code Online (Sandbox Code Playgroud)

控制台一直向我显示:

2016-02-04 22:27:51.855 GoogleAdMobTutorial [3394:56415]要在此设备上获取测试广告,请致电:request.testDevices = @ [kGADSimulatorID];

当我点击按钮时:

@IBAction func showAd(sender: AnyObject) {
    if self.interstitialAd.isReady {
        self.interstitialAd.presentFromRootViewController(self)
    }
}
Run Code Online (Sandbox Code Playgroud)

广告出现了.当我关闭广告时,我会在控制台中看到这个:

2016-02-04 22:29:16.661 GoogleAdMobTutorial [3394:56743]请求错误:由于使用了插页式对象,因此无法发送请求.

这是我关闭广告时调用的函数:

func interstitialDidDismissScreen(ad: GADInterstitial!) {
    self.interstitialAd = reloadInterstitialAd()
}
Run Code Online (Sandbox Code Playgroud)

为了清楚起见:如何在视图控件加载时显示插页式广告?

Gur*_*uru 7

在任何viewController中,添加此函数.

   override func viewDidLoad() {
    super.viewDidLoad()

    let App = UIApplication.sharedApplication().delegate as! AppDelegate
    App.gViewController = self;
    App.showAdmobInterstitial()
}
Run Code Online (Sandbox Code Playgroud)

//在AppDelegate中,将gViewController声明为成员变量

  var gViewController: UIViewController?
  var mInterstitial: GADInterstitial!
Run Code Online (Sandbox Code Playgroud)

在AppDelegate类中添加这些函数

    func showAdmobInterstitial()
    {
            let kGoogleFullScreenAppUnitID = "ca-app-pub-***";
            self.mInterstitial = GADInterstitial.init(adUnitID:kGoogleFullScreenAppUnitID )

            mInterstitial.delegate = self
            let Request  = GADRequest()
            Request.testDevices = ["2077ef9a63d2b398840261c8221a0c9b"]
            mInterstitial.loadRequest(Request)
    }

    func interstitialDidReceiveAd(ad: GADInterstitial!)
    {
        ad.presentFromRootViewController(self.gViewController)
    }
Run Code Online (Sandbox Code Playgroud)

确保在AppDelegate中添加了GADInterstitialDelegate.

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate, GADInterstitialDelegate {
Run Code Online (Sandbox Code Playgroud)