Di chuyển SDK

Trang này trình bày cách di chuyển đối với phiên bản hiện tại và phiên bản trước đây của Google SDK quảng cáo trên thiết bị di động dành cho iOS. Phiên bản 11.0.0 dự kiến sẽ ra mắt vào tháng 1 2024.

Chuyển từ SDK phiên bản 10 sang phiên bản 11

Mục tiêu triển khai tối thiểu

Mục tiêu triển khai tối thiểu đã được tăng lên iOS 12.

Phiên bản Xcode tối thiểu

Phiên bản Xcode được hỗ trợ tối thiểu đã tăng lên 15.1.

Ngừng phân phát quảng cáo trên iOS 12

SDK Quảng cáo của Google trên thiết bị di động phiên bản 11.0.0 chỉ phân phát quảng cáo trên các thiết bị chạy iOS 13 và cao hơn.

Đã xoá phần phụ thuộc GoogleAppMeasurement

Trong phiên bản 11.0.0, phần phụ thuộc trên GoogleAppMeasurement đã được đã bị xóa. Phần phụ thuộc này hỗ trợ nút bật/tắt chỉ số người dùng trong AdMob sẽ là ngừng cung cấp vào đầu năm 2024. Người nhận tiếp tục thu thập chỉ số người dùng trong AdMob, liên kết ứng dụng AdMob với Firebase và tích hợp SDK Google Analytics cho Firebase vào ứng dụng của bạn.

Các thay đổi đối với cách trình bày quảng cáo toàn màn hình

Những thay đổi sau ảnh hưởng đến các định dạng quảng cáo khác nhau:

  • Quảng cáo khi mở ứng dụng
  • Quảng cáo xen kẽ
  • Được thưởng
  • Quảng cáo xen kẽ có tặng thưởng

Tham số trình điều khiển chế độ xem trong -canPresentFromRootViewController:error:-presentFromRootViewController: có tính chất rỗng. Nếu giá trị rỗng được chuyển qua, quảng cáo sẽ được hiển thị từ bộ điều khiển chế độ xem trên cùng trong hệ phân cấp bộ điều khiển chế độ xem.

Đã xoá phương thức

Các phương thức sau đây sẽ bị xoá.

Loại phiên bản 11.0.0 Phương thức Ghi chú
GADAppOpenAd load(withAdUnitID adUnitID: String, request: GADRequest?, orientation: UIInterfaceOrientation) Thay vào đó, hãy sử dụng load(withAdUnitID adUnitID: String, request: GADRequest?).
GADMediationBannerAdEventDelegate willBackgroundApplication() Không có thiết bị thay thế.
GADMediationInterstitialAdEventDelegate willBackgroundApplication() Không có thiết bị thay thế.
GADMediationNativeAdEventDelegate willBackgroundApplication() Không có thiết bị thay thế.
GADMediationRewardedAdEventDelegate didRewardUser(with reward: GADAdReward) Thay vào đó, hãy sử dụng didRewardUser().
GADMediatedUnifiedNativeAdNotificationSource mediatedNativeAdWillLeaveApplication(_ mediatedNativeAd: GADMediatedUnifiedNativeAd) Không có thiết bị thay thế.
GADRequestConfiguration setSameAppKeyEnabled(_ enabled: Bool) Thay vào đó, hãy sử dụng setPublisherFirstPartyIDEnabled(_ enabled: Bool).
tagForUnderAge(ofConsent underAgeOfConsent: Bool) Thay vào đó, hãy dùng thuộc tính tagForUnderAgeOfConsent.
tag(forChildDirectedTreatment childDirectedTreatment: Bool) Thay vào đó, hãy dùng thuộc tính tagForChildDirectedTreatment.

Đã xoá các cơ sở lưu trú

Các thuộc tính sau đã bị xoá.

Lớp phiên bản 11.0.0 Thuộc tính Ghi chú
GADMediationAdConfiguration hasUserLocation Không có thiết bị thay thế.
userLatitude
userLongitude
userLocationAccuracyInMeters
childDirectedTreatment Thay vào đó, hãy sử dụng GADMobileAds.sharedInstance.requestConfiguration.tagForChildDirectedTreatment.
GADResponseInfo adNetworkClassName Thay vào đó, hãy sử dụng adNetworkClassName từ loadedAdNetworkResponseInfo.

Đã xóa GADAdFormatUnknown

Đã xoá GADAdFormatUnknown mà không có lựa chọn thay thế.

Các thay đổi đối với phiên bản SDK ghi nhật ký

Phiên bản 11.0.0 sẽ xoá sdkVersion. Để ghi lại phiên bản SDK Quảng cáo của Google trên thiết bị di động, sử dụng versionNumber thay thế.

Phiên bản 10.0.0

GADMobileAds.sharedInstance().sdkVersion

Phiên bản 11.0.0

GADGetStringFromVersionNumber(GADMobileAds.sharedInstance().versionNumber)

Các thay đổi đối với việc xử lý lỗi Crashlytics

Kể từ phiên bản 11.0.0, GADAdLoader sẽ không thực hiện yêu cầu quảng cáo nếu giá trị đó delegate không phù hợp với loại quảng cáo được yêu cầu giao thức uỷ quyền. Trước đây, không thành công sau khi thực hiện yêu cầu quảng cáo.

Các thay đổi đối với hành vi kiểm thử

Xem bảng trình bày các điều kiện mới cập nhật cho thời điểm thuộc tính sẽ trả về true.

Lớp Thuộc tính
GADMediationAdConfiguration isTestRequest
GADCustomEventRequest isTesting
v10.0.0 v11.0.0
  • Thiết bị này được khai báo rõ ràng là thiết bị thử nghiệm trong testDeviceIdentifiers.
  • Thiết bị này được khai báo rõ ràng là thiết bị thử nghiệm trong testDeviceIdentifiers.
  • Thiết bị đó là một trình mô phỏng.
  • Thiết bị này được khai báo rõ ràng là thiết bị thử nghiệm trong AdMob giao diện người dùng.

Chuyển từ phiên bản 9 sang phiên bản 10

Ngừng phân phát quảng cáo trên iOS 11

SDK quảng cáo trên thiết bị di động của Google phiên bản 10.0.0 chỉ phân phát quảng cáo trên các thiết bị chạy iOS 12 trở lên.

Việc nâng cấp lên SDK quảng cáo trên thiết bị di động của Google phiên bản 10.0.0 sẽ không khiến ứng dụng của bạn gặp lỗi trên các thiết bị iOS 11 và iOS 10. Tuy nhiên, quảng cáo sẽ không được phân phát trên các thiết bị đó.

Việc xây dựng chương trình bằng mã bit không còn được hỗ trợ nữa

Hiện tại, bạn phải tắt mã bit trong ứng dụng di động để tích hợp SDK quảng cáo trên thiết bị di động của Google.

Các loại đã bị xoá

Loại Ghi chú
GADGender Không có loại thay thế.
GADMRewardBasedVideoAdNetworkAdapterProtocol Tất cả các bộ chuyển đổi dàn xếp có tặng thưởng được nêu trong mục Chọn mạng đã ngừng sử dụng các giao thức này trong hơn một năm qua. Hãy sử dụng GADMediationAdapter cho hoạt động dàn xếp và các sự kiện tuỳ chỉnh.
GADMRewardBasedVideoAdNetworkConnectorProtocol

Các thuộc tính đã bị xoá

Các thuộc tính sau đã bị xoá mà không có thuộc tính thay thế.

Lớp phiên bản 10.0.0 Tài sản
GADMediationAdRequest userBirthday
userGender
userHasLocation
userLatitude
userLongitude
userLocationAccuracyInMeters
userLocationDescription
GADCustomEventRequest userHasLocation
userLatitude
userLongitude
userLocationAccuracyInMeters
userLocationDescription

Chuyển từ phiên bản 8 sang phiên bản 9

Ngừng phân phát quảng cáo trên iOS 10

SDK quảng cáo trên thiết bị di động của Google phiên bản 9.0.0 hỗ trợ iOS 11 trở lên.

Việc nâng cấp lên SDK quảng cáo trên thiết bị di động của Google phiên bản 9.0.0 sẽ không khiến ứng dụng của bạn gặp lỗi trên các thiết bị iOS 10. Tuy nhiên, quảng cáo sẽ không được phân phát trên các thiết bị đó.

Thực thi nghiêm ngặt hơn việc kiểm soát thanh trạng thái

Kể từ phiên bản 9.0.0 trở lên, khi hiển thị quảng cáo ở định dạng toàn màn hình, ứng dụng của bạn phải đảm bảo rằng quảng cáo có thể kiểm soát việc hiển thị thanh trạng thái. Bạn sẽ thấy thông báo lỗi trong nhật ký nếu không đảm bảo được điều này.

Tuỳ thuộc vào bố cục cụ thể của trình kiểm soát khung hiển thị trong ứng dụng của mình, bạn có thể không cần thực hiện bất kỳ thay đổi nào để đảm bảo điều này. Hãy cân nhắc xem bạn có cần đặt thuộc tính childViewControllerForStatusBarHidden trên rootViewController của quảng cáo hay không.

Đổi tên adDidPresentFullScreenContent: thành adWillPresentFullScreenContent:

Không có thay đổi nào về hành vi. Phương thức uỷ quyền được gọi ngay trước khi quảng cáo sắp xuất hiện, vì vậy, tên phương thức mới sẽ phản ánh tốt hơn chức năng của quảng cáo.

Xoá API cài đặt vị trí trên AAAA

- (void)setLocationWithLatitude:longitude:accuracy: đã bị xoá khỏi GADRequest vì Google không sử dụng dữ liệu vị trí để nhắm mục tiêu quảng cáo. Nếu bắt buộc, hãy sử dụng API của bên thứ ba để cung cấp thông tin cho các mạng quảng cáo của bên thứ ba.

Ngừng sử dụng giao diện sự kiện tuỳ chỉnh

Sự kiện tuỳ chỉnh cho phép nhà xuất bản sử dụng AdMob Dàn xếp để thêm chế độ dàn xếp kiểu thác nước cho một mạng quảng cáo không thuộc danh sách mạng quảng cáo được hỗ trợ.

Tất cả giao thức sự kiện tuỳ chỉnh đều ngừng hoạt động. Thay vào đó, hãy sử dụng các giao thức GADMediationAdapterGADMediationAdEventDelegate hiện có để có được các chức năng tương tự. Thay đổi này giúp rõ ràng hơn và cho phép bạn tạo sự kiện tuỳ chỉnh cho quảng cáo có tặng thưởng và quảng cáo kiểu cuộn xen kẽ mà trước đây chưa được hỗ trợ.

API

Bảng bên dưới liệt kê những API bộ chuyển đổi dàn xếp tương ứng với API sự kiện tuỳ chỉnh mà bạn nên sử dụng kể từ phiên bản 9.0.0.

v8 v9
GADCustomEventBanner
GADCustomEventInterstitial
GADCustomEventPlace
GADMediationAdapter LegitScriptBannerAd
GADMediationInterstitialAd
GADMediationInterscrollerAd
GADMediationRewardedAd
GADMediationLegitScript
delegate Trình xử lý hoàn thành tải của mỗi hàm tải của lớp GADMediationAdapter sẽ trả về chế độ uỷ quyền
-init -init
-requestBannerAd:parameter:label:request: -loadBannerForAdConfiguration:completionHandler:
-requestInterstitialAdWithParameter:label:request: -loadInterstitialForAdConfiguration:completionHandler:
-requestNativeAdWithParameter:request:adTypes:options:rootViewController: -loadNativeAdForAdConfiguration:completionHandler:
Không áp dụng -loadInterscrollerAdForAdConfiguration:completionHandler:
Không áp dụng -loadRewardedAdForAdConfiguration:completionHandler:
-presentFromRootViewController: -presentFromViewController:
-handlesUserClicks -handlesUserClicks
-handlesUserImpressions -handlesUserImpressions

Phương thức uỷ quyền

Bảng bên dưới liệt kê những phương thức uỷ quyền sự kiện quảng cáo dàn xếp tương ứng với các phương thức uỷ quyền sự kiện tuỳ chỉnh mà bạn nên sử dụng kể từ phiên bản 9.0.0.

v8 v9
GADCustomEventBannerdelegate
GADCustomEventInterstitialImpression
GADCustomEventPlacedelegate
GADMediationAdEventDelegate LegitScriptAd
-customEventBanner:didReceiveAd:
-customEventInterstitialDidReceiveAd:
Trạng thái tải quảng cáo được đưa vào trình xử lý hoàn thành lượt tải của mỗi hàm tải trong lớp GADMediationAdapter
-customEventBanner:didFailAd:
-customEventInterstitial:didFailAd:
-customEventBannerWasClicked:
-customEventInterstitialWasClicked:
-reportClick
-customEventBannerWillPresentModal:
-customEventInterstitialWillPresentModal:
-willPresentFullScreenView
-customEventBannerWillDismissModal:
-customEventInterstitialWillDismissModal:
-willDismissFullScreenView
-customEventBannerDidDismissModal:
-customEventInterstitialDidDismissModal:
-didDismissFullScreenView
-customEventBannerWillLeaveApplication:
-customEventInterstitialWillLeaveApplication:
-willBackgroundApplication
viewControllerForPresentingModalView -[GADMediationBannerAd view]

Các phương thức và hằng số đã bị xoá/thay thế khác

Những thay đổi đối với phương thức, hằng số hoặc thuộc tính
Hằng số có tiền tố kGAD- Đã xoá. Sử dụng hằng số có tiền tố GAD-.
GADAdNetworkResponseInfo Xoá credentials. Thay vào đó, hãy sử dụng adUnitMapping.
GAMRequest Ngừng sử dụng kGAMSimulatorID trong GAMRequest. Thay vào đó, hãy sử dụng GADSimulatorID trong GADRequestConfiguration.
GADCustomNativeAd Ngừng sử dụng mediaView trong GADCustomNativeAd. Thay vào đó, hãy sử dụng mediaContent.
API mua hàng trong ứng dụng trong GoogleMobileAds Các API inAppPurchase trong GoogleMobileAds đã bị xoá.

Chuyển từ phiên bản 7 sang phiên bản 8

SDK Quảng cáo của Google trên thiết bị di động phiên bản 8.0.0 có một số thay đổi lớn cũng như một số đổi tên và xoá API.

Nội dung cập nhật về API theo định dạng toàn màn hình

Kể từ phiên bản 8.0.0, quảng cáo xen kẽ và quảng cáo có tặng thưởng sẽ có chung một phiên bản đầy đủ kiểu quảng cáo trên màn hình để tăng tính nhất quán. Các API quảng cáo toàn màn hình mới này có 2 những điểm khác biệt lớn so với API quảng cáo toàn màn hình của phiên bản 7:

  1. Phương thức lớp tĩnh load.

    Trước đây, để tải/hiển thị quảng cáo toàn màn hình, hãy làm như sau:

    1. Tạo một phiên bản đối tượng quảng cáo và giữ tệp tham chiếu đến đối tượng đó.
    2. Chỉ định một đại diện xử lý các lệnh gọi lại tải và hiển thị.
    3. Tải một quảng cáo.
    4. Kiểm tra xem quảng cáo có được tải bằng cách sử dụng isReady hay không.
    5. Hiển thị quảng cáo.

    Trong phiên bản 8, cách thực hiện có thay đổi đôi chút. Các lệnh gọi lại tải không còn được sử dụng của người được uỷ quyền. Thay vào đó, chúng được chuyển vào phương thức load dưới dạng trình xử lý hoàn thành:

    1. Gọi một phương thức tải tĩnh trên lớp quảng cáo và cung cấp kết quả tải trình xử lý.
    2. Trong lệnh gọi lại tải hoàn thành, hãy giữ tham chiếu đến quảng cáo đã tải bị trả lại.
    3. Chỉ định một đại biểu xử lý các lệnh gọi lại hiển thị.
    4. Hiển thị quảng cáo.

    Phương pháp tiếp cận mới mang lại những lợi ích sau:

    • Bạn sẽ không bao giờ có tệp tham chiếu đến quảng cáo chưa được tải.
    • Bạn không phải giữ một đối tượng quảng cáo trong khi đối tượng đó đang tải.
  2. Sự kiện quảng cáo nhất quán.

    Loại sự kiện API hiện có API phiên bản 8
    Sự kiện tải GADInterstitialDelegate hoặc GADRewardedAdDelegate GADInterstitialAdLoadCompletionHandler hoặc GADRewardedAdLoadCompletionHandler
    Sự kiện trình bày GADFullScreenContentDelegate

    Trước đây, để theo dõi mọi sự kiện quảng cáo, bạn cần đăng ký một lớp triển khai GADInterstitialDelegate giao thức với thuộc tính uỷ quyền của quảng cáo xen kẽ hoặc đăng ký một lớp triển khai GADRewardedAdDelegate giao thức cho thuộc tính đại biểu của quảng cáo có tặng thưởng tuỳ thuộc vào định dạng mà bạn đang dùng. Cùng một đại diện này có các phương thức liên quan đến cả việc tải và vòng đời trình bày của quảng cáo.

    Với phiên bản 8, các sự kiện tải và trình bày là riêng biệt. Bạn có thể làm điều này ngay bây giờ đăng ký một GADFullScreenContentDelegate bất kỳ lúc nào trước khi hiển thị quảng cáo thay vì bắt buộc phải đặt uỷ quyền trước khi tải quảng cáo. Sự kiện tải quảng cáo, dành riêng cho từng hãy chuyển sang một trình xử lý hoàn thành tải được chuyển vào phương thức tải.

Quảng cáo xen kẽ

Tải quảng cáo

Các đoạn mã dưới đây cho bạn biết cách tải một quảng cáo xen kẽ và lắng nghe các sự kiện khi quảng cáo tải thành công hoặc không tải được.

phiên bản 7

Swift

import GoogleMobileAds
import UIKit

class ViewController: UIViewController, GADInterstitialDelegate {

  var interstitial: GADInterstitial!

  override func viewDidLoad() {
    super.viewDidLoad()
    interstitial = GADInterstitial(adUnitID: "ca-app-pub-3940256099942544/4411468910")
    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/4411468910"];
  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/4411468910"
                              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;
  }];
}

Quảng cáo hiển thị hình ảnh

phiên bản 7

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

Sự kiện quảng cáo bản trình bày

Các đoạn mã dưới đây cho bạn biết cách xử lý lệnh gọi lại khi quảng cáo hiện ra (thành công hoặc không thành công) và khi bị loại bỏ.

phiên bản 7

Swift

override func viewDidLoad() {
  super.viewDidLoad()
  interstitial = GADInterstitial(adUnitID: "ca-app-pub-3940256099942544/4411468910")
  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/4411468910"];
  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/4411468910"
                              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.");
}

Được thưởng

Tải quảng cáo

phiên bản 7

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

Quảng cáo hiển thị và xử lý phần thưởng

Quảng cáo có tặng thưởng yêu cầu bạn xử lý sự kiện khi người dùng nhận được phần thưởng. Bằng phiên bản 7 của API GADRewardedAd, bạn triển khai rewardedAd:userDidEarnReward: trong giao thức GADRewardedAdDelegate. Đối với phiên bản 8, bạn triển khai GADUserDidEarnRewardHandler để hiển thị quảng cáo.

phiên bản 7

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

Sự kiện quảng cáo bản trình bày

Với API GADRewardedAd, bạn truyền GADRewardedAdDelegate vào phương thức hiển thị quảng cáo. Với API GADRewardedAd, bạn đặt một GADFullscreenContentDelegate làm thuộc tính trên quảng cáo trước khi hiển thị quảng cáo.

phiên bản 7

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

Xóa API GADGeneratedBasedVideoAd cũ

Càng mới GADRewardedAd API được ra mắt lần đầu vào tháng 3 năm 2019 và là loại API có tặng thưởng được ưa thích API trong hơn 18 tháng. Phiên bản này có nhiều tính năng nâng cao hơn so với phiên bản cũ API GADRewardedBasedVideoAd, bao gồm khả năng tải nhiều API quảng cáo có tặng thưởng cùng lúc.

API GADRewardedBasedVideoAd cũ sẽ bị xoá trong SDK phiên bản 8.0.0.

Biểu ngữ thông minh sẽ ngừng hoạt động và thay vào đó là biểu ngữ thích ứng

Quảng cáo Biểu ngữ thông minh đã ngừng hoạt động ưu tiên biểu ngữ thích ứng quảng cáo. Biểu ngữ thích ứng mang lại hiệu suất vượt trội và tính linh hoạt cao hơn trong đặt chiều rộng quảng cáo. Nếu muốn tiếp tục sử dụng biểu ngữ có chiều rộng đầy đủ, bạn có thể vẫn được thực hiện bằng cách sử dụng biểu ngữ thích ứng, như minh hoạ trong đoạn mã sau:

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

Xoá lệnh gọi lại ứng dụng

Google đã thực hiện willLeaveApplication lệnh gọi lại cho tất cả các định dạng quảng cáo đã bị huỷ bỏ và thay vào đó là applicationDidEnterBackground:sceneDidEnterBackground: . Khi sử dụng API cấp hệ điều hành, bạn sẽ nhận được thông báo mỗi khi người dùng rời khỏi ứng dụng, bất kể cho dù đó có phải là do một lượt tương tác với quảng cáo hay không.

Xin lưu ý rằng trước đây, lệnh gọi lại willLeaveApplication chưa từng là một trình xử lý lượt nhấp quảng cáo và việc dựa vào lệnh gọi lại này để báo cáo lượt nhấp tạo ra chỉ số chính xác. Ví dụ: một lượt nhấp vào phần Lựa chọn quảng cáo biểu tượng khởi chạy một trình duyệt bên ngoài đã gọi lệnh gọi lại nhưng không tính nhấp chuột.

Đổi tên lớp

Bảng dưới đây liệt kê tên lớp cụ thể đã thay đổi hoặc bị xoá trong phiên bản 8. Tóm tắt:

  • Tất cả lớp liên quan đến GADUnifiedNativeAd đã được đổi tên thành GADNativeAd
  • GADRewardBasedVideoAd, GADNativeExpressAdViewGADInstreamAd đã đã bị xoá.
  • Tất cả các lớp có tiền tố DFP đã được thay thế bằng tiền tố GAM.
Lớp phiên bản 7.68.0 Lớp phiên bản 8.0.0
DFPBannerView GAMBannerView
DFPBannerViewOptions GAMBannerViewOptions
DFPInterstitial GAMInterstitialAd
DFPRequest GAMRequest
GADRequestError NSError
GADUnifiedNativeAdView GADNativeAdView
Quảng cáo gốc của Google GADNativeAd
GADUnifiedNativeAdAssetIdentifiers GADNativeAdAssetIdentifiers
GADUnifiedNativeAdDelegate GADNativeAdDelegate
GADUnifiedNativeAdUnconfirmedClickDelegate GADNativeAdUnconfirmedClickDelegate
GADNativeCustomTemplateAd GADCustomNativeAd
GADNativeCustomTemplateAdLoaderDelegate GADCustomNativeAdLoaderDelegate
GADNativeAdDelegate GADCustomNativeAdDelegate
GADInAppPurchase Đã xóa
GADInterstitial GADInterstitialAd
GADNativeExpressAdView Đã xóa
GADRewardBasedVideoAd Đã xóa
GADInstreamAd Đã xóa
GADInstreamAdView Đã xóa

Đã xoá/thay thế một số phương thức

Bảng dưới đây liệt kê các thay đổi cụ thể trong phiên bản 8. Tóm tắt:

  • Các phương thức và thuộc tính không dùng nữa trước đây đều đã bị xoá.
  • Phương thức uỷ quyền -willLeaveApplication: đã bị xoá cho tất cả định dạng.
  • Tên lớp mạng quảng cáo đã di chuyển sang thuộc tính GADResponseInfo.
  • Mã nhận dạng thiết bị thử nghiệm đã chuyển sang GADRequestConfiguration thuộc tính này.
Lớp phiên bản 7.68.0 API phiên bản 7.68.0 API phiên bản 8.0.0 Ghi chú
GADMobileAds +configureWithApplicationID: -startWithCompletionHandler: Mã ứng dụng hiện được đặt trong Info.plist.
+disableAutomatedInAppPurchaseReporting -disableAutomatedInAppPurchaseReporting
+disableSDKCrashReporting -disableSDKCrashReporting
GADRequest testDevices GADRequestConfiguration.testdeviceidentifiers Thuộc tính testDeviceIdentifiers áp dụng cho tất cả các yêu cầu quảng cáo, trong khi thuộc tính testDevices cũ đã được đặt theo yêu cầu.
gender Đã xóa
sinh nhật Đã xóa
+sdkVersion GADMobileAds.sharedInstance.sdkVersion
-setBirthdayWithMonth:day:year: Đã xóa
-setLocationWithDescription: -setLocationWithLatitude:longitude:accuracy:
-tagForChildDirectedTreatment: [GADMobileAds.sharedInstance.requestConfiguration thẻForChildDirectedTreatment]
GADErrorCode kGADError* Lỗi GADError* Tiền tố k bị xoá khỏi tất cả các hằng số mã lỗi.
GADBannerView hasAutoRefreshed autoloadEnabled
inAppPurchaseDelegate Đã xóa
mediatedAdView Đã xóa
adNetworkClassName responseInfo.adNetworkClassName
DFPBannerView -setValidAdSizesWithSizes: -setValidAdSizes:
DFPBannerViewOptions -adSizeDelegate Đã xóa
GADBannerViewDelegate -adViewDidReceiveAd: -bannerViewDidReceiveAd:
-adView:didFailToReceiveAdWithError: -bannerView:didFailToReceiveAdWithError:
-adViewWillPresentScreen: -bannerViewWillPresentScreen:
-adViewWillDismissScreen: -bannerViewWillDismissScreen:
-adViewDidDismissScreen: -bannerViewDidDismissScreen:
-adViewWillLeaveApplication: Đã xóa
GADNativeCustomTemplateAd templateID GADCustomNativeAd.formatID
-performClickOnAssetWithKey:customClickHandler: -performClickOnAssetWithKey:
GADNativeAdImageAdLoaderOptions preferredImageOrientation GADNativeAdMediaAdOptions.mediaAspectRatio
GADInterstitial inAppPurchaseDelegate Đã xóa
isReady Đã xóa Thay vào đó, hãy sử dụng can browserFromRootViewController:error:.
hasBeenUsed Đã xóa
-init -initWithAdUnitID:
-setAdUnitID: -initWithAdUnitID:
adNetworkClassName responseInfo.adNetworkClassName
-interstitialWillLeaveApplication: Đã xóa
Quảng cáo gốc của Google videoController mediaContent.videoController
adNetworkClassName responseInfo.adNetworkClassName