Reklama pełnoekranowa z nagrodą

Reklama pełnoekranowa z nagrodą to typ reklamy zachęcającej do otrzymania nagrody, która umożliwia oferowanie nagród za wyświetlanie reklam, które pojawiają się automatycznie w naturalnych momentach przejściowych 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

  • Pakiet SDK do reklam mobilnych Google w wersji 7.60.0 lub nowszej.
  • Przeczytaj wprowadzenie.

Wdrażanie

Aby zintegrować reklamy pełnoekranowe z nagrodą:

  • Wczytywanie reklamy
  • [Opcjonalnie] Zweryfikuj wywołania zwrotne SSV
  • Zarejestruj się na oddzwonienie
  • Wyświetlaj reklamę i obsługuj zdarzenie związane z nagrodą

Wczytywanie reklamy

Wczytywanie reklamy odbywa się przy użyciu statycznej metody loadWithAdUnitID:request:completionHandler: w klasie GADRewardedInterstitialAd. Metoda wczytywania wymaga identyfikatora jednostki reklamowej, obiektu GADRequest i modułu obsługi ukończenia, które są wywoływane po udanym lub nieudanym wczytaniu reklamy. Wczytany obiekt GADRewardedInterstitialAd jest podany jako parametr w procedurze zakończenia. Poniższy przykład pokazuje, jak wczytać klasę GADRewardedInterstitialAd w klasie ViewController.

Swift

import GoogleMobileAds
import UIKit

class ViewController: UIViewController {

  private var rewardedInterstitialAd: GADRewardedInterstitialAd?

  override func viewDidLoad() {
    super.viewDidLoad()

    GADRewardedInterstitialAd.load(withAdUnitID:"ca-app-pub-3940256099942544/6978759866",
    request: GADRequest()) { 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:@"ca-app-pub-3940256099942544/6978759866"
                request:[GADRequest request]
      completionHandler:^(
          GADRewardedInterstitialAd* _Nullable rewardedInterstitialAd,
          NSError* _Nullable error) {
        if (!error) {
          self.rewardedInterstitialAd = rewardedInterstitialAd;
        }
      }
  ];
}

[Opcjonalnie] Zweryfikuj wywołania zwrotne po stronie serwera (SSV)

Aplikacje, które wymagają dodatkowych danych w wywołaniach zwrotnych po stronie serwera, powinny używać niestandardowej funkcji danych w reklamach z nagrodą. Każda wartość ciągu ustawiona w obiekcie reklamy z nagrodą jest przekazywana do parametru zapytania custom_data wywołania zwrotnego SSV. Jeśli nie ustawisz niestandardowej wartości danych, w wywołaniu wywołania SSV nie będzie wartości parametru zapytania custom_data.

Przeanalizuj przykładowy kod poniżej, aby dowiedzieć się, jak skonfigurować dane niestandardowe w obrębie obiektu reklamy pełnoekranowej z nagrodą, zanim wyślesz żądanie reklamy.

Swift

GADRewardedInterstitialAd.load(withAdUnitID:"ca-app-pub-3940256099942544/6978759866",
                       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

GADRequest *request = [GADRequest request];
[GADRewardedInterstitialAd
     loadWithAdUnitID:@"ca-app-pub-3940256099942544/6978759866"
              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;
    }];

Zarejestruj się na oddzwonienie

Aby otrzymywać powiadomienia o zdarzeniach związanych z prezentacją, musisz zaimplementować protokół GADFullScreenContentDelegate i przypisać go do właściwości fullScreenContentDelegate w zwróconej reklamie. Protokół GADFullScreenContentDelegate obsługuje wywołania zwrotne w przypadku udanych i nieudanych wyświetleń reklamy oraz jej odrzucenia. W tym kodzie pokazujemy, jak wdrożyć protokół i przypisać go do reklamy:

Swift

class ViewController: UIViewController {

  private var rewardedInterstitialAd: GADRewardedInterstitialAd?

  override func viewDidLoad() {
    super.viewDidLoad()

    GADRewardedInterstitialAd.load(withAdUnitID:"ca-app-pub-3940256099942544/6978759866",
    request: GADRequest()) { 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:@"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świetlaj reklamę i obsługuj zdarzenie związane z nagrodą

Gdy prezentujesz reklamę, musisz udostępnić obiekt GADUserDidEarnRewardHandler, by obsłużyć nagrodę dla użytkownika.

Ten kod pokazuje najlepszą metodę wyświetlania reklamy pełnoekranowej z nagrodą.

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

Dalsze kroki

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