Los anuncios intersticiales bonificados son un formato que te permite ofrecer recompensas por ver anuncios que aparecen automáticamente cuando tienen lugar las transiciones naturales de una aplicación. A diferencia de lo que ocurre con los anuncios bonificados tradicionales, no hace falta que los usuarios indiquen que quieren verlos para que se les muestren.
Requisitos previos
- Tener la versión 7.60.0 del SDK de anuncios de Google para móviles o una posterior.
- Seguir la guía de introducción para importar el SDK de anuncios de Google para móviles.
- Ponerte en contacto con tu gestor de cuentas para poder usar los anuncios intersticiales bonificados.
Implementación
Los pasos principales para integrar anuncios intersticiales bonificados son los siguientes:
- Cargar un anuncio.
- Registrarse para recibir retrollamadas.
- Mostrar el anuncio y gestionar el evento de bonificación.
Cargar un anuncio
Para cargar anuncios, utiliza el método loadWithAdUnitID:request:completionHandler:
estático de la clase GADRewardedInterstitialAd
. El método de carga requiere el ID de tu bloque de anuncios, un objeto GADRequest
y un controlador de finalización al que se llama cuando la carga de anuncios se realiza correctamente o da error. El objeto GADRewardedInterstitialAd
cargado se proporciona como parámetro en el controlador de finalización. En el siguiente ejemplo se muestra cómo cargar un objeto GADRewardedInterstitialAd
en tu clase 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; } } ]; }
Registrarse para recibir retrollamadas
Para recibir notificaciones de eventos de presentación, debes implementar el protocolo GADFullScreenContentDelegate
y asignarlo a la propiedad fullScreenContentDelegate
del anuncio devuelto. El protocolo GADFullScreenContentDelegate
gestiona las retrollamadas cuando el anuncio se muestra correctamente, cuando no se puede presentar y cuando lo cierra el usuario. En el siguiente código se muestra cómo implementar el protocolo y asignarlo al anuncio:
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."); }
Mostrar el anuncio y gestionar el evento de bonificación
Al mostrar el anuncio, debes proporcionar un objeto GADUserDidEarnRewardHandler
para gestionar la recompensa que obtendrán los usuarios.
En el siguiente fragmento de código se muestra el mejor método para mostrar un anuncio intersticial bonificado.
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! }]; }