LoginSignup
15
11

More than 3 years have passed since last update.

iOS AdMobのインタースティシャル広告の実装方法

Last updated at Posted at 2018-03-11

はじめに

こんにちは :leaves:

img
アプリにて全画面表示される、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) {}

参考にさせていただいた記事

見て頂いてありがとうございます。

15
11
1

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
15
11