LoginSignup
0
0

More than 5 years have passed since last update.

iOSアプリにAdMobで広告を表示する

Posted at

AdMobでアカウント作成は一瞬なので、省きます。

全画面型
スクリーンショット 2018-10-20 13.25.20.png
を、表示させます。


AppDelegateでアプリIDを読み込む

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
    GADMobileAds.configure(withApplicationID: "アプリID")
return true

本番用のIDを使用しないように
それとユニットIDではなく、アプリIDです。


GoogleMobileAdsをimportして
GADInterstitial!型の変数を用意
Delegateも宣言しておきましょう。

GADInterstitial!型の変数を用意っていい方あってます?

import GoogleMobileAds

class IPVideoChatViewController: UIViewController, GADInterstitialDelegate {

    var interstitial: GADInterstitial!

}

ユニットIDで広告を指定して、初期化

func createAndLoadInterstitial() {
        interstitial = GADInterstitial(adUnitID: "ユニットID")
        interstitial.delegate = self
        interstitial.load(GADRequest())
    }

ここも、テスト用IDを使用しましょう。


スクリーンショット 2018-10-20 13.46.34.png
だそうですので、

func interstitialDidDismissScreen(_ ad: GADInterstitial) {
        createAndLoadInterstitial()
    }

で、広告が消される度にinterstitialDidDismissScreen(_ ad: GADInterstitial)を呼んで、初期化しておくと再表示できます。


記事を書いてみて

広告表示の手順を整理するつもりで書きましたが、初学者が見るといまいち分かりづらそうな記事になったかなという発見がありました。
次回は実装手順と注意点、勘所を分けて書こうかと思います。

0
0
0

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
0
0