SwiftでGoogleAdmobインタースティシャル広告を貼る方法
admob事前導入がお済みでない方はこちらを参照してください
boostnote.hatenablog.com
インタースティシャル広告の貼り方
最初にテスト表示する時は
ca-app-pub-3940256099942544/4411468910
こちらのテスト用のadUnitIDを使用してください。
アカウントが削除される恐れがあります。
今回の実装は
Xcode: v9.1
シミュレータ: iPhone 8 Plus - iOS11.1
を使用しています。
インタースティシャル広告は単価が高いので開発者の方にはとてもおすすめです。 今回の実装は、先にボタンを配置して、そのボタンを押すとインタースティシャル広告が出るという仕組みにしました。
ViewController.swiftに以下のソースコードをコピペすると完了です。
詳細な説明は番号を参照してください。
import UIKit import GoogleMobileAds // ① class ViewController: UIViewController, GADInterstitialDelegate { // ② var interstitial: GADInterstitial! private var myButton: UIButton! override func viewDidLoad() { super.viewDidLoad() // initButton ③ myButton = UIButton() let bWidth: CGFloat = 200 let bHeight: CGFloat = 50 let posX: CGFloat = self.view.frame.width/2 - bWidth/2 let posY: CGFloat = self.view.frame.height/2 - bHeight/2 myButton.frame = CGRect(x: posX, y: posY, width: bWidth, height: bHeight) myButton.backgroundColor = UIColor.red myButton.layer.masksToBounds = true myButton.layer.cornerRadius = 20.0 myButton.setTitle("interstisial", for: .normal) myButton.setTitleColor(UIColor.white, for: .normal) myButton.setTitle("interstisial", for: .highlighted) myButton.setTitleColor(UIColor.black, for: .highlighted) myButton.addTarget(self, action: #selector(ViewController.onClickMyButton(sender:)), for: .touchUpInside) self.view.addSubview(myButton) // interstisial ④ interstitial = createAndLoadInterstitial() } // ⑤ func createAndLoadInterstitial() -> GADInterstitial { var interstitial = GADInterstitial(adUnitID: "ca-app-pub-3940256099942544/4411468910") interstitial.delegate = self interstitial.load(GADRequest()) return interstitial } // ⑥ func interstitialDidDismissScreen(_ ad: GADInterstitial) { interstitial = createAndLoadInterstitial() } // mybutton method ⑦ @objc internal func onClickMyButton(sender: UIButton) { if interstitial.isReady { interstitial.present(fromRootViewController: self) } else { print("Ad wasn't ready") } } /* delegate method ⑧*/ /// Tells the delegate an ad request succeeded. func interstitialDidReceiveAd(_ ad: GADInterstitial) { print("interstitialDidReceiveAd") } /// Tells the delegate an ad request failed. func interstitial(_ ad: GADInterstitial, didFailToReceiveAdWithError error: GADRequestError) { print("interstitial:didFailToReceiveAdWithError: \(error.localizedDescription)") } /// Tells the delegate that an interstitial will be presented. func interstitialWillPresentScreen(_ ad: GADInterstitial) { print("interstitialWillPresentScreen") } /// Tells the delegate the interstitial is to be animated off the screen. func interstitialWillDismissScreen(_ ad: GADInterstitial) { print("interstitialWillDismissScreen") } /// Tells the delegate that a user click will open another app /// (such as the App Store), backgrounding the current app. func interstitialWillLeaveApplication(_ ad: GADInterstitial) { print("interstitialWillLeaveApplication") } override func didReceiveMemoryWarning() { super.didReceiveMemoryWarning() // Dispose of any resources that can be recreated. } }
①事前準備で用意した「GoogleMobileAds」をインポートしてます
② GADInterstitialDelegateを呼びます
③mybuttonという名前のボタンを生成してます
④ createAndLoadInterstitialメソッドを呼び出しています
⑤ 先ほどの④の内容です。adUnitIDを取得し、広告のロードを行なっています。更にdelegateをセットしてます
⑥このメソッドはデリゲートメソッドです。インタースティシャル広告が閉じられた時にもう一度createAndLoadInterstitialメソッドを呼び出し、新しい広告をロードしています。これを実装しないと、インタースティシャル広告は一度しか表示されなくなります。
⑦mybuttonを押した時のメソッドです。ここでもし、interstisial広告が用意されているなら広告を表示するか否かの設定を行なってます。
⑧interstisialのデリゲートメソッドです。広告を受け取った時や、エラーで広告が受け取れなかった場合などの処理をここに書けます。
実際に本物の広告を貼る際には、
var interstitial = GADInterstitial(adUnitID: "ca-app-pub-3940256099942544/4411468910")
このadUnitIDに自分のadmobアカウントで申請したアプリのIDを貼ってください。
以上、SwiftでのAdmobインタースティシャル広告の貼り方でした。
Boostnoteのダウンロードはこちらから boostnote.io
GitHub Repository github.com