2020/05/08
Admobのインタースティシャル広告実装方法 -Swift5.0
Google Admobでの広告実装方法です。
アプリを使っているとめちゃくちゃうざいと思うことも多いですが、収益性もいいので、ぜひ入れたいところですよね。
ということで自分のアプリにも実装したのでまとめました。
Podfileへの追加
※CocoaPodsを使います。インストールしている前提
Use_frameworks!の下に二つ追加する
use_frameworks!
pod ‘Firebase/Core’
pod ‘Firebase/AdMob’
Xcode実装するサンプルコード
サンプルコードは下記の通りで、基本的な説明もコメントに記載済みです。
今回は、下記実装になっています。
・ボタンをタップするとカウントダウンタイマーが作動
・タイマーが0になると、ポップアップ(UIAlertController)が表示
・ポップアップの「はい」を押すとインステ広告が表示される
LabelとButtonの接続だけ忘れずに。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 |
//サンプルコード //ストーリーボードで、LabelのOutlet接続とボタンのAction接続は必要 import UIKit import GoogleMobileAds //CocoaPodsなどで用意した「GoogleMobileAds」のインストール class ViewController2: UIViewController,GADInterstitialDelegate { //デリゲートを呼ぶ var interstitial: GADInterstitial! // 広告をnullで宣言 var timer = Timer() var count:Int = 3 var adunitID = "ca-app-pub-3940256099942544/4411468910" @IBOutlet weak var myLabel: UILabel! override func viewDidLoad() { super.viewDidLoad() // interstisial広告を貼るためにメソッドを呼び出す interstitial = createAndLoadInterstitial() } //ボタンを押した時の処理 -> タイマースタート @IBAction func tappedButton(_ sender: Any) { //1秒周期でcountDownメソッドを呼び出すタイマーを開始する。 self.timer = Timer.scheduledTimer(timeInterval: 1.0, target:self, selector: #selector(self.countDown), userInfo:nil, repeats:true) } //タイマーから呼び出されるメソッド @objc func countDown(){ //カウントを減してラベル文字を更新 count -= 1 myLabel.text = "残り:\(count)" //1以上 if(count > 0) { //カウントが0になったら } else if (count == 0) { self.alertDisplay() timer.invalidate() //カウントを元の状態に戻す count = 3 myLabel.text = "残り:3" } } //アラート(ポップアップ)表示 func alertDisplay() { //ポップアップを実装 let alertController = UIAlertController(title: "広告", message: "広告を表示しますか?", preferredStyle: UIAlertController.Style.alert) //はいボタンの実装 let okAction = UIAlertAction(title: "はい", style: UIAlertAction.Style.default){ (action: UIAlertAction) in //「はい」がクリックされた時の処理 print("はいをクリック") //広告を表示する関数の呼び出し self.dispalyAd() } //いいえボタンの実装 let cancelButton = UIAlertAction(title: "いいえ", style: UIAlertAction.Style.cancel, handler: nil) //はいボタンをポップアップのビューに追加 alertController.addAction(okAction) //いいえボタンをポップアップのビューに追加 alertController.addAction(cancelButton) //アラートの表示 present(alertController,animated: true,completion: nil) } /* ここから下が広告設定 */ //広告が表示用の関数。広告の準備があれば表示する処理を入れている。 @objc func dispalyAd() { if self.interstitial.isReady{ print("広告準備あり") self.interstitial.present(fromRootViewController: self) } else { print("広告の準備がない") } } // override func viewDidLoad()で呼び出したメソッドの中身を実装するイメージ。 // adUnitIDはAdmobで作成たいものを使用(現状はテスト用のID) func createAndLoadInterstitial() -> GADInterstitial { let interstitial = GADInterstitial(adUnitID: adunitID) interstitial.delegate = self interstitial.load(GADRequest()) return interstitial } // デリゲートメソッド。インタースティシャル広告が閉じられた時にもう一度createAndLoadInterstitialメソッドを呼び出し、 // 新しい広告をロードしています。これを実装しないと、インタースティシャル広告は一度しか表示されなくなる。 func interstitialDidDismissScreen(_ ad: GADInterstitial) { interstitial = createAndLoadInterstitial() } //デリゲートメソッドです。広告を受け取った時や、エラーで広告が受け取れなかった場合などの処理をここに書けます。 /// 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") } } |
お知らせ

RealmSwift, Admobの動画・インステ・バナー広告、UICollectionView、iOS-Charts、UITableViewを使用しているので、是非ご利用ください!
[…] Admobのインタースティシャル広告実装方法 -Swift4.0 […]
初めまして。
アプリをビルドした1回目のボタンタップ時、またはアプリを落として再度開いてから、1回目のボタンタップ時の広告がでません。
デバックには
広告の準備がないとでます。
2回目以降のボタンタップ時は問題ないのですが。
コメントありがとうございます。
今試してみましたが、私の挙動では問題ないことを確認しました。
うーん、、なんでしょうかね・・・・テストIDで実施してますでしょうか?
テストIDであれば基本的には広告が切れることはないと思うのですが、、初回のインタースティシャルの読み込みが終わっていないか、読み込みが失敗しているのかなと思います。
もしコード貼っていただければもう少し詳細を確認します。
よろしくお願いします!
問題解決しました。タイマーで読み込みを行なっていたのですね。
タイマーのコードは必要ないと思って書いてませんでした。
お手数をおかけしました。ありがとうございます!