插頁式獎勵廣告

插頁式獎勵廣告是一種獎勵廣告格式,可針對在自然應用程式轉換時自動顯示的廣告提供獎勵。與獎勵廣告不同,使用者不需要選擇觀看插頁式獎勵廣告。

必要條件

  • Google Mobile Ads SDK 7.60.0 或更新版本。
  • 完成入門指南

實作

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

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

載入廣告

請使用 GADRewardedInterstitialAd 類別的靜態 loadWithAdUnitID:request:completionHandler: 方法完成廣告載入。載入方法需要廣告單元 ID、GAMRequest 物件,以及廣告載入成功或失敗時呼叫的處理常式。載入的 GADRewardedInterstitialAd 物件會在完成的處理常式中做為參數提供。以下範例說明如何在 ViewController 類別中載入 GADRewardedInterstitialAd

Swift

import GoogleMobileAds
import UIKit

class ViewController: UIViewController {

  private var rewardedInterstitialAd: GADRewardedInterstitialAd?

  override func viewDidLoad() {
    super.viewDidLoad()

    GADRewardedInterstitialAd.load(withAdUnitID:"/21775744923/example/rewarded_interstitial",
    request: GAMRequest()) { ad, error in
      if let error = error {
        return print("Failed to load rewarded interstitial ad with error: \(error.localizedDescription)")
      }

      self.rewardedInterstitialAd = ad
    }
  }
}

Objective-C

#import "ViewController.h"

@interface ViewController ()
@property(nonatomic, strong) GADRewardedInterstitialAd* rewardedInterstitialAd;
@end

@implementation ViewController

- (void)viewDidLoad {
  [super viewDidLoad];

  [GADRewardedInterstitialAd
       loadWithAdUnitID:@"/21775744923/example/rewarded_interstitial"
                request:[GAMRequest request]
      completionHandler:^(
          GADRewardedInterstitialAd* _Nullable rewardedInterstitialAd,
          NSError* _Nullable error) {
        if (!error) {
          self.rewardedInterstitialAd = rewardedInterstitialAd;
        }
      }
  ];
}

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

如果應用程式需要伺服器端驗證回呼提供額外資料,則應使用獎勵廣告的自訂資料功能。獎勵廣告物件的任何字串值,都會傳遞至 SSV 回呼的 custom_data 查詢參數。如未設定自訂資料值,SSV 回呼就不會顯示 custom_data 查詢參數值。

以下程式碼範例示範如何在發出廣告請求前,為獎勵插頁式廣告物件設定自訂資料。

Swift

GADRewardedInterstitialAd.load(withAdUnitID:"/21775744923/example/rewarded_interstitial",
                       request: request,
                       completionHandler: { [self] ad, error in
      if let error != error {
      rewardedInterstitialAd = ad
      let options = GADServerSideVerificationOptions()
      options.customRewardString = "SAMPLE_CUSTOM_DATA_STRING"
      rewardedInterstitialAd.serverSideVerificationOptions = options
    }

Objective-C

GAMRequest *request = [GAMRequest request];
[GADRewardedInterstitialAd
     loadWithAdUnitID:@"/21775744923/example/rewarded_interstitial"
              request:request
    completionHandler:^(GADRewardedInterstitialAd *ad, NSError *error) {
      if (error) {
        // Handle Error
        return;
      }
      self.rewardedInterstitialAd = ad;
      GADServerSideVerificationOptions *options =
          [[GADServerSideVerificationOptions alloc] init];
      options.customRewardString = @"SAMPLE_CUSTOM_DATA_STRING";
      ad.serverSideVerificationOptions = options;
    }];

註冊回呼

如要接收簡報事件的通知,您必須實作 GADFullScreenContentDelegate 通訊協定,並指派給所傳回廣告的 fullScreenContentDelegate 屬性。GADFullScreenContentDelegate 通訊協定會處理廣告成功或失敗以及關閉時的回呼。以下程式碼示範如何實作通訊協定,並將其指派給廣告:

Swift

class ViewController: UIViewController {

  private var rewardedInterstitialAd: GADRewardedInterstitialAd?

  override func viewDidLoad() {
    super.viewDidLoad()

    GADRewardedInterstitialAd.load(withAdUnitID:"/21775744923/example/rewarded_interstitial",
    request: GAMRequest()) { ad, error in
      if let error = error {
        return print("Failed to load rewarded interstitial ad with error: \(error.localizedDescription)")
      }

      self.rewardedInterstitialAd = ad
      self.rewardedInterstitialAd?.fullScreenContentDelegate = self
    }
  }
}

extension ViewController: GADFullScreenContentDelegate {

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

Objective-C

@interface ViewController ()<GADFullScreenContentDelegate>
@property(nonatomic, strong) GADRewardedInterstitialAd *rewardedInterstitialAd;
@end

@implementation ViewController

- (void)viewDidLoad {
  [super viewDidLoad];
  // Do any additional setup after loading the view.

  [GADRewardedInterstitialAd
       loadWithAdUnitID:@"/21775744923/example/rewarded_interstitial"
                request:[GAMRequest request]
      completionHandler:^(
          GADRewardedInterstitialAd *_Nullable rewardedInterstitialAd,
          NSError *_Nullable error) {
        if (!error) {
          self.rewardedInterstitialAd = rewardedInterstitialAd;
          self.rewardedInterstitialAd.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.");
}

顯示廣告並處理獎勵事件

提供廣告時,您必須提供 GADUserDidEarnRewardHandler 物件來處理使用者的獎勵。

以下程式碼是顯示插頁式插頁式廣告的最佳做法。

Swift

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

  rewardedInterstitialAd.present(fromRootViewController: self) {
    let reward = rewardedInterstitialAd.adReward
    // TODO: Reward the user!
  }
}

Objective-C

- (void)show {
  [_rewardedInterstitialAd presentFromRootViewController:self
                                userDidEarnRewardHandler:^{

                                  GADAdReward *reward =
                                      self.rewardedInterstitialAd.adReward;
                                  // TODO: Reward the user!
                                }];
}