โฆษณาคั่นระหว่างหน้าที่มีการให้รางวัล (เบต้า)

โฆษณาคั่นระหว่างหน้าที่มีการให้รางวัลเป็นประเภท ของรูปแบบโฆษณาที่มีสิ่งจูงใจซึ่ง ให้คุณเสนอรางวัลสำหรับโฆษณาที่ปรากฏ โดยอัตโนมัติระหว่างการเปลี่ยนหน้าตามปกติของแอป สิ่งที่แตกต่างจากโฆษณาที่มีการให้รางวัลคือ ที่จำเป็นต่อการเลือกใช้เพื่อดูโฆษณาคั่นระหว่างหน้าที่มีการให้รางวัล

ข้อกำหนดเบื้องต้น

การใช้งาน

ขั้นตอนหลักในการผสานรวมโฆษณาคั่นระหว่างหน้าที่มีการให้รางวัลมีดังนี้

  • โหลดโฆษณา
  • [ไม่บังคับ] ตรวจสอบ Callback ของ SSV
  • ลงทะเบียนสำหรับการติดต่อกลับ
  • แสดงโฆษณาและจัดการกิจกรรมมอบรางวัล

โหลดโฆษณา

การโหลดโฆษณาดำเนินการได้โดยใช้ loadWithAdUnitID:request:completionHandler: ใน ชั้นเรียนGADRewardedInterstitialAd วิธีโหลดต้องมีรหัสหน่วยโฆษณา ออบเจ็กต์ GADRequest และเครื่องจัดการเสร็จสมบูรณ์ ซึ่งจะเรียกใช้เมื่อโหลดโฆษณาสำเร็จหรือไม่สำเร็จ โหลดแล้ว มีการระบุออบเจ็กต์ GADRewardedInterstitialAd เป็นพารามิเตอร์ในการดำเนินการให้เสร็จสมบูรณ์ เครื่องจัดการ ตัวอย่างต่อไปนี้แสดงวิธีโหลด GADRewardedInterstitialAd ใน ชั้นเรียนViewController

Swift

import GoogleMobileAds
import UIKit

class ViewController: UIViewController {

  private var rewardedInterstitialAd: GADRewardedInterstitialAd?

  override func viewDidLoad() {
    super.viewDidLoad()

    do {
      rewardedInterstitialAd = try await GADRewardedInterstitialAd.load(
        withAdUnitID: "ca-app-pub-3940256099942544/6978759866", request: GADRequest())
    } catch {
      print("Failed to load rewarded interstitial ad with error: \(error.localizedDescription)")
    }
  }
}

Objective-C

#import "ViewController.h"

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

@implementation ViewController

- (void)viewDidLoad {
  [super viewDidLoad];

  [GADRewardedInterstitialAd
      loadWithAdUnitID:@"<var label='the ad unit ID'>ca-app-pub-3940256099942544/6978759866</var>"
                request:[GADRequest request]
      completionHandler:^(
          GADRewardedInterstitialAd* _Nullable rewardedInterstitialAd,
          NSError* _Nullable error) {
        if (!error) {
          self.rewardedInterstitialAd = rewardedInterstitialAd;
        }
      }
  ];
}

[ไม่บังคับ] ตรวจสอบ Callback ของการยืนยันฝั่งเซิร์ฟเวอร์ (SSV)

แอปที่ต้องใช้ข้อมูลเพิ่มเติมในฝั่งเซิร์ฟเวอร์ การยืนยัน Callback ควรใช้ ฟีเจอร์ข้อมูลที่กำหนดเองของโฆษณาที่มีการให้รางวัล ค่าสตริงใดๆ ที่กำหนดไว้ในโฆษณาที่มีการให้รางวัล ระบบจะส่งผ่านไปยังพารามิเตอร์การค้นหา custom_data ของ Callback SSV หากไม่ใช่ มีการตั้งค่าข้อมูลที่กำหนดเอง ค่าพารามิเตอร์การค้นหา custom_data จะไม่เพิ่มขึ้น อยู่ในการเรียกกลับของ SSV

ตัวอย่างโค้ดต่อไปนี้แสดงวิธีตั้งค่าข้อมูลที่กำหนดเองสำหรับโฆษณาที่มีการให้รางวัล ออบเจ็กต์โฆษณาคั่นระหว่างหน้าก่อนที่จะขอโฆษณา

Swift

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

Objective-C

[GADRewardedInterstitialAd
    loadWithAdUnitID:@"ca-app-pub-3940256099942544/6978759866"
              request:GADRequest
    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 จัดการ Callback เมื่อโฆษณา แสดงสำเร็จหรือไม่สำเร็จ และเมื่อปิดไป ดังต่อไปนี้ แสดงวิธีการติดตั้งโปรโตคอลและกำหนดให้กับโฆษณา:

Swift

import GoogleMobileAds
import UIKit

class ViewController: UIViewController, GADFullScreenContentDelegate {

  private var rewardedInterstitialAd: GADRewardedInterstitialAd?

  override func viewDidLoad() {
    super.viewDidLoad()

    do {
      rewardedInterstitialAd = try await GADRewardedInterstitialAd.load(
        withAdUnitID: "ca-app-pub-3940256099942544/6978759866", request: GADRequest())
      self.rewardedInterstitialAd?.fullScreenContentDelegate = self
    } catch {
      print("Failed to load rewarded interstitial ad 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.")
  }
}

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:@"ca-app-pub-3940256099942544/6978759866"
                request:[GADRequest 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.")
  }

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

Objective-C

- (void)show {
  // The UIViewController parameter is nullable.
  [_rewardedInterstitialAd presentFromRootViewController:nil
                                userDidEarnRewardHandler:^{

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

ขั้นตอนถัดไป

ดูข้อมูลเพิ่มเติมเกี่ยวกับความเป็นส่วนตัวของผู้ใช้