公開日:
iOSアプリにGoogleAdMobバナー表示テストまで行ったこと、行き詰まった箇所などを備忘録としてまとめています。
StoryBoard上でUIViewを設置(iOSバナー用320×50)
ViewControllerにつなぎます。
@IBOutlet var bannerView: GADBannerView!
注意点(行き詰まった箇所で説明)
https://developers.google.com/admob/ios/test-ads?hl=ja
のIDを記述する箇所は後述の
info.plist
ViewController.swift
の二箇所です
公式HPのコードをコピペする。
https://developers.google.com/admob/ios/quick-start?hl=ja#update_your_infoplist
<key>GADApplicationIdentifier</key> <string>
ca-app-pub-3940256099942544/2934735716</string>
import GoogleMobileAds ~~~~~~~~ //GoogleMobileAdsを使用可能にする GADMobileAds.sharedInstance().start(completionHandler: nil)
bannerを表示するViewControllerに以下を記述
import GoogleMobileAds 〜〜〜〜〜〜〜〜〜〜〜〜〜〜〜 override func viewDidLoad() { super.viewDidLoad() ... bannerView.adUnitID = "ca-app-pub-3940256099942544/2934735716" bannerView.rootViewController = self bannerView.load(GADRequest()) }
必須ではなさそうなので保留にしました。
https://developers.google.com/admob/ios/banner?hl=ja#ad_events
イベントを実装する際に以下が必要になりそうです。
import GoogleMobileAds import UIKit class ViewController: UIViewController, GADBannerViewDelegate { var bannerView: GADBannerView! override func viewDidLoad() { super.viewDidLoad() ... bannerView.delegate = self } }
テスト表示は2回目なのですが、これはうっかりしていました。
エラー内容から推測することもできず、半日行き詰まってしまった箇所です。
*** Terminating app due to uncaught exception 'GADInvalidInitializationException', reason: 'The Google Mobile Ads SDK was initialized without an application ID. Google AdMob publishers, follow instructions here: https://googlemobileadssdk.page.link/admob-ios-update-plist to set GADApplicationIdentifier with a valid App ID. Google Ad Manager publishers, follow instructions here: https://googlemobileadssdk.page.link/ad-manager-ios-update-plist'
エラー内容に従って
https://googlemobileadssdk.page.link/ad-manager-ios-update-plist
からコードをinfo.plistにコピペしました。
最終的にinfo.plistは上のようになりました。(GADApplicationIdenfierはテストID)
以上でGoogleAdMobのテスト表示ができました。