Миграция 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