目的
Google Mobile Ads SDK(AdMob)のバージョン8
が導入できるようになっていたので、入れてみました。
その対応メモです。
公式ページでも書いていますが、大幅な変更となっています。
趣旨としてはインタースティシャル広告とリワード広告のスタイル共有という一貫性向上のためということらしいです。
ただバナー広告とかも修正入っている感じでした。
広告は現金収入及び、報酬という大事な部分となるので、
修正して不具合が起こると、集金やアプリ信頼性などに結構な打撃となります。
あまりアップデートはしてほしくないですよね。
環境
- Xcode Version 12.5
- Swift 5
参照サイト
Google Admob SDK v8 の準備
Google Admob インタースティシャル広告
Google Admob ネイティブ広告
Google Admob バナー広告
一番上のページがもっとも重要で、このページにインタースティシャル広告やネイティブ広告の導入方法も書いてあります。
しかもv7とv8を簡単に比較できておまけにSwfitとObjective-Cも簡単に切り替えれて、ものすごく親切。
ただ。。
インタースティシャル広告の場合、1番目と2番目のページはほぼ一緒ですが、2番目のページにのみ超重要なことが書いています。1ページ目にも書けばよいのに。
GADInterstitialAd は使い捨てオブジェクトです。つまり、インタースティシャル広告を一度表示すると、再度表示することはできません。おすすめの方法は、GADFullScreenContentDelegate の adDidDismissFullScreenContent: メソッドで別のインタースティシャル広告を読み込んでおくことです。この方法では、前のインタースティシャル広告の表示が終了したらすぐに次のインタースティシャル広告を読み込めます。
まぁ、前のバージョンからそうだったと思われるので、すぐ気付くと思いますが。
対応メモ
以降の記載は設定メモですので、参照した際の損害は一切負いかねますのでご了承下さい。var unifiedNativeAd: GADUnifiedNativeAd?
↓↓↓
var unifiedNativeAd: GADNativeAd?
---------------------
adLoader = GADAdLoader(adUnitID: "ca-app-pub-xxxx",
rootViewController: self,
adTypes: [ GADAdLoaderAdType.unifiedNative],
options: nil)
adLoader?.delegate = self
↓↓↓
adLoader = GADAdLoader(adUnitID: "ca-app-pub-xxxx",
rootViewController: self,
adTypes: [ .native ],
options: nil)
adLoader?.delegate = self
adLoader?.load(GADRequest())
---------------------
self.adLoader?.load(GADRequest())
↓↓↓変化無し!?
self.adLoader?.load(GADRequest())
if let nativeAdView = nibView as? GADUnifiedNativeAdView {
}
↓↓↓
if let nativeAdView = nibView as? GADNativeAdView {
}
extension xxx: GADUnifiedNativeAdLoaderDelegate {
func adLoader(_ adLoader: GADAdLoader, didReceive nativeAd: GADUnifiedNativeAd) {
unifiedNativeAd?.delegate = self
unifiedNativeAd = nativeAd
}
func adLoader(_ adLoader: GADAdLoader, didFailToReceiveAdWithError error: GADRequestError) {
}
}
↓↓↓
extension newarrivalsTableViewController: GADNativeAdLoaderDelegate {
func adLoader(_ adLoader: GADAdLoader, didReceive nativeAd: GADNativeAd) {
unifiedNativeAd?.delegate = self
unifiedNativeAd = nativeAd
}
func adLoaderDidFinishLoading(_ adLoader: GADAdLoader) {
}
func adLoader(_ adLoader: GADAdLoader, didFailToReceiveAdWithError error: Error) {
}
}
extension xxx: GADUnifiedNativeAdDelegate {
func nativeAdDidRecordImpression(_ nativeAd: GADUnifiedNativeAd) {
}
func nativeAdDidRecordClick(_ nativeAd: GADUnifiedNativeAd) {
}
func nativeAdWillPresentScreen(_ nativeAd: GADUnifiedNativeAd) {
}
func nativeAdWillDismissScreen(_ nativeAd: GADUnifiedNativeAd) {
}
func nativeAdDidDismissScreen(_ nativeAd: GADUnifiedNativeAd) {
}
func nativeAdWillLeaveApplication(_ nativeAd: GADUnifiedNativeAd) {
}
↓↓↓
extension xxx: GADNativeAdDelegate {
func nativeAdDidRecordImpression(_ nativeAd: GADNativeAd) {
}
func nativeAdDidRecordClick(_ nativeAd: GADNativeAd) {
}
func nativeAdWillPresentScreen(_ nativeAd: GADNativeAd) {
}
func nativeAdWillDismissScreen(_ nativeAd: GADNativeAd) {
}
func nativeAdDidDismissScreen(_ nativeAd: GADNativeAd) {
}
func nativeAdWillLeaveApplication(_ nativeAd: GADNativeAd) {
}
}
冒頭にも書いたが、adDidDismissFullScreenContentで広告データを呼ばないと、2回目の広告表示で、
Presentation Error: Will not present ad because ad object has been used..
var interstitial: GADInterstitial!
↓↓↓
var interstitial: GADInterstitialAd!
self.interstitial = GADInterstitial(adUnitID: "ca-app-pub-xxxx")
self.interstitial.delegate = self
let request = GADRequest()
self.interstitial.load(request)
↓↓↓
let request = GADRequest()
GADInterstitialAd.load(withAdUnitID:"ca-app-pub-xxxx",
request: request,
completionHandler: { (ad, error) in
if let error = error {
return
}
self.interstitial = ad
self.interstitial.fullScreenContentDelegate = self
})
if interstitial.isReady {
interstitial.present(fromRootViewController: self)
}
↓↓↓
if let ad = interstitial {
ad.present(fromRootViewController: self)
}
extension xxx: GADInterstitialDelegate {
func interstitialDidReceiveAd(_ ad: GADInterstitial) {
}
func interstitial(_ ad: GADInterstitial, didFailToReceiveAdWithError error: GADRequestError) {
}
func interstitialWillPresentScreen(_ ad: GADInterstitial) {
}
func interstitialWillDismissScreen(_ ad: GADInterstitial) {
}
func interstitialDidDismissScreen(_ ad: GADInterstitial) {
//self.interstitial.loadを呼ぶ
}
func interstitialWillLeaveApplication(_ ad: GADInterstitial) {
}
}
↓↓↓
extension xxx: GADFullScreenContentDelegate {
func adDidPresentFullScreenContent(_ ad: GADFullScreenPresentingAd) {
}
func adDidDismissFullScreenContent(_ ad: GADFullScreenPresentingAd) {
//self.interstitial = adを呼ぶ
}
func ad(_ ad: GADFullScreenPresentingAd, didFailToPresentFullScreenContentWithError error: Error) {
}
}
class xxx: UIViewController, GADRewardedAdDelegate {
}
↓↓↓
class xxx: UIViewController, GADFullScreenContentDelegate {
}
rewardedAd = GADRewardedAd(adUnitID: "ca-app-pub-xxx")
rewardedAd?.load(GADRequest()) { error in
if let error = error {
} else {
}
}
↓↓↓
let request = GADRequest()
var adunitidString = "ca-app-pub-xxx"
GADRewardedAd.load(withAdUnitID: adunitidString,
request: request, completionHandler: { (ad, error) in
if let error = error {
return
}
self.rewardedAd = ad
self.rewardedAd?.fullScreenContentDelegate = self
}
)
if rewardedAd?.isReady {
rewardedAd?.present(fromRootViewController: self, delegate:self)
}
↓↓↓
if let ad = rewardedAd {
ad.present(fromRootViewController: self, userDidEarnRewardHandler: {
let reward = ad.adReward
}
}
func rewardedAdDidPresent(_ rewardedAd: GADRewardedAd) {
}
func rewardedAdDidDismiss(_ rewardedAd: GADRewardedAd) {
//rewardedAd?.loadを呼ぶ
}
func rewardedAd(_ rewardedAd: GADRewardedAd, didFailToPresentWithError error: Error) {
}
↓↓↓
func adDidPresentFullScreenContent(_ ad: GADFullScreenPresentingAd) {
}
func adDidDismissFullScreenContent(_ ad: GADFullScreenPresentingAd) {
//GADRewardedAd.loadを呼ぶ
}
func ad(_ ad: GADFullScreenPresentingAd, didFailToPresentFullScreenContentWithError error: Error) {
}
バナー広告について
バナー広告については実装しているアプリがないので省略します。
v7.68.0でDFPBannerViewとなっていて。
v8.0.0でGAMBannerViewとなっていました。
ちょっと気になったのが、自分が知っているのはGADBannerView
というクラスだったのですが、
いつの間にかDFPBannerViewに変わったんですかね?
んーでも以下のバナー設定ページでは引き続きGADBannerView
が使われてますね。
Google Admob バナー広告
ちょっと保留。
以上です。