[Swift] iOSでのAdMob実装手順まとめ

iOSでのAdMobの広告実装の間隔が空いてしまうと忘れてしまうので、メモ感覚で手順をまとめます。
具体的な実装方法は、AdMobが用意しているサイトにかなり詳しく記述してるあるのですが、全英文でいちいち読むのが面倒なのでさらっとコピペで実装できるくらいにまとめます。
本記事ではバナー広告とリワード広告を扱います。

詳しい実装手順や仕様を知りたい方は公式サイトへどうぞ!
参考 AdMob実装(公式)developers.google.com

MEMO
Swift4に対応しています。

手順

  • STEP.1
    アプリ登録
    AdMob上で広告を掲載するアプリを登録します。サイドメニューの「アプリ」から追加可能。
  • STEP.2
    広告の登録
    AdMob上でアプリ上に出す広告の種類を登録します。STEP.1のアプリを登録後選択し、広告ユニットから追加可能。
  • STEP.3
    ライブラリのインストール
    広告を表示するためのライブラリをCocoaPodsでインストール。
  • STEP.4
    Swiftで実装

CocoaPodsでインストール

おそらくこのページをご覧の方々はCocoaPodsを使ったことがあるかただと思いますが、一応CocoaPods自体のインストールとCocoaPodsを使ったライブラリのインストール方法です。

CocoaPods導入・使い方まとめ

Swiftで実装

使用する広告のIDはすべてAdMobで提供されているテストコードです。
複数広告を設置する場合は、enumでそれぞれの広告IDを管理しておくと便利です。


enum AdMobID : String {
    case appId = "?"
    case adBanner1 = "?"
    case adBanner2 = "?"
    case adVideo1 = "?"
    case bannerViewTest = "ca-app-pub-3940256099942544/2934735716"
    case rewordVideoTest = "ca-app-pub-3940256099942544/1712485313"
}

バナー広告

AppDelegate

import GoogleMobileAds
...
  func application(_ application: UIApplication,
      didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {

    GADMobileAds.configure(withApplicationID: AdMobID.appId.rawValue)
...

ViewController StoryBoardを使う場合

import GoogleMobileAds

class ViewController : UIViewController, GADBannerViewDelegate {

    @IBOutlet weak var adBannerView: GADBannerView!
    
    override func viewDidLoad() {
        super.viewDidLoad()

        adBannerView.adUnitID = AdMobID.bannerViewTest.rawValue
        adBannerView.rootViewController = self
        adBannerView.delegate = self
        adBannerView.load(GADRequest())

    }
}
ViewController StoryBoardを使わない場合

import GoogleMobileAds
...

class ViewController: UIViewController {

  var bannerView: GADBannerView!

  override func viewDidLoad() {
    super.viewDidLoad()
    
    bannerView = GADBannerView(adSize: kGADAdSizeBanner)
    addBannerViewToView(bannerView)

    bannerView.adUnitID = AdMobID.bannerViewTest.rawValue
    bannerView.rootViewController = self
    bannerView.delegate = self
    bannerView.load(GADRequest())

  }

  func addBannerViewToView(_ bannerView: GADBannerView) {
    bannerView.translatesAutoresizingMaskIntoConstraints = false
    view.addSubview(bannerView)
    view.addConstraints(
      [NSLayoutConstraint(item: bannerView,
                          attribute: .bottom,
                          relatedBy: .equal,
                          toItem: bottomLayoutGuide,
                          attribute: .top,
                          multiplier: 1,
                          constant: 0),
       NSLayoutConstraint(item: bannerView,
                          attribute: .centerX,
                          relatedBy: .equal,
                          toItem: view,
                          attribute: .centerX,
                          multiplier: 1,
                          constant: 0)
      ])
   }
}

リワード広告

AppDelegate

import GoogleMobileAds
...
  func application(_ application: UIApplication,
      didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {

    GADMobileAds.configure(withApplicationID: AdMobID.appId.rawValue)
...

ViewController

import GoogleMobileAds
...

class ViewController: UIViewController, GADRewardBasedVideoAdDelegate {

  override func viewDidLoad() {
    super.viewDidLoad()
    
        GADRewardBasedVideoAd.sharedInstance().delegate = self
        GADRewardBasedVideoAd.sharedInstance().load(GADRequest(),
                                                    withAdUnitID: AdMobID.rewordVideoTest.rawValue)

  }

// callRewordAdを呼び出すと動画広告が呼び出される。
  func callRewordAd(){
      if GADRewardBasedVideoAd.sharedInstance().isReady == true {
          GADRewardBasedVideoAd.sharedInstance().present(fromRootViewController: self)
      }
  }

// 動画をすべて見終わった場合に呼び出される。
  func rewardBasedVideoAd(_ rewardBasedVideoAd: GADRewardBasedVideoAd, didRewardUserWith reward: GADAdReward) {
      //reward.amountでAdMob上で設定した報酬量を取得できる。
      print(Int(truncating: reward.amount))
      //...なんらかの達成処理
  }
  
// 動画がすべて見終わる前に終了された場合に呼び出される。
  func rewardBasedVideoAdDidClose(_ rewardBasedVideoAd: GADRewardBasedVideoAd) {
      GADRewardBasedVideoAd.sharedInstance().load(GADRequest(),
                                                  withAdUnitID: AdMobID.rewordVideoTest.rawValue)
  }

}

コメントを残す

このサイトはスパムを低減するために Akismet を使っています。コメントデータの処理方法の詳細はこちらをご覧ください