SDK 遷移

此頁面涵蓋當前和以前版本的遷移。

遷移到 SDK v9

Google 移動廣告 SDK 版本 9.0.0 將於 2022 年初推出,其中包含一些重大更改,以及一些 API 重命名和已棄用的 API 的刪除。本指南概述了這些更改以及使用我們最新的 SDK 使您的應用程序更新的最佳實踐。

廣告在 iOS 10 上停止投放

Google 移動廣告 SDK 9.0.0 版支持的最低 iOS 版本是 iOS 11。

升級到 Google Mobile Ads SDK 9.0.0 版不會破壞您在 iOS 10 設備上的應用,但不會在這些設備上投放廣告。

更嚴格地執行狀態欄控制

從 9.0.0 版本開始,當您展示全屏格式的廣告時,您的應用應確保廣告能夠控制狀態欄的展示。如果您不這樣做,您將在日誌中看到一條錯誤消息。

根據應用中視圖控制器的特定佈局,您可能不需要進行任何更改來確保這一點,但在某些情況下,您可能需要設置父視圖控制器的modalPresentationCapturesStatusBarAppearance屬性。

將 adDidPresentFullScreenContent: 重命名為 adWillPresentFullScreenContent:

沒有行為改變。委託方法在廣告即將展示之前被調用,因此新方法名稱更好地反映了它的功能。

刪除 GADRequest 上的位置設置 API

- (void)setLocationWithLatitude:longitude:accuracy:已從GADRequest中刪除,因為 Google 不使用位置數據來定位廣告。如果需要,使用第三方 API 將信息提供給第三方廣告網絡。

自定義事件接口棄用

自定義事件使使用 AdMob 中介的發布商可以為不屬於受支持的廣告網絡之一的廣告網絡添加瀑布中介。

所有自定義事件協議均已棄用。相反,使用現有的GADMediationAdapterGADMediationAdEventDelegate協議來實現相同的功能。此更改提高了清晰度,並使您能夠為以前不可用的獎勵廣告和滾動廣告創建自定義事件。

蜜蜂

下表列出了應從版本 9.0.0 開始使用的自定義事件 API 的相應中介適配器 API。

v8 v9
GADCustomEventBanner
GADCustomEvent 插頁式
GADCustomEventNativeAd
GAD中介適配器GAD中介橫幅廣告
GAD 中介插頁式廣告
GADMediationInterscrollerAd
GAD 中介獎勵廣告
GADMediationNativeAd
delegate委託由GADMediationAdapter類的每個加載函數的加載完成處理程序返回
-init -init
-requestBannerAd:parameter: label:request: -loadBannerForAdConfiguration: completionHandler:
-requestInterstitialAdWith Parameter:label:request: -loadInterstitialFor AdConfiguration: completionHandler:
-requestNativeAdWithParameter: request:adTypes:options: rootViewController: -loadNativeAdFor AdConfiguration: completionHandler:
不適用-loadInterscrollerAdFor AdConfiguration: completionHandler:
不適用-loadRewardedAdFor AdConfiguration: completionHandler:
-presentFromRootViewController: -presentFromViewController:
-handlesUserClicks -handlesUserClicks
-handlesUserImpressions -handlesUserImpressions

委託方法

下表列出了從版本 9.0.0 開始應使用的自定義事件委託方法的相應中介廣告事件委託方法。

v8 v9
GADCustomEventBannerDelegate
GADCustomEventInterstitialDelegate
GADCustomEventNativeAdDelegate
GADMediationAdEventDelegate GAD中介廣告
-customEventBanner:didReceiveAd:
-customEventInterstitialDidReceiveAd:
廣告加載狀態包含在GADMediationAdapter類中每個加載函數的加載完成處理程序中
-customEventBanner:didFailAd:
-customEventInterstitial:didFailAd:
-customEventBannerWasClicked:
-customEventInterstitialWasClicked:
-reportClick
-customEventBannerWill PresentModal:
-customEventInterstitialWill PresentModal:
-willPresentFullScreenView
-customEventBannerWill DismissModal:
-customEventInterstitialWill DismissModal:
-willDismissFullScreenView
-customEventBannerDid DismissModal:
-customEventInterstitialDid DismissModal:
-didDismissFullScreenView
-customEventBannerWill LeaveApplication:
-customEventInterstitialWill LeaveApplication:
-willBackgroundApplication
viewControllerFor PresentingModalView -[GADMediationBannerAd view]

其他刪除/替換的方法和常量

方法、常量或屬性的更改
kGAD-前綴常量已移除。使用GAD-前綴的常量。
GADAdNetworkResponseInfo credentials被刪除。請改用adUnitMapping
GADCustomNativeAd GADCustomNativeAd中的mediaView已棄用。請改用mediaContent
GoogleMobileAds中的應用內購買 API inAppPurchase中的GoogleMobileAds API 已被移除。

遷移到 SDK v8

Google 移動廣告 SDK 8.0.0 版引入了一些重大更改以及 API 的一些重命名和刪除。

全屏格式 API 更新

從 8.0.0 版開始,插頁式廣告和激勵廣告共享通用的全屏廣告樣式,以提高一致性。這些新的全屏廣告 API 與第 7 版全屏廣告 API 有兩個主要區別:

  1. 靜態類方法load

    之前加載/顯示全屏廣告的方法如下:

    1. 創建一個廣告對象實例並保存對它的引用。
    2. 分配一個處理加載和顯示回調的委託。
    3. 加載廣告。
    4. 檢查是否使用isReady加載了廣告。
    5. 展示廣告。

    在版本 8 中,方法略有變化。加載回調不再是委託的一部分。相反,它們作為完成處理程序傳遞給load方法:

    1. 在廣告類上調用靜態加載方法並提供加載完成處理程序。
    2. 在加載完成回調中,保留對返回的已加載廣告的引用。
    3. 分配一個處理顯示回調的委託。
    4. 展示廣告。

    新方法提供了以下好處:

    • 您永遠不會引用未加載的廣​​告。
    • 您不必在加載廣告對象時抓住它。
  2. 一致的廣告事件。

    事件類型現有 API v8 API
    加載事件GADInterstitialDelegateGADRewardedAdDelegate GADInterstitialAdLoadCompletionHandlerGADRewardedAdLoadCompletionHandler
    演講活動GADFullScreenContentDelegate

    以前,要偵聽任何廣告事件,您需要將實現GADInterstitialDelegate協議的類註冊到插頁式廣告的委託屬性,或將實現GADRewardedAdDelegate協議的類註冊到獎勵廣告的委託屬性,具體取決於您使用的格式。這個委託具有與廣告的加載和展示生命週期相關的方法。

    在版本 8 中,加載和演示事件是分開的。您現在可以在顯示廣告之前隨時註冊GADFullScreenContentDelegate ,而無需在加載廣告之前設置單個委託。特定於每種格式的廣告加載事件移動到加載方法中傳遞的單個加載完成處理程序。

插頁式

加載廣告

下面的代碼片段向您展示瞭如何加載插頁式廣告並在廣告加載成功或失敗時監聽事件。

v7

迅速

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

迅速

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;
  }];
}

展示廣告

v7

迅速

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

迅速

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");
  }
}

演示廣告活動

下面的代碼片段向您展示瞭如何處理廣告展示(成功或不成功)和關閉時的回調。

v7

迅速

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

迅速

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.");
}

獎勵

加載廣告

v7

迅速

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

迅速

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;
}

展示廣告並處理獎勵

獎勵廣告要求您在用戶獲得獎勵時處理事件。使用GADRewardedAd API 的第 7 版,您實現rewardedAd:userDidEarnReward:作為GADRewardedAdDelegate協議的一部分。對於版本 8,您實現GADUserDidEarnRewardHandler以展示廣告。

v7

迅速

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

迅速

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");
  }
}

演示廣告活動

使用GADRewardedAd API,您可以將GADRewardedAdDelegate傳遞給呈現廣告的方法。使用GADRewardedAd API,您可以在展示廣告之前將GADFullscreenContentDelegate設置為廣告的屬性。

v7

迅速

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

迅速

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.");
}

舊版 GADRewardedBasedVideoAd API 移除

較新的GADRewardedAd API 於 2019 年 3 月首次推出,18 個月以來一直是首選的獎勵 API。與舊版GADRewardedBasedVideoAd API 相比,它具有更多增強功能,包括一次加載多個獎勵廣告的能力。

SDK 版本 8.0.0 中刪除了舊版GADRewardedBasedVideoAd API。

智能橫幅棄用,支持自適應橫幅

智能橫幅廣告已棄用,取而代之的是自適應橫幅廣告。自適應橫幅在設置廣告寬度方面提供了卓越的性能和更大的靈活性。如果您希望繼續使用全角橫幅,仍然可以使用自適應橫幅來完成,如以下代碼片段所示:

迅速

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

離開應用程序回調刪除

所有廣告格式的 willLeaveApplication 回調已被移除,取而代之的是applicationDidEnterBackground:sceneDidEnterBackground:方法。每當用戶離開您的應用時,無論是否由於廣告交互,使用操作系統級 API 都會通知您。

請注意, willLeaveApplication 回調從未打算用作廣告點擊處理程序,並且依靠此回調來報告點擊並不能產生準確的指標。例如,點擊啟動外部瀏覽器的 AdChoices 圖標會調用回調,但不計入點擊次數。

類重命名

下表列出了版本 8 中已更改或刪除的特定類名稱。總結:

  • 所有與GADUnifiedNativeAd相關的類都已重命名為GADNativeAd
  • GADRewardBasedVideoAdGADNativeExpressAdViewGADInstreamAd已被移除。
  • 所有帶有DFP前綴的類都已替換為GAM前綴。
v7.68.0 類v8.0.0 類
DFP橫幅視圖GAM橫幅視圖
DFPBannerViewOptions GAMBannerViewOptions
DFP 插頁式廣告GAM 插頁式廣告
DFP 請求GAM請求
GADRequestError NS錯誤
GADUnifiedNativeAdView GADNativeAdView
GADUnifiedNativeAd GADNativeAd
GADUnifiedNativeAdAssetIdentifiers GADNativeAdAssetIdentifiers
GADUnifiedNativeAdDelegate GADNativeAdDelegate
GADUnifiedNativeAdUnconfirmedClickDelegate GADNativeAdUnconfirmedClickDelegate
GADNativeCustomTemplateAd GADCustomNativeAd
GADNativeCustomTemplateAdLoaderDelegate GADCustomNativeAdLoaderDelegate
GADNativeAdDelegate GADCustomNativeAdDelegate
GADInApp購買已移除
GAD 插頁式GAD 插頁式廣告
GADNativeExpressAdView已移除
GADRewardBased 視頻廣告已移除
GADInstreamAd已移除
GADInstreamAdView已移除

刪除/替換的方法

下表列出了版本 8 中的具體更改。總結:

  • 以前不推薦使用的方法和屬性已被刪除。
  • -willLeaveApplication:已刪除所有格式的委託方法。
  • 廣告網絡類名稱已移至GADResponseInfo屬性。
  • 測試設備標識符已移至GADRequestConfiguration屬性。
v7.68.0 類v7.68.0 API v8.0.0 API筆記
GAD移動廣告+configureWithApplicationID: -startWithCompletionHandler:應用 ID 現在在 Info.plist 中設置。
+disableAutomatedInApp PurchaseReporting -disableAutomatedInApp PurchaseReporting
+disableSDK 崩潰報告-disableSDKCrashReporting
GAD請求測試設備GADRequestConfiguration .testdeviceidentifiers testDeviceIdentifiers 屬性適用於所有廣告請求,而舊的 testDevices 屬性是按請求設置的。
性別已移除
生日已移除
+sdkV版本GADMobileAds.sharedInstance .sdkVersion
-setBirthday WithMonth:day:year:已移除
-setLocationWithDescription: -setLocationWith 緯度:經度:準確度:
-tagForChildDirectedTreatment: [GADMobileAds.sharedInstance.requestConfiguration tagForChildDirectedTreatment]
GAD錯誤代碼kGAD誤差* GAD錯誤* k前綴從所有錯誤代碼常量中刪除。
GAD橫幅視圖已自動刷新自動加載啟用
inAppPurchaseDelegate已移除
中介 AdView已移除
adNetworkClassName responseInfo .adNetworkClassName
DFP橫幅視圖-setValidAdSizesWithSizes: -setValidAdSizes:
DFPBannerViewOptions -adSizeDelegate已移除
GADBannerViewDelegate -adViewDidReceiveAd: -bannerViewDidReceiveAd:
-adView:didFailToReceive AdWithError: -bannerView:didFailToReceive AdWithError:
-adViewWillPresentScreen: -bannerViewWillPresentScreen:
-adViewWillDismissScreen: -bannerViewWillDismissScreen:
-adViewDidDismissScreen: -bannerViewDidDismissScreen:
-adViewWillLeaveApplication:已移除
GADNativeCustomTemplateAd模板ID GADCustomNativeAd.formatID
-performClickOnAssetWithKey:customClickHandler: -performClickOnAssetWithKey:
GADNativeAdImage廣告加載器選項首選圖像方向GADNativeAdMediaAdOptions .mediaAspectRatio
GAD 插頁式inAppPurchaseDelegate已移除
準備好了已移除使用 canPresentFrom RootViewController:error: 代替。
已經用過已移除
-在裡面-initWithAdUnitID:
-setAdUnitID: -initWithAdUnitID:
adNetworkClassName responseInfo .adNetworkClassName
-interstitialWill LeaveApplication:已移除
GADUnifiedNativeAd視頻控制器mediaContent.videoController
adNetworkClassName responseInfo .adNetworkClassName