Reklama pełnoekranowa z nagrodą

Reklama pełnoekranowa z nagrodą to format reklamy zachęcającej do działania, który umożliwia oferowanie nagród za reklamy, które wyświetlają się automatycznie podczas naturalnych przejść w aplikacji. W przeciwieństwie do reklam z nagrodą użytkownicy nie muszą wyrazić zgody na wyświetlenie reklamy pełnoekranowej z nagrodą.

Wymagania wstępne

Implementacja

Aby zintegrować reklamy pełnoekranowe z nagrodą, musisz wykonać te podstawowe czynności:

  • Wczytaj reklamę
  • [Opcjonalnie] Weryfikacja wywołań zwrotnych SSV
  • Zarejestruj się na wywołania zwrotne
  • wyświetlanie reklamy i obsługa zdarzenia związanego z nagrodą,

Wczytaj reklamę

Wczytywanie reklamy odbywa się za pomocą statycznej metody loadWithAdUnitID:request:completionHandler: w klasie GADRewardedInterstitialAd. Metoda wczytywania wymaga identyfikatora jednostki reklamowej, obiektu GADRequest oraz modułu obsługi zakończenia, który jest wywoływany w przypadku powodzenia lub niepowodzenia wczytywania reklamy. Wczytywany obiekt GADRewardedInterstitialAd jest podany jako parametr w module obsługi ukończenia. Poniższy przykład pokazuje, jak wczytać GADRewardedInterstitialAd w klasie 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;
        }
      }
  ];
}

[Opcjonalnie] Sprawdzanie wywołań zwrotnych weryfikacji po stronie serwera (SSV)

Aplikacje, które wymagają dodatkowych danych w wywołaniach zwrotnych weryfikacji po stronie serwera, powinny korzystać z funkcji danych niestandardowych reklam z nagrodą. Wartość ciągu znaków ustawiona w obiekcie reklamy z nagrodą jest przekazywana do parametru zapytania custom_data wywołania zwrotnego SSV. Jeśli nie ustawisz żadnej wartości danych niestandardowych, wartość parametru zapytania custom_data nie będzie podana w wywołaniu zwrotnym SSV.

Poniższy przykładowy kod pokazuje, jak ustawić dane niestandardowe w obiekcie reklamy pełnoekranowej z nagrodą przed żądaniem reklamy.

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

Zarejestruj się na wywołania zwrotne

Aby otrzymywać powiadomienia o zdarzeniach związanych z prezentacją, musisz wdrożyć protokół GADFullScreenContentDelegate i przypisać go do właściwości fullScreenContentDelegate zwróconej reklamy. Protokół GADFullScreenContentDelegate obsługuje wywołania zwrotne w przypadku pomyślnego lub nieudanego przedstawienia reklamy oraz zamknięcia. Poniższy kod pokazuje, jak wdrożyć protokół i przypisać go do reklamy:

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

wyświetlanie reklamy i obsługa zdarzenia związanego z nagrodą,

Podczas wyświetlania reklamy musisz udostępnić obiekt GADUserDidEarnRewardHandler, który obsługuje nagrodę dla użytkownika.

Poniższy kod przedstawia najlepszą metodę wyświetlania reklamy pełnoekranowej z nagrodą.

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

Dalsze kroki

Dowiedz się więcej o prywatności użytkowników.