【Swift3】と【cocoaPods】と【Firebase】と【adMob】

Swift3でFirebaseのadMobを入れます。

以前に書いた記事、
adMobがFirebaseとやらになっとる! | iPhoneアプリ備忘録
と、
FirebaseのadMobのページを参考にします。
スタートガイド  |  Firebase

まずはcocoaPodsで関連ファイルをインストール

前々回作ったポッドファイルに
source ‘https://github.com/CocoaPods/Specs.git’
pod ‘Firebase/Core’
pod ‘Firebase/AdMob’
を追記。

source 'https://github.com/CocoaPods/Specs.git'

# Uncomment this line to define a global platform for your project
platform :ios, ’8.0’
# Uncomment this line if you're using Swift
# use_frameworks!

target 'FirstSwift' do
pod 'Google/Analytics'
pod 'Firebase/Core'
pod 'Firebase/AdMob'
end

target 'FirstSwiftTests' do

end

target 'FirstSwiftUITests' do

end

保存をしてターミナルを起動。
アプリのディレクトリに移動してから
$ pod update を実行。

あ、一応xcodeを終了しておこう。

$ pod update
Update all pods
Updating local specs repositories

CocoaPods 1.2.0.beta.1 is available.
To update use: `gem install cocoapods --pre`
[!] This is a test version we'd love you to try.

For more information see http://blog.cocoapods.org
and the CHANGELOG for this version http://git.io/BaH8pQ.

Analyzing dependencies
Downloading dependencies
Installing Firebase (3.8.0)
Using FirebaseAnalytics (3.5.1)
Using FirebaseCore (3.4.4)
Using FirebaseInstanceID (1.0.8)
Using Google (3.0.3)
Installing Google-Mobile-Ads-SDK (7.13.0)
Using GoogleAnalytics (3.17.0)
Using GoogleInterchangeUtilities (1.2.2)
Using GoogleSymbolUtilities (1.1.2)
Using GoogleToolboxForMac (2.1.0)
Generating Pods project
Integrating client project
Sending stats
Pod installation complete! There are 3 dependencies from the Podfile and 10
total pods installed.

[!] Your Podfile has had smart quotes sanitised. To avoid issues in the future, you should not use TextEdit for editing it. If you are not using TextEdit, you should turn off smart quotes in your editor of choice.

なんか色々[!]が付いてるけど(゚ε゚)キニシナイ!!(^_^;)

続いて、xcodeの作業に移ります。
「.xcodeproj」ファイルではなく「.xcworkspace」ファイルを開きましょう。

バナーViewを作るところまでは昔の記事を参照。
adMobがFirebaseとやらになっとる! | iPhoneアプリ備忘録

FirebaseのadMobのページを参考にGoogleMobileAdsをインポートし、bannerViewをアウトレット接続します。
スタートガイド | Firebase

import UIKit
import GoogleMobileAds

class ViewController: UIViewController {

  @IBOutlet weak var bannerView: GADBannerView!

続いてバナーを表示するコードを入力します。

override func viewDidLoad() {
  super.viewDidLoad()

  print("Google Mobile Ads SDK version: " + GADRequest.sdkVersion())
  bannerView.adUnitID = "ca-app-pub-3940256099942544/2934735716"
  bannerView.rootViewController = self
  bannerView.loadRequest(GADRequest())
}

と入力した所、.loadRequestが.loadだよとxcodeに直されました。

        //Firebase adMob
        print("Google Mobile Ads SDK version: " + GADRequest.sdkVersion())
        bannerView.adUnitID = "ca-app-pub-3940256099942544/2934735716"
        bannerView.rootViewController = self
        bannerView.load(GADRequest())

これで問題なくテストバナーが表示されました。ヽ(^。^)ノ

続いて広告ユニットを作成します。
参考はこちら
広告ユニットを作成する – AdMob ヘルプ

まずはadMobにログインします。
https://apps.admob.com

ホームに「+新しいアプリを収益化」とあるのでクリック。
%e3%82%b9%e3%82%af%e3%83%aa%e3%83%bc%e3%83%b3%e3%82%b7%e3%83%a7%e3%83%83%e3%83%88-2016-11-02-12-20-01

アプリを選択します。
まだアプリを作っている段階ではappstoreに上げてないと思うのでアプリを手動で追加を選びます。
%e3%82%b9%e3%82%af%e3%83%aa%e3%83%bc%e3%83%b3%e3%82%b7%e3%83%a7%e3%83%83%e3%83%88-2016-11-02-12-27-20
アプリ名は適応にプラットフォームは当然iOS.
で、アプリを追加。

次に広告フォーマットの選択と広告ユニット名の設定。
%e3%82%b9%e3%82%af%e3%83%aa%e3%83%bc%e3%83%b3%e3%82%b7%e3%83%a7%e3%83%83%e3%83%88-2016-11-02-12-28-47
とりあえず、バナーでテキストと画像。更新頻度は30秒にしています。
広告ユニット名は、アプリ名、表示するビュー、場所、などを入れています。
んで保存。

Firebase Analyticsとの連携はよくわからんのでスルー。

作成できると
ca-app-pub-3450075938507904/1623054513
といったユニットIDが作成されるので、それをアプリに組み込みます。

        //Firebase adMob
        print("Google Mobile Ads SDK version: " + GADRequest.sdkVersion())
        bannerView.adUnitID = "ca-app-pub-3450075938507904/1623054513"
        bannerView.rootViewController = self
        bannerView.load(GADRequest())

ちなみに、広告ユニットIDを作成してから実際に広告が配信されるまで時間がかかる場合があるようで、数時間経ってもbannerViewが真っ白なままなんて場合もあります。
広告が表示されない!と悩む前に暫く時間を置いてみましょう。
また、インターネットにつながっていない場合も広告が表示されないので、広告が表示されない場合の画面の状態も考えてみたほうが良いかもしれません。

あと、スマートバナーについては何度か取り上げましたがよくわかりません。(^_^;)
オートレイアウトでbannerViewのサイズが可変でスマートバナーのサイズに即していればスマートバナーとして認識されているようです。

こちらでは
バナー広告  |  Firebase
「スマートバナーを使用するには、以下のように定数 kGADAdSizeSmartBannerPortrait または kGADAdSizeSmartBannerLandscape を使用する必要があります。」って書いてあるんですけどね。

// Use kGADAdSizeSmartBannerLandscape if your app is running in landscape.
let bannerView = GADBannerView(adSize: kGADAdSizeSmartBannerPortrait)

オートレイアウトでsmartBanner対応のビューを作るのはこちらの過去記事を参考にして下さい。
adMobがFirebaseとやらになっとる! | iPhoneアプリ備忘録

2019年3月28日 追記
Swift4で似たような記事書きました。
【Swift4】【Firebase】【AdMob】と【Analytics】(1) SDKを【cocoaPods】でインストール | iPhoneアプリ備忘録

コメントを残す

メールアドレスが公開されることはありません。 * が付いている欄は必須項目です