Interstitial con premio (beta)

Interstitial con premio è un tipo di formato dell'annuncio con incentivi che ti consente di offrire premi per gli annunci pubblicati automaticamente durante le transizioni naturali dell'app. A differenza degli annunci con premio, gli utenti è necessario attivare la visualizzazione di un interstitial con premio.

Prerequisiti

Implementazione

Di seguito sono riportati i passaggi principali per integrare gli annunci interstitial con premio:

  • Carica un annuncio
  • [Facoltativo] Convalida i callback SSV
  • Registrati per le richiamate
  • Visualizzare l'annuncio e gestire l'evento di ricompensa

Carica un annuncio

Il caricamento di un annuncio viene eseguito utilizzando loadWithAdUnitID:request:completionHandler: nella GADRewardedInterstitialAd corso. Il metodo di caricamento richiede l'ID unità pubblicitaria, un GAMRequest oggetto e un gestore del completamento che viene chiamato quando il caricamento dell'annuncio ha esito positivo o negativo. Il modello caricato L'oggetto GADRewardedInterstitialAd viene fornito come parametro nel completamento . L'esempio seguente mostra come caricare un GADRewardedInterstitialAd in il tuo corso 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: "/21775744923/example/rewarded_interstitial", request: GAMRequest())
    } 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;
        }
      }
  ];
}

[Facoltativo] Convalida i callback di verifica lato server (SSV)

App che richiedono dati aggiuntivi sul lato server i callback di verifica devono utilizzare funzionalità per i dati personalizzati degli annunci con premio. Qualsiasi valore di stringa impostato su un annuncio con premio viene passato al parametro di query custom_data del callback SSV. In caso contrario valore dei dati personalizzati è impostato, il valore del parametro di ricerca custom_data non sarà presente nel callback SSV.

Il seguente esempio di codice mostra come impostare dati personalizzati su un premio prima di richiedere un annuncio.

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

Registrati per le richiamate

Per ricevere notifiche per gli eventi di presentazione, devi implementare il protocollo GADFullScreenContentDelegate e assegnarlo al Proprietà fullScreenContentDelegate dell'annuncio restituito. La Il protocollo GADFullScreenContentDelegate gestisce i callback quando l'annuncio presenta correttamente o meno e quando viene ignorato. Le seguenti mostra come implementare il protocollo e assegnarlo all'annuncio:

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

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

Visualizzare l'annuncio e gestire l'evento di ricompensa

Quando presenti il tuo annuncio, devi fornire un oggetto GADUserDidEarnRewardHandler per gestire il premio per l'utente.

Il seguente codice presenta il metodo migliore per mostrare un premio annuncio interstitial.

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

Passaggi successivi

Scopri di più sulla privacy dell'utente.