獎勵廣告

使用者可選擇與獎勵廣告互動 提供應用程式內獎勵本指南將說明如何將 AdMob 獎勵廣告整合至 iOS 應用程式。 閱讀一些客戶成功案例:個案研究 1個案研究 2

必要條件

  • Google Mobile Ads SDK 8.0.0 以上版本。
  • 完成入門指南

請務必使用測試廣告進行測試

建構及測試應用程式時,請務必使用測試廣告,而非 現場及正式環境廣告否則帳戶可能會遭到停權。

要載入測試廣告,最簡單的方法就是使用我們專屬的 iOS 測試廣告單元 ID 獎勵廣告:

ca-app-pub-3940256099942544/1712485313

這項機制經過特別設定,可針對每個請求傳回測試廣告 在您的程式設計、測試和偵錯時,可以免費使用應用程式。只要在發布應用程式前,將其替換為自己的廣告單元 ID 即可。

若要進一步瞭解 Mobile Ads SDK 測試廣告的運作方式,請參閱「測試 Google Ads

導入作業

整合獎勵廣告的主要步驟如下:

  • 載入廣告
  • [選用] 驗證 SSV 回呼
  • 註冊回呼
  • 顯示廣告並處理獎勵事件

載入廣告

系統會使用 load(adUnitID:request) 完成載入廣告 GADRewardedAd 類別中的方法。

Swift

import GoogleMobileAds
import UIKit

class ViewController: UIViewController {

  private var rewardedAd: GADRewardedAd?

  func loadRewardedAd() async {
    do {
      rewardedAd = try await GADRewardedAd.load(
        withAdUnitID: "ca-app-pub-3940256099942544/1712485313", request: GADRequest())
    } catch {
      print("Rewarded ad failed to load with error: \(error.localizedDescription)")
    }
  }
}

SwiftUI

import GoogleMobileAds

class RewardedViewModel: NSObject, ObservableObject, GADFullScreenContentDelegate {
  @Published var coins = 0
  private var rewardedAd: GADRewardedAd?

  func loadAd() async {
    do {
      rewardedAd = try await GADRewardedAd.load(
        withAdUnitID: "ca-app-pub-3940256099942544/1712485313", request: GADRequest())
      rewardedAd?.fullScreenContentDelegate = self
    } catch {
      print("Failed to load rewarded ad with error: \(error.localizedDescription)")
    }
  }

Objective-C

@import GoogleMobileAds;
@import UIKit;

@interface ViewController ()

@property(nonatomic, strong) GADRewardedAd *rewardedAd;

@end

@implementation ViewController
- (void)loadRewardedAd {
  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.");
      }];
}

[選用] 驗證伺服器端驗證 (SSV) 回呼

需要在伺服器端使用額外資料的應用程式 驗證回呼應使用 獎勵廣告的自訂資料功能。獎勵廣告物件上設定的任何字串值,都會傳遞至 SSV 回呼的 custom_data 查詢參數。如果答案為「否」 如果已設定自訂資料值,custom_data 查詢參數將不會 傳遞到 SSV 回呼中。

以下程式碼範例示範如何設定獎勵廣告的自訂資料 物件,再請求廣告。

Swift

do {
  rewardedAd = try await GADRewardedAd.load(
    withAdUnitID: "ca-app-pub-3940256099942544/1712485313", request: GADRequest())
  let options = GADServerSideVerificationOptions()
  options.customRewardString = "SAMPLE_CUSTOM_DATA_STRING"
  rewardedAd.serverSideVerificationOptions = options
} catch {
  print("Rewarded ad failed to load with error: \(error.localizedDescription)")
}

Objective-C

[GADRewardedAd
     loadWithAdUnitID:@"ca-app-pub-3940256099942544/1712485313"
              request:[GADRequest request];
    completionHandler:^(GADRewardedAd *ad, NSError *error) {
      if (error) {
        // Handle Error
        return;
      }
      self.rewardedAd = ad;
      GADServerSideVerificationOptions *options =
          [[GADServerSideVerificationOptions alloc] init];
      options.customRewardString = @"SAMPLE_CUSTOM_DATA_STRING";
      ad.serverSideVerificationOptions = options;
    }];

註冊回呼

您必須執行 GADFullScreenContentDelegate 通訊協定並指派給 傳回廣告的 fullScreenContentDelegate 屬性。 GADFullScreenContentDelegate 通訊協定會處理廣告發生時的回呼 是否會成功或失敗,以及在關閉時啟動。下列 程式碼顯示如何執行通訊協定,並將其指派給廣告:

Swift

import GoogleMobileAds
import UIKit

class ViewController: UIViewController, GADFullScreenContentDelegate {

  private var rewardedAd: GADRewardedAd?

  func loadRewardedAd() async {
    do {
      rewardedAd = try await GADRewardedAd.load(
        withAdUnitID: "ca-app-pub-3940256099942544/1712485313", request: GADRequest())
      rewardedAd?.fullScreenContentDelegate = self
    } catch {
      print("Rewarded ad failed to load with error: \(error.localizedDescription)")
    }
  }

  /// Tells the delegate that the ad failed to present full screen content.
  func ad(_ ad: GADFullScreenPresentingAd, didFailToPresentFullScreenContentWithError error: Error) {
    print("Ad did fail to present full screen content.")
  }

  /// Tells the delegate that the ad will present full screen content.
  func adWillPresentFullScreenContent(_ ad: GADFullScreenPresentingAd) {
    print("Ad will present full screen content.")
  }

  /// Tells the delegate that the ad dismissed full screen content.
  func adDidDismissFullScreenContent(_ ad: GADFullScreenPresentingAd) {
    print("Ad did dismiss full screen content.")
  }
}

SwiftUI

fullScreenContentDelegate 屬性指派給傳回的廣告:

rewardedAd?.fullScreenContentDelegate = self

實作通訊協定:

func adDidRecordImpression(_ ad: GADFullScreenPresentingAd) {
  print("\(#function) called")
}

func adDidRecordClick(_ ad: GADFullScreenPresentingAd) {
  print("\(#function) called")
}

func ad(
  _ ad: GADFullScreenPresentingAd,
  didFailToPresentFullScreenContentWithError error: Error
) {
  print("\(#function) called")
}

func adWillPresentFullScreenContent(_ ad: GADFullScreenPresentingAd) {
  print("\(#function) called")
}

func adWillDismissFullScreenContent(_ ad: GADFullScreenPresentingAd) {
  print("\(#function) called")
}

func adDidDismissFullScreenContent(_ ad: GADFullScreenPresentingAd) {
  print("\(#function) called")
  // Clear the rewarded ad.
  rewardedAd = nil
}

Objective-C

@interface ViewController () <GADFullScreenContentDelegate>

@property(nonatomic, strong) GADRewardedAd *rewardedAd;

@end

@implementation ViewController
- (void)loadRewardedAd {
  GADRequest *request = [GADRequest request];
  [GADRewardedAd
      loadWithAdUnitID:@"ca-app-pub-3940256099942544/4806952744"
                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 ad failed to present full screen content.
- (void)ad:(nonnull id<GADFullScreenPresentingAd>)ad
    didFailToPresentFullScreenContentWithError:(nonnull NSError *)error {
    NSLog(@"Ad did fail to present full screen content.");
}

/// Tells the delegate that the ad will present full screen content.
- (void)adWillPresentFullScreenContent:(nonnull id<GADFullScreenPresentingAd>)ad {
    NSLog(@"Ad will present full screen content.");
}

/// Tells the delegate that the ad dismissed full screen content.
- (void)adDidDismissFullScreenContent:(nonnull id<GADFullScreenPresentingAd>)ad {
    NSLog(@"Ad did dismiss full screen content.");
}

GADRewardedAd 是一次性的物件。這表示當獎勵廣告 系統現在無法再次顯示最佳做法是載入其他獎勵廣告 在 GADFullScreenContentDelegateadDidDismissFullScreenContent: 方法中 ,緊接在上一個獎勵廣告後 已關閉。

顯示廣告並處理獎勵事件

向使用者顯示獎勵廣告之前,請務必 明確選擇觀看獎勵廣告以換取獎勵。獎勵廣告 廣告都必須是使用者主動選擇使用的體驗。

顯示廣告時,您必須提供 GADUserDidEarnRewardHandler 物件 處理使用者獎勵

以下程式碼提供顯示獎勵廣告的最佳方法。

Swift

func show() {
  guard let rewardedAd = rewardedAd else {
    return print("Ad wasn't ready.")
  }

  // The UIViewController parameter is an optional.
  ad.present(fromRootViewController: nil) {
    let reward = ad.adReward
    print("Reward received with currency \(reward.amount), amount \(reward.amount.doubleValue)")
    // TODO: Reward the user.
  }
}

SwiftUI

監聽檢視畫面中的 UI 事件,決定廣告顯示的時機。

var body: some View {
  VStack(spacing: 20) {
      Button("Watch video for additional 10 coins") {
        viewModel.showAd()
        showWatchVideoButton = false
      }

在檢視模型中顯示獎勵廣告:

func showAd() {
  guard let rewardedAd = rewardedAd else {
    return print("Ad wasn't ready.")
  }

  rewardedAd.present(fromRootViewController: nil) {
    let reward = rewardedAd.adReward
    print("Reward amount: \(reward.amount)")
    self.addCoins(reward.amount.intValue)
  }
}

Objective-C

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

常見問題

我可以取得 GADRewardedAd的獎勵詳細資料嗎?
是的,如果您需要在 userDidEarnReward 回呼觸發前取得獎勵金額,GADRewardedAd 就會提供 adReward 屬性,讓您在廣告載入後確認獎勵金額。
初始化呼叫是否有逾時?
10 秒後,Google Mobile Ads SDK 會叫用 GADInitializationCompletionHandler提供給 startWithCompletionHandler: 方法,就算中介服務聯播網尚未 並完成初始化。
如果在收到初始化回呼時,部分中介服務聯播網尚未就緒,該怎麼辦?

建議您在 GADInitializationCompletionHandler 中載入廣告。即使中介服務聯播網尚未準備就緒 Google Mobile Ads SDK 仍會要求該聯播網提供廣告。因此,如果中介服務網路在逾時後完成初始化,仍可在該工作階段中處理未來的廣告請求。

您可以持續輪詢所有轉接程式的初始化狀態 呼叫 GADMobileAds.initializationStatus 來修正應用程式工作階段。

如何找出特定中介服務聯播網未就緒的原因?

GADAdapterStatus 物件的 description 屬性會說明為何轉接程式無法處理廣告請求。

userDidEarnRewardHandler 完成處理常式是否一律會在 adDidDismissFullScreenContent: 委派方法之前呼叫?

對於 Google Ads,所有 userDidEarnRewardHandler 呼叫都會在 adDidDismissFullScreenContent: 之前發生。對於透過 中介服務,也就是第三方廣告聯播網 SDK 的實作方式會決定回呼順序。針對符合下列條件的廣告聯播網 SDK 提供獎勵資訊的單一委派方法,中介服務轉接程式 在 adDidDismissFullScreenContent: 之前叫用 userDidEarnRewardHandler

GitHub 上的範例

查看您偏好語言的完整獎勵廣告範例:

後續步驟

進一步瞭解使用者隱私