Migrasi SDK

Halaman ini membahas migrasi untuk Google Mobile Ads SDK versi saat ini dan sebelumnya untuk iOS. Versi 11.0.0 diperkirakan akan dirilis pada Januari 2024.

Bermigrasi dari SDK versi v10 ke v11

Target deployment minimum

Target deployment minimum telah ditingkatkan ke iOS 12.

Versi Xcode minimum

Versi Xcode minimum yang didukung telah dinaikkan ke 15.1.

Iklan berhenti ditayangkan di iOS 12

Google Mobile Ads SDK versi 11.0.0 hanya menayangkan iklan di perangkat yang menjalankan iOS 13 dan yang lebih baru.

Menghapus dependensi GoogleAppMeasurement

Pada versi 11.0.0, dependensi pada GoogleAppMeasurement telah dihapus. Dependensi ini yang mendukung tombol metrik pengguna di AdMob akan dihentikan pada awal 2024. Untuk terus mengumpulkan metrik pengguna di AdMob, tautkan aplikasi AdMob Anda ke Firebase dan integrasikan Google Analytics for Firebase SDK ke dalam aplikasi Anda.

Perubahan pada presentasi iklan layar penuh

Perubahan berikut memengaruhi berbagai format iklan:

  • Iklan di layar pembuka aplikasi
  • Interstisial
  • Diberikan sebagai reward
  • Interstisial reward

Parameter pengontrol tampilan di -canPresentFromRootViewController:error: dan -presentFromRootViewController: bersifat nullable. Jika nilai nol diteruskan, iklan akan ditampilkan dari pengontrol tampilan teratas dalam hierarki pengontrol tampilan.

Metode dihapus

Metode berikut dihapus.

Jenis v11.0.0 Metode Catatan
GADAppOpenAd load(withAdUnitID adUnitID: String, request: GADRequest?, orientation: UIInterfaceOrientation) Sebagai gantinya, gunakan load(withAdUnitID adUnitID: String, request: GADRequest?).
GADMediationBannerAdEventDelegate willBackgroundApplication() Tidak ada pengganti.
GADMediationInterstitialAdEventDelegate willBackgroundApplication() Tidak ada pengganti.
GADMediationNativeAdEventDelegate willBackgroundApplication() Tidak ada pengganti.
GADMediationRewardedAdEventDelegate didRewardUser(with reward: GADAdReward) Sebagai gantinya, gunakan didRewardUser().
GADMediatedUnifiedNativeAdNotificationSource mediatedNativeAdWillLeaveApplication(_ mediatedNativeAd: GADMediatedUnifiedNativeAd) Tidak ada pengganti.
GADRequestConfiguration setSameAppKeyEnabled(_ enabled: Bool) Sebagai gantinya, gunakan setPublisherFirstPartyIDEnabled(_ enabled: Bool).
tagForUnderAge(ofConsent underAgeOfConsent: Bool) Sebagai gantinya, gunakan properti tagForUnderAgeOfConsent.
tag(forChildDirectedTreatment childDirectedTreatment: Bool) Sebagai gantinya, gunakan properti tagForChildDirectedTreatment.

Properti dihapus

Properti berikut dihapus.

Kelas v11.0.0 Properti Catatan
GADMediationAdConfiguration hasUserLocation Tidak ada pengganti.
userLatitude
userLongitude
userLocationAccuracyInMeters
childDirectedTreatment Sebagai gantinya, gunakan GADMobileAds.sharedInstance.requestConfiguration.tagForChildDirectedTreatment.
GADResponseInfo adNetworkClassName Sebagai gantinya, gunakan adNetworkClassName dari loadedAdNetworkResponseInfo.

Menghapus GADAdFormatUnknown

Menghapus GADAdFormatUnknown tanpa pengganti.

Perubahan pada versi SDK logging

Versi 11.0.0 menghapus sdkVersion. Untuk mencatat versi Google Mobile Ads SDK ke dalam log, gunakan versionNumber.

Versi 10.0.0

GADMobileAds.sharedInstance().sdkVersion

Versi 11.0.0

GADGetStringFromVersionNumber(GADMobileAds.sharedInstance().versionNumber)

Perubahan pada penanganan error GADAdLoader

Mulai versi 11.0.0, GADAdLoader tidak membuat permintaan iklan jika delegate tidak sesuai dengan protokol delegasi jenis iklan yang diminta. Sebelumnya, metode ini gagal setelah permintaan iklan dibuat.

Perubahan pada perilaku pengujian

Lihat tabel yang menampilkan kondisi yang diperbarui saat properti berikut menampilkan true.

Class Properti
GADMediationAdConfiguration isTestRequest
GADCustomEventRequest isTesting
v10.0.0 v11.0.0
  • Perangkat secara eksplisit dideklarasikan sebagai perangkat pengujian di testDeviceIdentifiers.
  • Perangkat secara eksplisit dideklarasikan sebagai perangkat pengujian di testDeviceIdentifiers.
  • Perangkat adalah simulator.
  • Perangkat secara eksplisit dideklarasikan sebagai perangkat pengujian di UI AdMob .

Bermigrasi dari v9 ke v10

Iklan berhenti ditayangkan di iOS 11

Google Mobile Ads SDK versi 10.0.0 hanya menayangkan iklan di perangkat yang menjalankan iOS 12 dan yang lebih baru.

Upgrade ke Google Mobile Ads SDK versi 10.0.0 tidak akan merusak aplikasi Anda di perangkat iOS 11 dan iOS 10. Namun, iklan tidak akan ditayangkan di perangkat tersebut.

Membuat aplikasi dengan bitcode tidak lagi didukung

Bitcode di aplikasi seluler Anda kini harus dinonaktifkan untuk mengintegrasikan Google Mobile Ads SDK.

Jenis dihapus

Jenis Catatan
GADGender Tidak ada pengganti.
GADMRewardBasedVideoAdNetworkAdapterProtocol Semua adaptor mediasi reward yang tercantum di Choose Networks telah berhenti menggunakan protokol ini selama lebih dari satu tahun. Gunakan GADMediationAdapter untuk mediasi dan peristiwa khusus.
GADMRewardBasedVideoAdNetworkConnectorProtocol

Properti dihapus

Properti berikut dihapus tanpa pengganti.

Kelas v10.0.0 Properti
GADMediationAdRequest userBirthday
userGender
userHasLocation
userLatitude
userLongitude
userLocationAccuracyInMeters
userLocationDescription
GADCustomEventRequest userHasLocation
userLatitude
userLongitude
userLocationAccuracyInMeters
userLocationDescription

Bermigrasi dari v8 ke v9

Iklan berhenti ditayangkan di iOS 10

Versi iOS minimum yang didukung Google Mobile Ads SDK versi 9.0.0 adalah iOS 11.

Upgrade ke Google Mobile Ads SDK versi 9.0.0 tidak akan merusak aplikasi Anda di perangkat iOS 10, tetapi, iklan tidak akan ditayangkan di perangkat tersebut.

Penerapan kontrol status bar yang lebih ketat

Mulai versi 9.0.0, saat menayangkan iklan format layar penuh, aplikasi Anda harus memastikan bahwa iklan tersebut dapat mengontrol penyajian status bar. Anda akan melihat pesan error di log jika gagal melakukannya.

Bergantung pada tata letak khusus pengontrol tampilan di aplikasi, Anda mungkin tidak perlu membuat perubahan untuk memastikan hal ini. Pertimbangkan apakah Anda perlu menetapkan properti childViewControllerForStatusBarHidden di rootViewController iklan Anda.

Mengganti nama adDidPresentFullScreenContent: menjadi adwillPresentFullScreenContent:

Tidak ada perubahan perilaku. Metode delegasi dipanggil tepat sebelum iklan akan ditampilkan, sehingga nama metode baru lebih mencerminkan fungsinya.

Menghapus API setelan lokasi di GADRequest

- (void)setLocationWithLatitude:longitude:accuracy: telah dihapus dari GADRequest karena data lokasi tidak digunakan oleh Google untuk menargetkan iklan. Gunakan API pihak ketiga untuk memberikan informasi kepada jaringan iklan pihak ketiga, jika diperlukan.

Penghentian antarmuka peristiwa kustom

Peristiwa kustom memungkinkan penayang menggunakan AdMob Mediasi untuk menambahkan mediasi waterfall untuk jaringan iklan yang bukan merupakan salah satu jaringan iklan yang didukung.

Semua protokol peristiwa kustom tidak digunakan lagi. Sebagai gantinya, gunakan protokol GADMediationAdapter dan GADMediationAdEventDelegate yang ada untuk mencapai fungsionalitas yang sama. Perubahan ini meningkatkan kejelasan dan memungkinkan Anda membuat peristiwa kustom untuk iklan reward dan iklan interscroller yang sebelumnya tidak tersedia.

API

Tabel di bawah ini mencantumkan API adaptor mediasi yang sesuai ke API peristiwa kustom yang harus digunakan mulai dari versi 9.0.0.

v8 v9
GADCustomEventBanner
GADCustomEventInterstitial
GADCustomEventNativeAd
GADMediationAdapter GADMediationBannerAd
GADMediationInterstitialAd
GADMediationInterscrollerAd
GADMediationRewardedAd
GADMediationNativeAd
delegate Delegasi ditampilkan oleh pengendali penyelesaian pemuatan dari setiap fungsi pemuatan class GADMediationAdapter
-init -init
-requestBannerAd:parameter:label:request: -loadBannerForAdConfiguration:completionHandler:
-requestInterstitialAdWithParameter:label:request: -loadInterstitialForAdConfiguration:completionHandler:
-requestNativeAdWithParameter:request:adTypes:options:rootViewController: -loadNativeAdForAdConfiguration:completionHandler:
T/A -loadInterscrollerAdForAdConfiguration:completionHandler:
T/A -loadRewardedAdForAdConfiguration:completionHandler:
-presentFromRootViewController: -presentFromViewController:
-handlesUserClicks -handlesUserClicks
-handlesUserImpressions -handlesUserImpressions

Metode delegasi

Tabel di bawah ini mencantumkan metode delegasi peristiwa iklan mediasi yang sesuai untuk metode delegasi peristiwa kustom yang harus digunakan mulai dari versi 9.0.0.

v8 v9
GADCustomEventBannerDelegate
GADCustomEventInterstitialDelegate
GADCustomEventNativeAdDelegate
GADMediationAdEventDelegate IklanMediasi GAD
-customEventBanner:didReceiveAd:
-customEventInterstitialDidReceiveAd:
Status pemuatan iklan disertakan dalam pengendali penyelesaian pemuatan di setiap fungsi pemuatan di class GADMediationAdapter
-customEventBanner:didFailAd:
-customEventInterstitial:didFailAd:
-customEventBannerWasClicked:
-customEventInterstitialWasClicked:
-reportClick
-customEventBannerWillPresentModal:
-customEventInterstitialWillPresentModal:
-willPresentFullScreenView
-customEventBannerWillDismissModal:
-customEventInterstitialWillDismissModal:
-willDismissFullScreenView
-customEventBannerDidDismissModal:
-customEventInterstitialDidDismissModal:
-didDismissFullScreenView
-customEventBannerWillLeaveApplication:
-customEventInterstitialWillLeaveApplication:
-willBackgroundApplication
viewControllerForPresentingModalView -[GADMediationBannerAd view]

Metode dan konstanta lainnya yang dihapus/diganti

Perubahan pada metode, konstanta, atau properti
Konstanta berawalan kGAD- Dihapus. Gunakan konstanta berawalan GAD-.
GADAdNetworkResponseInfo credentials dihapus. Sebagai gantinya, gunakan adUnitMapping.
GAMRequest kGAMSimulatorID di GAMRequest tidak digunakan lagi. Sebagai gantinya, gunakan GADSimulatorID di GADRequestConfiguration.
GADCustomNativeAd mediaView di GADCustomNativeAd tidak digunakan lagi. Gunakan mediaContent sebagai gantinya.
API Pembelian dalam Aplikasi di GoogleMobileAds inAppPurchase API di GoogleMobileAds telah dihapus.

Bermigrasi dari v7 ke v8

Google Mobile Ads SDK versi 8.0.0 memperkenalkan beberapa perubahan besar serta beberapa penggantian nama dan penghapusan API.

Update API format layar penuh

Mulai versi 8.0.0, iklan interstisial dan reward memiliki gaya iklan layar penuh yang sama agar lebih konsisten. API iklan layar penuh baru ini memiliki dua perbedaan besar dari API iklan layar penuh versi 7:

  1. Metode class statis load.

    Pendekatan sebelumnya untuk memuat/menampilkan iklan layar penuh adalah sebagai berikut:

    1. Membuat instance objek iklan dan menyimpan referensi ke instance tersebut.
    2. Menetapkan delegasi yang menangani beban dan menampilkan callback.
    3. Muat iklan.
    4. Periksa apakah iklan dimuat menggunakan isReady.
    5. Menampilkan iklan.

    Pada versi 8, pendekatannya sedikit berubah. Callback pemuatan tidak lagi menjadi bagian dari delegasi. Sebagai gantinya, peristiwa tersebut akan diteruskan ke metode load sebagai pengendali penyelesaian:

    1. Panggil metode pemuatan statis di class iklan dan sediakan pengendali penyelesaian beban.
    2. Dalam callback penyelesaian pemuatan, pertahankan referensi ke iklan yang dimuat dan ditampilkan.
    3. Menetapkan delegasi yang menangani callback acara.
    4. Menampilkan iklan.

    Pendekatan baru memberikan manfaat berikut:

    • Anda tidak akan memiliki referensi ke iklan yang tidak dimuat.
    • Anda tidak perlu menahan objek iklan saat sedang dimuat.
  2. Peristiwa iklan yang konsisten.

    Jenis peristiwa API yang sudah ada API v8
    Memuat peristiwa GADInterstitialDelegate atau GADRewardedAdDelegate GADInterstitialAdLoadCompletionHandler atau GADRewardedAdLoadCompletionHandler
    Acara presentasi GADFullScreenContentDelegate

    Sebelumnya, untuk memproses peristiwa iklan apa pun, Anda harus mendaftarkan class yang menerapkan protokol GADInterstitialDelegate ke properti delegasi interstisial, atau mendaftarkan class yang mengimplementasikan protokol GADRewardedAdDelegate ke properti delegasi iklan reward, bergantung pada format yang Anda gunakan. Delegasi yang sama ini memiliki metode yang berkaitan dengan siklus proses pemuatan dan presentasi iklan.

    Pada versi 8, peristiwa pemuatan dan presentasi terpisah. Kini Anda dapat mendaftarkan GADFullScreenContentDelegate kapan saja sebelum menampilkan iklan, tanpa harus menetapkan satu delegasi sebelum memuat iklan. Peristiwa pemuatan iklan, yang khusus untuk setiap format, dipindahkan ke satu pengendali penyelesaian pemuatan yang diteruskan dalam metode pemuatan.

Interstisial

Muat iklan

Cuplikan kode di bawah ini menunjukkan cara memuat iklan interstisial dan memproses peristiwa saat iklan berhasil atau gagal dimuat.

v7

Swift

import GoogleMobileAds
import UIKit

class ViewController: UIViewController, GADInterstitialDelegate {

  var interstitial: GADInterstitial!

  override func viewDidLoad() {
    super.viewDidLoad()
    interstitial = GADInterstitial(adUnitID: "ca-app-pub-3940256099942544/44118910")
    interstitial.delegate = self
    let request = GADRequest()
    interstitial.load(request)
  }

  /// Tells the delegate an ad request succeeded.
  func interstitialDidReceiveAd(_ ad: GADInterstitial) {
    print("Interstitial ad loaded.")
  }

  /// Tells the delegate an ad request failed.
  func interstitial(_ ad: GADInterstitial, didFailToReceiveAdWithError error: GADRequestError) {
    print("Interstitial ad failed to load with error: \(error.localizedDescription)")
  }
}

Objective-C

@import GoogleMobileAds;
@import UIKit;

@interface ViewController () 

@property(nonatomic, strong) GADInterstitial *interstitial;

@end

@implementation ViewController

- (void)viewDidLoad {
  [super viewDidLoad];
  self.interstitial = [[GADInterstitial alloc]
      initWithAdUnitID:@"ca-app-pub-3940256099942544/44118910"];
  self.interstitial.delegate = self;
  GADRequest *request = [GADRequest request];
  [self.interstitial loadRequest:request];
}

/// Tells the delegate an ad request succeeded.
- (void)interstitialDidReceiveAd:(GADInterstitial *)ad {
  NSLog(@"Insterstitial ad loaded.");
}

/// Tells the delegate an ad request failed.
- (void)interstitial:(GADInterstitial *)ad
    didFailToReceiveAdWithError:(GADRequestError *)error {
  NSLog(@"Interstitial ad failed to load with error: %@", [error localizedDescription]);
}

v8

Swift

import GoogleMobileAds
import UIKit

class ViewController: UIViewController, GADFullScreenContentDelegate {

  var interstitial: GADInterstitialAd?

  override func viewDidLoad() {
    super.viewDidLoad()
    let request = GADRequest()
    GADInterstitialAd.load(withAdUnitID:"ca-app-pub-8123415297019784/4985798738",
                                request: request,
                      completionHandler: { (ad, error) in
                        if let error = error {
                          print("Failed to load interstitial ad with error: \(error.localizedDescription)")
                          return
                        }
                        self.interstitial = ad
                        self.interstitial.fullScreenContentDelegate = self
                      }
    )
  }
}

Objective-C

@import GoogleMobileAds;
@import UIKit;

@interface ViewController () 

@property(nonatomic, strong) GADInterstitialAd *interstitial;

@end

@implementation ViewController

- (void)viewDidLoad {
  [super viewDidLoad];
  GADRequest *request = [GADRequest request];
  [GADInterstitialAd loadWithAdUnitID:@"ca-app-pub-3940256099942544/44118910"
                              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;
  }];
}

Iklan Display

v7

Swift

func showInterstitial() {
  ...
  if interstitial.isReady {
    interstitial.present(fromRootViewController: self)
  } else {
    print("Ad wasn't ready")
  }
}

Objective-C

- (void)showInterstitial: {
  ...
  if (self.interstitial.isReady) {
    [self.interstitial presentFromRootViewController:self];
  } else {
    NSLog(@"Ad wasn't ready");
  }
}

v8

Swift

func showInterstitial() {
  ...
  if let ad = interstitial {
    ad.present(fromRootViewController: self)
  } else {
    print("Ad wasn't ready")
  }
}

Objective-C

- (void)showInterstitial: {
  ...
  if (self.interstitial) {
    [self.interstitial presentFromRootViewController:self];
  } else {
    NSLog(@"Ad wasn't ready");
  }
}

Peristiwa iklan presentasi

Cuplikan kode di bawah ini menunjukkan cara menangani callback saat iklan ditampilkan (berhasil atau tidak berhasil) dan saat iklan ditutup.

v7

Swift

override func viewDidLoad() {
  super.viewDidLoad()
  interstitial = GADInterstitial(adUnitID: "ca-app-pub-3940256099942544/44118910")
  interstitial.delegate = self
  ...
}

/// Tells the delegate that an interstitial will be presented.
func interstitialWillPresentScreen(_ ad: GADInterstitial) {
  print("Interstitial ad will be presented.")
}

/// Tells the delegate the interstitial is to be animated off the screen.
func interstitialWillDismissScreen(_ ad: GADInterstitial) {
  print("Interstitial ad will be dismissed.")
}

/// Tells the delegate the interstitial had been animated off the screen.
func interstitialDidDismissScreen(_ ad: GADInterstitial) {
  print("Interstitial ad dismissed.")
}

/// Tells the delegate that a user click will open another app
/// (such as the App Store), backgrounding the current app.
///
/// This is not a reliable callback for an ad click event and is removed in
/// version 8. If you wish to listen to an ad causing a user to leave the app,
/// use applicationWillResignActive: or sceneWillResignActive: instead.
func interstitialWillLeaveApplication(_ ad: GADInterstitial) {
  print("Interstitial ad will leave application.")
}

Objective-C

- (void)viewDidLoad {
  [super viewDidLoad];
  self.interstitial = [[GADInterstitial alloc] initWithAdUnitID:"ca-app-pub-3940256099942544/44118910"];
  self.interstitial.delegate = self;
  ...
}

/// Tells the delegate that an interstitial will be presented.
- (void)interstitialWillPresentScreen:(GADInterstitial *)ad {
  NSLog(@"Interstitial ad will be presented.");
}

/// Tells the delegate the interstitial is to be animated off the screen.
- (void)interstitialWillDismissScreen:(GADInterstitial *)ad {
  NSLog(@"Interstitial ad will be dismissed.");
}

/// Tells the delegate the interstitial had been animated off the screen.
- (void)interstitialDidDismissScreen:(GADInterstitial *)ad {
  NSLog(@"Interstitial ad dismissed.");
}

/// Tells the delegate that a user click will open another app
/// (such as the App Store), backgrounding the current app.
///
/// This is not a reliable callback for an ad click event and is removed in
/// version 8. If you wish to listen to an ad causing a user to leave the app,
/// use applicationWillResignActive: or sceneWillResignActive: instead.
- (void)interstitialWillLeaveApplication:(GADInterstitial *)ad {
  NSLog(@"Interstitial ad will leave application.");
}

v8

Swift

override func viewDidLoad() {
  super.viewDidLoad()
  let request = GADRequest()
  GADInterstitialAd.load(withAdUnitID:"ca-app-pub-8123415297019784/4985798738",
                              request: request,
                    completionHandler: { (ad, error) in
                      if let error = error {
                        print(error.localizedDescription)
                        return
                      }
                      self.interstitial = ad
                      self.interstitial.fullScreenContentDelegate = self
                    }
  )
}

func adDidPresentFullScreenContent(_ ad: GADFullScreenPresentingAd) {
  print("Ad did present full screen content.")
}

func ad(_ ad: GADFullScreenPresentingAd, didFailToPresentFullScreenContentWithError error: Error) {
  print("Ad failed to present full screen content with error \(error.localizedDescription).")
}

func adDidDismissFullScreenContent(_ ad: GADFullScreenPresentingAd) {
  print("Ad did dismiss full screen content.")
}

Objective-C

- (void)viewDidLoad {
  [super viewDidLoad];
  GADRequest *request = [GADRequest request];
  [GADInterstitialAd loadWithAdUnitID:@"ca-app-pub-3940256099942544/44118910"
                              request:request
                    completionHandler:^(GADInterstitialAd *ad, NSError *error) {
    if (error) {
      NSLog(@"interstitial:didFailToReceiveAdWithError: %@", [error localizedDescription])
      return;
    }
    self.interstitial = ad;
    self.interstitial.fullScreenContentDelegate = self;
  }];
}

- (void)adDidPresentFullScreenContent:(id)ad {
  NSLog(@"Ad did present full screen content.");
}

- (void)ad:(id)ad didFailToPresentFullScreenContentWithError:(NSError *)error {
  NSLog(@"Ad failed to present full screen content with error %@.", [error localizedDescription]);
}

- (void)adDidDismissFullScreenContent:(id)ad {
  NSLog(@"Ad did dismiss full screen content.");
}

Diberikan sebagai reward

Muat iklan

v7

Swift

import GoogleMobileAds
import UIKit

class ViewController: UIViewController, GADRewardedAdDelegate {
  /// The rewarded ad.
  var rewardedAd: GADRewardedAd?

  override func viewDidLoad() {
    super.viewDidLoad()
    rewardedAd = GADRewardedAd(adUnitID: "ca-app-pub-3940256099942544/1712485313")
    rewardedAd.delegate = self
    rewardedAd?.load(GADRequest()) { error in
      if let error = error {
        print("Rewarded ad failed to load with error: \(error.localizedDescription)")
      } else {
        print("Rewarded ad loaded.")
      }
    }
  }
}

Objective-C

@import GoogleMobileAds;
@import UIKit;

@interface ViewController () 

@property(nonatomic, strong) GADRewardedAd *rewardedAd;

@end

@implementation ViewController

- (void)viewDidLoad {
  [super viewDidLoad];

  self.rewardedAd = [[GADRewardedAd alloc]
      initWithAdUnitID:@"ca-app-pub-3940256099942544/1712485313"];
  self.rewardedAd.delegate = self;
  GADRequest *request = [GADRequest request];
  [self.rewardedAd loadRequest:request completionHandler:^(GADRequestError * _Nullable error) {
    if (error) {
      NSLog(@"Rewarded ad failed to load with error: %@", [error localizedDescription]);
    } else {
      NSLog(@"Rewarded ad loaded.");
    }
  }];
}

v8

Swift

import GoogleMobileAds
import UIKit

class ViewController: UIViewController, GADFullScreenContentDelegate {
  /// The rewarded ad.
  var rewardedAd: GADRewardedAd?

  override func viewDidLoad() {
  super.viewDidLoad()
  let request = GADRequest()
  GADRewardedAd.load(withAdUnitID: "ca-app-pub-8123415297019784/9501821136",
                          request: request, completionHandler: { (ad, error) in
                            if let error = error {
                              print("Rewarded ad failed to load with error: \(error.localizedDescription)")
                              return
                            }
                            self.rewardedAd = ad
                            self.rewardedAd?.fullScreenContentDelegate = self
                          }
  )
  }
}

Objective-C

@import GoogleMobileAds;
@import UIKit;

@interface ViewController () 

@property(nonatomic, strong) GADRewardedAd *rewardedAd;

@end

@implementation ViewController

- (void)viewDidLoad {
  [super viewDidLoad];

  GADRequest *request = [GADRequest request];
  [GADRewardedAd loadWithAdUnitID:@"ca-app-pub-3940256099942544/1712485313"
                          request:request
                completionHandler:^(GADRewardedAd *ad, NSError *error) {
    if (error) {
      NSLog(@"Rewarded ad failed to load with error: %@", [error localizedDescription]);
      return;
    }
    self.rewardedAd = ad;
    NSLog(@"Rewarded ad loaded.");
    self.rewardedAd.fullScreenContentDelegate = self;
}

Tampilkan iklan dan tangani reward

Iklan reward mengharuskan Anda menangani peristiwa saat pengguna mendapatkan reward. Dengan API GADRewardedAd versi 7, Anda menerapkan rewardedAd:userDidEarnReward: sebagai bagian dari protokol GADRewardedAdDelegate. Untuk versi 8, Anda menerapkan GADUserDidEarnRewardHandler untuk menampilkan iklan.

v7

Swift

func showRewardedAd() {
  ...
  if rewardedAd.isReady {
    rewardedAd.present(fromRootViewController: self delegate:self)
  } else {
    print("Ad wasn't ready")
  }
}

/// Tells the delegate that the user earned a reward.
func rewardedAd(_ rewardedAd: GADRewardedAd, userDidEarnReward: GADAdReward) {
  // TODO: Reward the user.
}

Objective-C

- (void)showRewardedAd: {
  ...
  if (self.rewardedAd.isReady) {
    [self.rewardedAd presentFromRootViewController:self delegate:self];
  } else {
    NSLog(@"Ad wasn't ready");
  }
}

/// Tells the delegate that the user earned a reward.
- (void)rewardedAd:(GADRewardedAd *)rewardedAd userDidEarnReward:(GADAdReward *)reward {
  // TODO: Reward the user.
}

v8

Swift

func showRewardedAd() {
  ...
  if let ad = rewardedAd {
      ad.present(fromRootViewController: self,
               userDidEarnRewardHandler: {
                 let reward = ad.adReward
                 // TODO: Reward the user.
               }
      )
  } else {
    print("Ad wasn't ready")
  }
}

Objective-C

- (void)showRewardedAd: {
  ...
  if (self.rewardedAd) {
    [self.rewardedAd presentFromRootViewController:self
                          userDidEarnRewardHandler:^ {
      GADAdReward *reward = self.rewardedAd.adReward;
      // TODO: Reward the user.
    }];
  } else {
    NSLog(@"Ad wasn't ready");
  }
}

Peristiwa iklan presentasi

Dengan GADRewardedAd API, Anda meneruskan GADRewardedAdDelegate ke metode yang menampilkan iklan. Dengan GADRewardedAd API, Anda menetapkan GADFullscreenContentDelegate sebagai properti pada iklan sebelum menayangkan iklan.

v7

Swift

func showRewardedAd() {
  ...
  if rewardedAd.isReady {
    rewardedAd.present(fromRootViewController: self delegate:self)
  } else {
    print("Ad wasn't ready")
  }
}

/// Tells the delegate that the rewarded ad was presented.
func rewardedAdDidPresent(_ rewardedAd: GADRewardedAd) {
  print("Rewarded ad presented.")
}
/// Tells the delegate that the rewarded ad was dismissed.
func rewardedAdDidDismiss(_ rewardedAd: GADRewardedAd) {
  print("Rewarded ad dismissed.")
}
/// Tells the delegate that the rewarded ad failed to present.
func rewardedAd(_ rewardedAd: GADRewardedAd, didFailToPresentWithError error: Error) {
  print("Rewarded ad failed to present with error: \(error.localizedDescription).")
}

Objective-C

- (void)showRewardedAd: {
  ...
  if (self.rewardedAd.isReady) {
    [self.rewardedAd presentFromRootViewController:self delegate:self];
  } else {
    NSLog(@"Ad wasn't ready");
  }
}

/// Tells the delegate that the rewarded ad was presented.
- (void)rewardedAdDidPresent:(GADRewardedAd *)rewardedAd {
  NSLog(@"Rewarded ad presented.");
}

/// Tells the delegate that the rewarded ad failed to present.
- (void)rewardedAd:(GADRewardedAd *)rewardedAd didFailToPresentWithError:(NSError *)error {
  NSLog(@"Rewarded ad failed to present with error: %@",
        [error localizedDescription]);
}

/// Tells the delegate that the rewarded ad was dismissed.
- (void)rewardedAdDidDismiss:(GADRewardedAd *)rewardedAd {
  NSLog(@"Rewarded ad dismissed.");
}

v8

Swift

override func viewDidLoad() {
  super.viewDidLoad()
  let request = GADRequest()
  GADRewardedAd.load(withAdUnitID: "ca-app-pub-8123415297019784/9501821136",
                          request: request, completionHandler: { (ad, error) in
                            if let error = error {
                              print(error.localizedDescription)
                              return
                            }
                            self.rewardedAd = ad
                            self.rewardedAd?.fullScreenContentDelegate = self
                          }
  )
}

/// Tells the delegate that the rewarded ad was presented.
func adDidPresentFullScreenContent(_ ad: GADFullScreenPresentingAd) {
  print("Rewarded ad presented.")
}
/// Tells the delegate that the rewarded ad was dismissed.
func adDidDismissFullScreenContent(_ ad: GADFullScreenPresentingAd) {
  print("Rewarded ad dismissed.")
}
/// Tells the delegate that the rewarded ad failed to present.
func ad(_ ad: GADFullScreenPresentingAd,
    didFailToPresentFullScreenContentWithError error: Error) {
  print("Rewarded ad failed to present with error: \(error.localizedDescription).")
}

Objective-C

- (void)viewDidLoad {
  [super viewDidLoad];

  GADRequest *request = [GADRequest request];
  [GADRewardedAd loadWithAdUnitID:@"ca-app-pub-3940256099942544/1712485313"
                          request:request
                completionHandler:^(GADRewardedAd *ad, NSError *error) {
    if (error) {
      NSLog(@"Rewarded ad failed to load with error: %@", [error localizedDescription]);
      return;
    }
    self.rewardedAd = ad;
    NSLog(@"Rewarded ad loaded.");
    self.rewardedAd.fullScreenContentDelegate = self;
}

/// Tells the delegate that the rewarded ad was presented.
- (void)adDidPresentFullScreenContent:(id)ad {
  NSLog(@"Rewarded ad presented.");
}

/// Tells the delegate that the rewarded ad failed to present.
- (void)ad:(id)ad
    didFailToPresentFullScreenContentWithError:(NSError *)error {
  NSLog(@"Rewarded ad failed to present with error: %@",
       [error localizedDescription]);
}

/// Tells the delegate that the rewarded ad was dismissed.
- (void)adDidDismissFullScreenContent:(id)ad {
  NSLog(@"Rewarded ad dismissed.");
}

Penghapusan GADRewardedBasedVideoAd API lama

GADRewardedAd API yang lebih baru pertama kali diperkenalkan pada Maret 2019 dan telah menjadi API reward pilihan selama lebih dari 18 bulan. API ini memiliki lebih banyak peningkatan dibandingkan dengan GADRewardedBasedVideoAd API lama, termasuk kemampuan untuk memuat lebih dari satu iklan reward sekaligus.

GADRewardedBasedVideoAd API lama dihapus di SDK versi 8.0.0.

Penghentian banner cerdas dan banner adaptif

Iklan banner cerdas tidak digunakan lagi dan digantikan dengan iklan banner adaptif. Banner adaptif memberikan performa yang unggul dan fleksibilitas yang lebih baik dalam menetapkan lebar iklan. Jika Anda memilih untuk terus menggunakan banner dengan lebar penuh, hal tersebut masih dapat dilakukan menggunakan banner adaptif, seperti yang ditunjukkan dalam cuplikan kode berikut:

Swift

class ViewController: UIViewController {

  override func viewDidAppear(_ animated: Bool) {
    super.viewDidAppear(animated)
    // Note: The safe area is not known until viewWillAppear.
    let adSize = getFullWidthAdaptiveAdSize()
  }

  func getFullWidthAdaptiveAdSize() -> GADAdSize {
    // Here safe area is taken into account, hence the view frame is used after the
    // view has been laid out.
    let frame = { () -> CGRect in
      if #available(iOS 11.0, *) {
        return view.frame.inset(by: view.safeAreaInsets)
      } else {
        return view.frame
      }
    }()
    return GADCurrentOrientationAnchoredAdaptiveBannerAdSizeWithWidth(frame.size.width)
  }
}

Objective-C

@implementation ViewController

- (void)viewDidAppear:(BOOL)animated {
  [super viewDidAppear:animated];
  // Note: The safe area is not known until viewWillAppear.
  GADAdSize adSize = [self getFullWidthAdaptiveAdSize];
}

- (GADAdSize)getFullWidthAdaptiveAdSize {
  CGRect frame = self.view.frame;
  // Here safe area is taken into account, hence the view frame is used after
  // the view has been laid out.
  if (@available(iOS 11.0, *)) {
    frame = UIEdgeInsetsInsetRect(self.view.frame, self.view.safeAreaInsets);
  }
  return GADCurrentOrientationAnchoredAdaptiveBannerAdSizeWithWidth(frame.size.width);
}

@end

Keluar dari penghapusan callback aplikasi

Callback willLeaveApplication untuk semua format iklan telah dihapus dan diganti dengan metode applicationDidEnterBackground: dan sceneDidEnterBackground:. Penggunaan API tingkat OS akan memberi tahu Anda setiap kali pengguna keluar dari aplikasi, terlepas dari apakah masalah tersebut disebabkan oleh interaksi iklan atau tidak.

Perhatikan bahwa callback willLeaveApplication tidak pernah dimaksudkan untuk menjadi pengendali klik iklan, dan mengandalkan callback ini untuk melaporkan klik tidak menghasilkan metrik yang akurat. Misalnya, klik pada ikon AdChoices yang meluncurkan browser eksternal memanggil callback, tetapi tidak menghitung klik.

Penggantian nama class

Tabel di bawah mencantumkan nama class tertentu yang telah diubah atau dihapus di versi 8. Ringkasan:

  • Semua class yang terkait dengan GADUnifiedNativeAd telah diganti namanya menjadi GADNativeAd.
  • GADRewardBasedVideoAd, GADNativeExpressAdView, dan GADInstreamAd telah dihapus.
  • Semua class dengan awalan DFP telah diganti dengan awalan GAM.
Kelas v7.68.0 Kelas v8.0.0
DFPBannerView GAMBannerView
DFPBannerViewOptions GAMBannerViewOptions
DFPInterstitial Iklan GAMInterstitial
DFPRequest GAMRequest
GADRequestError NSError
GADUnifiedNativeAdView GADNativeAdView
GADUnifiedNativeAd GADNativeAd
GADUnifiedNativeAdAssetIdentifiers GADNativeAdAssetIdentifiers
GADUnifiedNativeAdDelegate GADNativeAdDelegate
GADUnifiedNativeAdUnconfirmedClickDelegate GADNativeAdUnconfirmedClickDelegate
GADNativeCustomTemplateAd GADCustomNativeAd
GADNativeCustomTemplateAdLoaderDelegate GADCustomNativeAdLoaderDelegate
GADNativeAdDelegate GADCustomNativeAdDelegate
GADInAppPurchase Dihapus
GADInterstitial GADInterstitialAd
GADNativeExpressAdView Dihapus
GADRewardBasedVideoAd Dihapus
GADInstreamAd Dihapus
GADInstreamAdView Dihapus

Metode dihapus/diganti

Tabel di bawah mencantumkan perubahan spesifik dalam versi 8. Ringkasan:

  • Metode dan properti yang sebelumnya tidak digunakan lagi telah dihapus.
  • -willLeaveApplication: metode delegasi telah dihapus untuk semua format.
  • Nama kelas jaringan iklan telah dipindahkan ke properti GADResponseInfo.
  • ID perangkat pengujian telah dipindahkan ke properti GADRequestConfiguration.
Kelas v7.68.0 API v7.68.0 API v8.0.0 Catatan
GADMobileAds +configureWithApplicationID: {i>-startWithCompletionHandler:<i} ID aplikasi sekarang ditetapkan di Info.plist.
+disableAutomatedInAppPurchaseReporting -disableAutomatedInAppPurchaseReporting
+disableSDKCrashReporting -disableSDKCrashReporting
GADRequest testDevices GADRequestConfiguration.testdeviceidentifiers Properti testDeviceIdentifiers berlaku untuk semua permintaan iklan, sedangkan properti testDevices lama ditetapkan per permintaan.
gender Dihapus
ulang tahun Dihapus
+sdkVersion GADMobileAds.sharedInstance.sdkVersion
-setBirthdayWithMonth:day:year: Dihapus
{i>-setLocationWithDescription:<i} -setLocationWithLatitude:longitude:accuracy:
-tagForChildDirectedTreatment: [GADMobileAds.sharedInstance.requestConfiguration tagForChildDirectedTreatment]
GADErrorCode kGADError* GADError* Awalan k dihapus dari semua konstanta kode error.
GADBannerView hasAutoRefreshed autoloadEnabled
inAppPurchaseDelegate Dihapus
mediatedAdView Dihapus
adNetworkClassName responseInfo.adNetworkClassName
DFPBannerView -setValidAdSizesWithSizes: -setValidAdSizes:
DFPBannerViewOptions {i>-adSizeDelegate<i} Dihapus
GADBannerViewDelegate -adViewDidReceiveAd: -bannerViewDidReceiveAd:
-adView:didFailToReceiveAdWithError: -bannerView:didFailToReceiveAdWithError:
{i>-adView WillPresentScreen:<i} -bannerViewWillPresentScreen:
{i>-adView {/5}DismissScreen:<i} -bannerViewWillDismissScreen:
-adViewDidDismissScreen: -bannerViewDidDismissScreen:
{i>-adViewAllowLeadApplication<i}: Dihapus
GADNativeCustomTemplateAd templateID GADCustomNativeAd.formatID
-performClickOnAssetWithKey:customClickHandler: -performClickOnAssetWithKey:
GADNativeAdImageAdLoaderOptions preferredImageOrientation GADNativeAdMediaAdOptions.mediaAspectRatio
GADInterstitial inAppPurchaseDelegate Dihapus
isReady Dihapus Sebagai gantinya, gunakan canPresentFromRootViewController:error:.
hasBeenUsed Dihapus
-init {i>-initWithAdUnitID<i}:
-setAdUnitID: {i>-initWithAdUnitID<i}:
adNetworkClassName responseInfo.adNetworkClassName
-interstitial WillKeluar dari Aplikasi: Dihapus
GADUnifiedNativeAd videoController mediaContent.videoController
adNetworkClassName responseInfo.adNetworkClassName