はじめに
こんにちは
アプリにて全画面表示される、AdMobのインタースティシャル広告の実装について書いて見たいと思います。
Xcode9.2 Swift4.0.3
※実際は画面の切り替えタイミングなどで表示することが決まりのようです
導入について
今回はCocoaPodsを利用しました。
pod 'Google-Mobile-Ads-SDK'
インタースティシャル広告のテストユニットIDは以下になります。
ca-app-pub-3940256099942544/4411468910
サンプルコード
MainViewController.swift
import UIKit
import GoogleMobileAds
class MainViewController: UIViewController {
let interstitialADTestUnitID = "ca-app-pub-3940256099942544/4411468910"
fileprivate var interstitial: GADInterstitial!
override func viewDidLoad() {
super.viewDidLoad()
self.interstitial = GADInterstitial(adUnitID: interstitialADTestUnitID)
self.interstitial.delegate = self
self.interstitial.load(GADRequest())
}
@IBAction func buttonAction(_ sender: Any) {
if self.interstitial.isReady {
self.interstitial.present(fromRootViewController: self)
}
}
}
デリゲートメソッド
// GADInterstitialDelegate
self.interstitial.delegate = self
//広告の読み込みが完了した時
func interstitialDidReceiveAd(_ ad: GADInterstitial) {}
//広告の読み込みが失敗した時
func interstitial(_ ad: GADInterstitial, didFailToReceiveAdWithError error: GADRequestError) {}
//広告画面が開いた時
func interstitialWillPresentScreen(_ ad: GADInterstitial) {}
//広告をクリックして開いた画面を閉じる直前
func interstitialWillDismissScreen(_ ad: GADInterstitial) {}
//広告をクリックして開いた画面を閉じる直後
func interstitialDidDismissScreen(_ ad: GADInterstitial) {}
//広告をクリックした時
func interstitialWillLeaveApplication(_ ad: GADInterstitial) {}
参考にさせていただいた記事
見て頂いてありがとうございます。