Interstisial reward (beta)

Interstisial reward adalah jenis format iklan berinsentif yang memungkinkan Anda menawarkan reward untuk iklan yang muncul secara otomatis selama transisi aplikasi secara alami. Berbeda dengan iklan reward, pengguna tidak diwajibkan untuk memilih melihat interstisial reward.

Prasyarat

  • Google Mobile Ads SDK 7.60.0 atau yang lebih tinggi.
  • Selesaikan Panduan memulai.

Penerapan

Langkah utama untuk mengintegrasikan iklan interstisial reward adalah sebagai berikut:

  • Memuat iklan
  • [Opsional] Memvalidasi callback SSV
  • Mendaftar untuk callback
  • Menampilkan iklan dan menangani peristiwa reward

Memuat iklan

Pemuatan iklan dilakukan menggunakan load(adUnitID:request) pada class GADRewardedInterstitialAd.

Swift

import GoogleMobileAds
import UIKit

class ViewController: UIViewController {

  private var rewardedInterstitialAd: GADRewardedInterstitialAd?

  override func viewDidLoad() {
    super.viewDidLoad()

    Task {
      do {
        rewardedInterstitialAd = try await GADRewardedInterstitialAd.load(
          withAdUnitID: "/21775744923/example/rewarded-interstitial", request: GAMRequest())
      } catch {
        print("Failed to load rewarded interstitial ad with error: \(error.localizedDescription)")
      }
    }
  }
}

SwiftUI

import GoogleMobileAds

class RewardedInterstitialViewModel: NSObject, ObservableObject,
  GADFullScreenContentDelegate
{
  @Published var coins = 0
  private var rewardedInterstitialAd: GADRewardedInterstitialAd?

  func loadAd() async {
    do {
      rewardedInterstitialAd = try await GADRewardedInterstitialAd.load(
        withAdUnitID: "ca-app-pub-3940256099942544/6978759866", request: GADRequest())
      rewardedInterstitialAd?.fullScreenContentDelegate = self
    } 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'>/21775744923/example/rewarded-interstitial</var>"
                request:[GAMRequest request]
      completionHandler:^(
          GADRewardedInterstitialAd* _Nullable rewardedInterstitialAd,
          NSError* _Nullable error) {
        if (!error) {
          self.rewardedInterstitialAd = rewardedInterstitialAd;
        }
      }
  ];
}

[Opsional] Memvalidasi callback verifikasi sisi server (SSV)

Aplikasi yang memerlukan data tambahan di sisi server callback verifikasi harus menggunakan fitur data kustom iklan reward. Nilai string apa pun yang ditetapkan pada iklan reward diteruskan ke parameter kueri custom_data dari callback SSV. Jika tidak nilai data kustom ditetapkan, nilai parameter kueri custom_data tidak akan ada di callback SSV.

Contoh kode berikut menunjukkan cara menetapkan data kustom di iklan reward objek iklan interstisial sebelum meminta iklan.

Swift

do {
  rewardedInterstitialAd = try await GADRewardedInterstitialAd.load(
    withAdUnitID: "/21775744923/example/rewarded-interstitial", request: GAMRequest())
  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:@"/21775744923/example/rewarded-interstitial"
              request:GAMRequest
    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;
    }];

Mendaftar untuk callback

Untuk menerima notifikasi acara presentasi, Anda harus mengimplementasikan protokol GADFullScreenContentDelegate dan menetapkannya ke fullScreenContentDelegate dari iklan yang ditampilkan. Protokol GADFullScreenContentDelegate menangani callback saat iklan ditampilkan dengan berhasil atau tidak, dan saat iklan ditutup. Kode berikut menunjukkan cara menerapkan protokol dan menetapkannya ke iklan:

Swift

import GoogleMobileAds
import UIKit

class ViewController: UIViewController, GADFullScreenContentDelegate {

  private var rewardedInterstitialAd: GADRewardedInterstitialAd?

  override func viewDidLoad() {
    super.viewDidLoad()

    Task {
      do {
        rewardedInterstitialAd = try await GADRewardedInterstitialAd.load(
          withAdUnitID: "/21775744923/example/rewarded-interstitial", request: GAMRequest())
        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.")
  }
}

SwiftUI

Tetapkan properti fullScreenContentDelegate ke iklan yang ditampilkan:

rewardedInterstitialAd?.fullScreenContentDelegate = self

Terapkan protokol:

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 interstitial ad.
  rewardedInterstitialAd = nil
}

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

Menampilkan iklan dan menangani peristiwa reward

Saat menampilkan iklan, Anda harus memberikan objek GADUserDidEarnRewardHandler untuk menangani reward bagi pengguna.

Kode berikut menyajikan metode terbaik untuk menampilkan iklan reward iklan interstisial.

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

SwiftUI

Memproses peristiwa UI dalam tampilan untuk menampilkan iklan.

var rewardedInterstitialBody: some View {
  // ...
  }
  .onChange(
    of: showAd,
    perform: { newValue in
      if newValue {
        viewModel.showAd()
      }
    }
  )

Tampilkan iklan interstisial reward dari model tampilan:

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

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

Objective-C

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

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

Contoh di GitHub

Lihat contoh lengkap iklan interstisial reward dalam bahasa yang Anda inginkan:

Langkah berikutnya

Pelajari privasi pengguna lebih lanjut.