リワード インタースティシャル広告の実装方法。


admobをいじってたらリワード インタースティシャル(ベータ) と書いてるのを発見し、調べてみたところ、実装方法がほとんど載っていなかったので記事にします。

一応、ここにサンプルがあるものの、Obj-Cなのとなんか動かなかった。

リワードインタースティシャル #とは

リワード インタースティシャルは、アプリの画面が変わる自然なタイミングで自動的に表示される広告に対して報酬(例: コイン、追加ライフ)を提供できる、新しいタイプのインセンティブ広告フォーマットです。
リワード広告とは異なり、ユーザーはリワード インタースティシャルを表示するためにオプトインする必要はありません。
リワード広告のオプトイン プロンプトの代わりに、リワード インタースティシャルでは、報酬について通知し、ユーザーが希望する場合にはオプトアウトできる選択肢を提示する導入画面が必要です。

なるほど。

前提条件

実装

インタースティシャル広告を組み込む主な手順は、以下のとおりです。

  • 広告を読み込む
  • コールバックを登録する
  • 広告を表示してリワード イベントを処理する
ViewController.swift

var rewardedInterstitialAd: GADRewardedInterstitialAd!

override func viewDidload() {
    super.viewDidLoad()

    loadRewordAds()
}


func loadRewordAds() {
    let request: GADRequest = GADRequest()
    let adsView = GADRewardedInterstitialAd.load(
        withAdUnitID: "ca-app-pub-XXXXXXXXX/xxxxx",
        request: request,
        completionHandler: { ad, error in
            if let error = error {
              print("error: \(error.localizedDescription)")
              return
            }else {
                rewardedInterstitialAd = ad
            }
         }
    )
}

func callRewordAds() {
    rewardedInterstitialAd?.present(fromRootViewController: self, userDidEarnRewardHandler: {
        // ユーザー報酬の処理
        print(rewardedInterstitialAd.adReward.amount) // 報酬額(admobで設定したやつ)
    })

}

あとは好きなタイミングでcallRewordAdsを呼ぶだけです。

ただ2回目以降は呼び出されませんでした。多分GADRewardedInterstitialAdは使い捨てオブジェクトなんでしょう。

fullScreenContentDelegateを使って終わったら再度読み込むようにしてみます。

func loadRewordAds() {
    let request: GADRequest = GADRequest()
    let adsView = GADRewardedInterstitialAd.load(
        withAdUnitID: "ca-app-pub-XXXXXXXXX/xxxxx",
        request: request,
        completionHandler: { [weak self] ad, error in
            guard let self = self else { return }
            if let error = error {
              print("error: \(error.localizedDescription)")
              return
            } else {
                rewardedInterstitialAd = ad
                rewardedInterstitialAd.fullScreenContentDelegate = self // Add
            }
         }
    )
}
extension ViewController: GADFullScreenContentDelegate {
    /// Tells the delegate that the ad failed to present full screen content.
    func ad(_ ad: GADFullScreenPresentingAd, didFailToPresentFullScreenContentWithError error: Error) {
      print("Ad did fail to present full screen content.")
    }

    /// Tells the delegate that the ad presented full screen content.
    func adDidPresentFullScreenContent(_ ad: GADFullScreenPresentingAd) {
      print("Ad did present full screen content.")
    }

    /// Tells the delegate that the ad dismissed full screen content.
    func adDidDismissFullScreenContent(_ ad: GADFullScreenPresentingAd) {
      print("Ad did dismiss full screen content.")
      // ここで再読み込み
        loadRewordAds()
    }
}

さいごに

実装方法が全く乗ってなかったのですが最近でたやつなのかな?