SwiftでGoogleAdmobの動画広告を貼る方法
admob事前導入がお済みでない方はこちらを参照してください
boostnote.hatenablog.com
動画広告の貼り方
横画面gif
縦画面gif
最初にテスト表示する時は
ca-app-pub-3940256099942544/1712485313
こちらのテスト用のadUnitIDを使用してください。
アカウントが削除される恐れがあります。
今回の実装は
Xcode: v9.1
シミュレータ: iPhone 8 Plus - iOS11.1
を使用しています。
今回の実装は、先にボタンを配置して、そのボタンを押すと動画広告が出るという仕組みにしました。
ViewController.swiftに以下のソースコードをコピペすると完了です。
詳細な説明は番号を参照してください。
import UIKit import GoogleMobileAds // ① class ViewController: UIViewController, GADRewardBasedVideoAdDelegate{ // ② 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.blue myButton.layer.masksToBounds = true myButton.layer.cornerRadius = 20.0 myButton.setTitle("Rewarded Video", for: .normal) myButton.setTitleColor(UIColor.white, for: .normal) myButton.setTitle("Rewarded Video", for: .highlighted) myButton.setTitleColor(UIColor.black, for: .highlighted) myButton.addTarget(self, action: #selector(ViewController.onClickMyButton(sender:)), for: .touchUpInside) self.view.addSubview(myButton) // ④ GADRewardBasedVideoAd.sharedInstance().delegate = self GADRewardBasedVideoAd.sharedInstance().load(GADRequest(), withAdUnitID: "ca-app-pub-3940256099942544/1712485313") } ⑤ func rewardBasedVideoAd(_ rewardBasedVideoAd: GADRewardBasedVideoAd, didRewardUserWith reward: GADAdReward) { print("Reward received with currency: \(reward.type), amount \(reward.amount).") } func rewardBasedVideoAdDidReceive(_ rewardBasedVideoAd:GADRewardBasedVideoAd) { print("Reward based video ad is received.") } func rewardBasedVideoAdDidOpen(_ rewardBasedVideoAd: GADRewardBasedVideoAd) { print("Opened reward based video ad.") } func rewardBasedVideoAdDidStartPlaying(_ rewardBasedVideoAd: GADRewardBasedVideoAd) { print("Reward based video ad started playing.") } func rewardBasedVideoAdDidClose(_ rewardBasedVideoAd: GADRewardBasedVideoAd) { print("Reward based video ad is closed.") } func rewardBasedVideoAdWillLeaveApplication(_ rewardBasedVideoAd: GADRewardBasedVideoAd) { print("Reward based video ad will leave application.") } func rewardBasedVideoAd(_ rewardBasedVideoAd: GADRewardBasedVideoAd, didFailToLoadWithError error: Error) { print("Reward based video ad failed to load.") } // mybutton method ⑥ @objc internal func onClickMyButton(sender: UIButton) { if GADRewardBasedVideoAd.sharedInstance().isReady == true { GADRewardBasedVideoAd.sharedInstance().present(fromRootViewController: self) } } override func didReceiveMemoryWarning() { super.didReceiveMemoryWarning() // Dispose of any resources that can be recreated. } }
①事前準備で用意した「GoogleMobileAds」をインポートしてます
② GADRewardBasedVideoAdDelegateを呼びます
③mybuttonという名前のボタンを生成してます
④ GADRewardBasedVideoAdを生成とロードを行なってます
⑤ GADRewardBasedVideoAdのデリゲートメソッドです。広告を受け取った時や、エラーで広告が受け取れなかった場合などの処理をここに書けます。
⑥mybuttonを押した時のメソッドです。GADRewardBasedVideoAd広告が用意されているなら広告を表示します。
実際に本物の広告を貼る際には、
GADRewardBasedVideoAd.sharedInstance().load(GADRequest(), withAdUnitID: "ca-app-pub-3940256099942544/1712485313")
このadUnitIDに自分のadmobアカウントで申請したアプリのIDを貼ってください。
以上、Swift-Admobの動画広告の貼り方でした。
Boostnoteのダウンロードはこちらから boostnote.io
GitHub Repository github.com