Copyright (C) iPhoneアプリ開発備忘録 All rights reserved.
ブログ内で記したコード、内容の正確性は保証いたしません。
記載内容を実装したことにより発生した不具合・損害等の責任は一切負いません。



2021年02月25日

AdMob スマートバナーからアダプティブバナー、インターステイシャル広告からフルスクリーン広告への移行



AdMob 8.xでは インターステイシャル広告はDeprecatedとなり、スマートバナーはアダプティブバナーへの移行を推奨されているため、これらの広告へ移行する。本記事で掲載している修正箇所はObjective-cコードのみ。

1. スマートバナーからアダプティブバナー
(1) バナーサイズ
kGADAdSizeSmartBannerPortrait → GADPortraitAnchoredAdaptiveBannerAdSizeWithWidth(self.view.frame.size.width)


kGADAdSizeSmartBannerLandscape → GADLandscapeAnchoredAdaptiveBannerAdSizeWithWidth(self.view.frame.size.width)



(2) エラー受信処理
変更前
- (void)adView:(GADBannerView *)view didFailToReceiveAdWithError:(GADRequestError *)error

変更後
- (void)bannerView:(nonnull GADBannerView *)bannerView didFailToReceiveAdWithError:(nonnull NSError *)error



2. インターステイシャル広告からフルスクリーン広告への移行
(1) .hファイル
#import <GoogleMobileAds/GADInterstitialDelegate.h > → 削除


GADInterstitialDelegate → GADFullScreenContentDelegate


GADInterstitial → GADInterstitialAd


(2) .mファイル
A. 広告ロード処理
変更前
- (void)interstitialLoad {

// AdMobインタースティシャルのロード
interstitial_ = [[GADInterstitial alloc] initWithAdUnitID:@"ca-app-pub-XXXX"];
interstitial_.delegate = self;

if ([NSThread isMainThread]) { // メインスレッドで実行されているか
// Initiate a generic request to load it with an ad.
[interstitial_ loadRequest:[GADRequest request]];
} else { // メインスレッドで実行されていない
dispatch_async(dispatch_get_main_queue(), ^{ // メインスレッドで実行する
// Initiate a generic request to load it with an ad.
[self->interstitial_ loadRequest:[GADRequest request]];
});
}
}

変更後
- (void)interstitialLoad {

// AdMobインタースティシャルのロード
if ([NSThread isMainThread]) { // メインスレッドで実行されているか
[self interstitialLoadSub];
} else { // メインスレッドで実行されていない
dispatch_async(dispatch_get_main_queue(), ^{ // メインスレッドで実行する
[self interstitialLoadSub];
});
}
}

- (void)interstitialLoadSub {

// Initiate a generic request to load it with an ad.
GADRequest *request = [GADRequest request];

[GADInterstitialAd loadWithAdUnitID:@"ca-app-pub-XXXX"
request:request
completionHandler:^(GADInterstitialAd *ad, NSError *error) {
if (error) {
NSLog(@"Failed to load interstitial ad with error: %@", [error localizedDescription]);
return;
}
self->interstitial_ = ad;
self->interstitial_.fullScreenContentDelegate = self;
}];
}


B. 広告が閉じられた場合の処理(変更後のコードは、エラー処理も記載)
変更前
- (void)interstitialDidDismissScreen:(GADInterstitial *)interstitial {
// AdMobインタースティシャルが閉じられると呼ばれる

[self interstitialLoad];

}

変更後
/// Tells the delegate that the ad failed to present full screen content.
- (void)ad:(nonnull id)ad didFailToPresentFullScreenContentWithError:(nonnull NSError *)error {
NSLog(@"Ad did fail to present full screen content.");
}

/// Tells the delegate that the ad presented full screen content.
- (void)adDidPresentFullScreenContent:(nonnull id)ad {
NSLog(@"Ad did present full screen content.");
}

/// Tells the delegate that the ad dismissed full screen content.
- (void)adDidDismissFullScreenContent:(nonnull id)ad {
// AdMobインタースティシャルが閉じられると呼ばれる
NSLog(@"Ad did dismiss full screen content.");

[self interstitialLoad];

}


C. 広告表示処理
変更前
// AdMobインタースティシャル広告の表示
if ([self->interstitial_ isReady]) {
[self->interstitial_ presentFromRootViewController:self];
}

変更後
// AdMobインタースティシャル広告の表示
if (self->interstitial_) {
[self->interstitial_ presentFromRootViewController:self];
}



参考記事:
https://developers.google.com/admob/ios/interstitial
https://developers.google.com/admob/ios/banner/adaptive?hl=ja
https://developers.google.com/admob/ios/api/reference/Functions
https://developers.google.com/ad-manager/mobile-ads-sdk/ios/api/reference/Functions
https://developers.google.com/admob/ios/test-ads?hl=ja
posted by mobileDeveloper at 21:15 | Comment(0) | 開発の流れ はてなブックマーク - AdMob スマートバナーからアダプティブバナー、インターステイシャル広告からフルスクリーン広告への移行 | このブログの読者になる | 更新情報をチェックする
この記事へのコメント
コメントを書く
お名前:

メールアドレス:

ホームページアドレス:

コメント:

認証コード: [必須入力]


※画像の中の文字を半角で入力してください。
※ブログオーナーが承認したコメントのみ表示されます。
Apple、Appleのロゴ、App Store、iPodのロゴ、iTunesは、米国および他国のApple Inc.の登録商標です。
iPhone、iPod touch、iPadはApple Inc.の商標です。
iPhone商標は、アイホン株式会社のライセンスに基づき使用されています。
その他、本ブログに記載されている製品名、会社名は、それぞれ各社の商標または登録商標です。