iOSにAdMobを実装する
もちろん、公式頁 を見るのが早い。でも、英語…。
【旧式の手順】iOS6以前でも利用可能。
1. ADMOBアカウントを取得
もちろん、公式頁を見るのが早い。
2. ADMOB SDKをダウンロード
とりあえず、公式頁を見るのが早い。
もちろん、iOS。
Package の googlemobileadssdkios.zip からダウンロードする。
3. XcodeでADMOBを実装
① Single View App を新規作成。名前はTEST_ADMOB程度で。
② 2.でダウンロードしたGoogleMobileAdsSdkiOS-x.x.xを確認し、
File>Add Files To... でプロジェクトに追加する。copy items if needed にチェック。
③以下のフレームワークを追加する。
④コードを追加する。
************ 2018/9/29 追記 ************
AppDlegateにコードを追加する。
import GoogleMobileAds
と、
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
GADMobileAds.configure(withApplicationID: "ca-app-pub-xxxxxxxxxxxxxxxx~xxxxxxxxxx")
return true
}
*********************************************
ViewControllerにコードを追加する。
- GoogleMobileAds をインポート
- GADBannerViewDelegateを設定
- GADBannerViewをviewに貼り付けてGADRequestを設定
import UIKit
import GoogleMobileAds
class ViewController: UIViewController, GADBannerViewDelegate {
var adHeight: CGFloat = 44.0
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
// AdMob 表示
let bannerView = GADBannerView(adSize:kGADAdSizeBanner)
bannerView.frame.origin = CGPoint(x: 0.0, y: self.view.frame.size.height - bannerView.frame.height)
bannerView.frame.size = CGSize(width: self.view.frame.width, height: bannerView.frame.height)
// AdMobで発行された広告ユニットIDを設定
bannerView.adUnitID = "ca-app-pub-xxxxxxxxxxxxxxx/xxxxxxxxxx"
bannerView.delegate = self
bannerView.rootViewController = self
let gadRequest:GADRequest = GADRequest()
// テスト用の広告を表示する時のみ使用(申請時に削除)、
// テスト機のIDを入力、もしくはkGADSimulatorID
gadRequest.testDevices = ["x...x"]//[kGADSimulatorID]//
bannerView.load(gadRequest)
self.view.addSubview(bannerView)
adHeight = bannerView.frame.height
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
⑤Run
出ました。ADMOB。これはシミュレータなので、
gadRequest.testDevices = [kGADSimulatorID]
としている。