1. AdMobアカウントを作成、アプリID・広告ユニットIDを取得
2. CocoaPodsでSDKをインストール
<最初だけ> CocoaPodsの初期インストール
Terminalを開く
//Rudyのアップデート
> sudo gem update –system
//CocoaPodsのインストール
> sudo gem install -n /usr/local/bin cocoapods
Cocoa Podsのインストールは以上
<以下は各アプリごとに行う> アプリにSDKをダウンロード
Terminalでprojectの .xcodeproj があるレベルに入る
例:> cd Desktop/Xcode/aaaApp
Podfileの作成
> pod init
Finderで確認すると、上記.xcodeproj があるレベルにProfileが作成される。
Profileを以下の内容に書き換える。
ーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーー
#Uncomment the next line to define a global platform for your project
#platform :ios, ‘9.0’
target ‘アプリ名’ do
# Comment the next line if you don’t want to use dynamic frameworks
use_frameworks!
# Pods for アプリ名
pod ‘Google-Mobile-Ads-SDK’
end
ーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーー
再度、Termimalに戻りPod installを行う
> pod install
Pod installation complete! と表示されれば終了
3. Info.plistファイルに必要項目を登録する
Xcodeを開き、Info.plistに
・“GADIsAdManagerApp”の項目を追加する
Key:GADIsAdManagerApp
Type:Boolean
Value:YES
・“GADApplicationIdentifier”の項目を追加する
Key:GADApplicationIdentifier
Type:String
Value:1.で取得したアプリID
4. 以降の作業は、.xcodeproj ではなく.xcworkspaceで作業する
5. AppDelegate.swiftにアプリIDを登録する
AppDelegate.swiftを開き、以下の内容を追記する
ーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーー
import GoogleMobileAds
@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
//アプリID
GADMobileAds.sharedInstance().start(completionHandler: nil)
// Override point for customization after application launch.
return true
}
ーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーー
6.広告を表示したいViewController.swiftにソースコードを追加する
表示したいViewController.swiftを開き、以下の内容を追加する
ーーーーーーーーーーーーーーーーーーーーーーーーーーーーーー
import UIKit
import GoogleMobileAds
class ViewController: UIViewController, GADBannerViewDelegate {
var bannerView: GADBannerView!
func addBannerViewToView(_ bannerView: GADBannerView) {
bannerView.translatesAutoresizingMaskIntoConstraints = false
view.addSubview(bannerView)
if #available(iOS 11.0, *) {
// In iOS 11, we need to constrain the view to the safe area.
positionBannerViewFullWidthAtTopOfSafeArea(bannerView)
}
else {
// In lower iOS versions, safe area is not available so we use
// bottom layout guide and view edges.
positionBannerViewFullWidthAtTopOfView(bannerView)
}
}
// MARK: – view positioning
@available (iOS 11, *)
func positionBannerViewFullWidthAtTopOfSafeArea(_ bannerView: UIView) {
// Position the banner. Stick it to the bottom of the Safe Area.
// Make it constrained to the edges of the safe area.
let guide = view.safeAreaLayoutGuide
NSLayoutConstraint.activate([
guide.leftAnchor.constraint(equalTo: bannerView.leftAnchor),
guide.rightAnchor.constraint(equalTo: bannerView.rightAnchor),
guide.topAnchor.constraint(equalTo: bannerView.topAnchor)
])
}
func positionBannerViewFullWidthAtTopOfView(_ bannerView: UIView) {
view.addConstraint(NSLayoutConstraint(item: bannerView,
attribute: .leading,
relatedBy: .equal,
toItem: view,
attribute: .leading,
multiplier: 1,
constant: 0))
view.addConstraint(NSLayoutConstraint(item: bannerView,
attribute: .trailing,
relatedBy: .equal,
toItem: view,
attribute: .trailing,
multiplier: 1,
constant: 0))
view.addConstraint(NSLayoutConstraint(item: bannerView,
attribute: .bottom,
relatedBy: .equal,
toItem: view.safeAreaLayoutGuide,
attribute: .top,
multiplier: 1,
constant: 0))
}
override func viewDidLoad() {
super.viewDidLoad()
bannerView = GADBannerView(adSize: kGADAdSizeSmartBannerPortrait)
addBannerViewToView(bannerView)
bannerView.adUnitID = “広告ID”
bannerView.rootViewController = self
bannerView.load(GADRequest())
bannerView.delegate = self
}
ーーーーーーーーーーーーーーーーーーーーーーーーーーーーーー
上記は画面上部にバナー広告を表示するソースコードである。
画面下部にバナー広告を表示したい場合は、
“topAnchor”の部分を全て“bottomAnchor”に
“AtTopOf”の部分を全て“AtBottomOf”に
書き換える
7. 実機シュミレータで確認する