iPhoneアプリにGoogle AdSense, AdMob, Firebaseを使って広告を出す方法まとめ
色々試行錯誤してなんとか広告を表示できたのですが、思ったこと。
なんかWebサービスいっぱいあるし、
なんちゃらIDみたいのいっぱい出てくるし、混乱するよおお!!!
ということで、とりあえず関連図を一枚の画像にしてみました(コーディングまですべて書いてあるよ!)
まずは見易さ度外視でとりあえず全登場人物とその関係を書いてみました。
これさえ見れば、あなたも必ず広告が出せるようになるはず!
かなりデカイ画像なので、ダウンロードして拡大してみてくださいね!
ちなみにIDは適当なので、ご自分のものに置き換えてください。
コーディング
コピペできるように書いておきます。
Podfileはこちら。プロジェクト名はご自分のものに置き換えてください。
# Uncomment the next line to define a global platform for your project # platform :ios, '9.0' target 'MySkillTrain1' do # Comment the next line if you're not using Swift and don't want to use dynamic frameworks use_frameworks! # Pods for MySkillTrain1 pod 'Firebase/Core' pod 'Firebase/AdMob' pod 'Google-Mobile-Ads-SDK' end
AppDelegate.swiftに書くコードはこちら。IDは適当です。ご自分のものに置き換えてください。
import UIKit import Firebase @UIApplicationMain class AppDelegate: UIResponder, UIApplicationDelegate { var window: UIWindow? func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool { // Override point for customization after application launch. // Use Firebase library to configure APIs FIRApp.configure() // Initialize Google Mobile Ads SDK, application IDを設定 GADMobileAds.configure(withApplicationID: "ca-app-pub-2356785678063803~2746526570") return true }
広告を置くView Controllerのコードはこちら。IDは適当です。ご自分のものに置き換えてください。
import GoogleMobileAds class ViewController: UIViewController { @IBOutlet weak var bannerView: GADBannerView! override func viewDidLoad() { print("Google Mobile Ads SDK version: \(GADRequest.sdkVersion())") bannerView.adUnitID = "ca-app-pub-1245659001912343/1337795797" bannerView.rootViewController = self bannerView.load(GADRequest()) } }
各要素の解説
やる気があれば書きます。
とりあえず各Webサービスへのリンクを書いときます。
IDを取得する順番とか、必要ですかねー?
その他の設定
テストでうっかり忘れがちなのが、ATS(App Transport Security)です。
これで外部との通信を許可しておかないと、広告が表示されません。