Requisitos
- Importar el SDK de anuncios de Google para móviles, ya sea de forma independiente o como parte de Firebase.
- Integrar el formato de anuncio nativo.
GADMediaContent
Anuncios nativos proporcionan acceso a una claseGADMediaContent
que se utiliza para obtener información sobre el contenido multimedia, que puede ser un vídeo o una imagen. Esa clase también sirve para controlar la escucha de eventos de reproducción de anuncios de vídeo. Puedes acceder al objeto de contenido multimedia mediante la propiedad .mediaContent
del anuncio en cuestión.
El objeto GADMediaContent
contiene información como la relación de aspecto y la duración de un vídeo. En el siguiente fragmento se muestra cómo obtener la relación de aspecto y la duración de un anuncio nativo.
Swift
if myNativeAd.mediaContent.hasVideoContent { let mediaAspectRatio = CGFloat(myNativeAd.mediaContent.aspectRatio) let duration = myNativeAd.mediaContent.duration ... }
Objective‑C
if(myNativeAd.mediaContent.hasVideoContent) { CGFloat mediaAspectRatio = myNativeAd.mediaContent.aspectRatio; NSTimeInterval duration = myNativeAd.mediaContent.duration; ... }
GADVideoController
El objeto GADMediaContent
contiene una referencia a un objeto GADVideoController
. El objeto GADVideoController
permite a los editores responder a eventos de vídeo.
Este objeto GADMediaContent
se puede obtener llamando a mediaContent.videoController
.
Retrollamadas de eventos de vídeo
Para gestionar eventos de vídeo específicos, basta con que escribas una clase que implemente el protocolo GADVideoControllerDelegate
. Todos los métodos del protocolo son opcionales.
En el siguiente ejemplo se muestra cómo implementar el protocolo delegado:
Swift
class ViewController: GADUnifiedNativeAdLoaderDelegate, GADVideoControllerDelegate { private var adLoader: GADAdLoader? func viewDidLoad() { super.viewDidLoad() let videoOptions = GADVideoOptions() videoOptions.customControlsRequested = true adLoader = GADAdLoader( adUnitID: "ca-app-pub-3940256099942544/3986624511", rootViewController: self, adTypes: [kGADAdLoaderAdTypeUnifiedNative], options: [videoOptions]) adLoader?.delegate = self adLoader?.load(GADRequest()) } func adLoader( _ adLoader: GADAdLoader?, didReceive nativeAd: GADUnifiedNativeAd? ) { // Set the videoController's delegate to be notified of video events. nativeAd?.mediaContent.videoController.delegate = self } // GADVideoControllerDelegate methods func videoControllerDidPlayVideo(_ videoController: GADVideoController) { // Implement this method to receive a notification when the video controller // begins playing the ad. } func videoControllerDidPauseVideo(_ videoController: GADVideoController) { // Implement this method to receive a notification when the video controller // pauses the ad. } func videoControllerDidEndVideoPlayback(_ videoController: GADVideoController) { // Implement this method to receive a notification when the video controller // stops playing the ad. } func videoControllerDidMuteVideo(_ videoController: GADVideoController) { // Implement this method to receive a notification when the video controller // mutes the ad. } func videoControllerDidUnmuteVideo(_ videoController: GADVideoController) { // Implement this method to receive a notification when the video controller // unmutes the ad. } }
Objective‑C
@interface ViewController () <GADUnifiedNativeAdLoaderDelegate, GADVideoControllerDelegate> @property(nonatomic, strong) GADAdLoader *adLoader; @end @implementation ViewController - (void)viewDidLoad { [super viewDidLoad]; GADVideoOptions *videoOptions = [[GADVideoOptions alloc] init]; videoOptions.customControlsRequested = YES; self.adLoader = [[GADAdLoader alloc] initWithAdUnitID:@"ca-app-pub-3940256099942544/3986624511" rootViewController:self adTypes:@[ kGADAdLoaderAdTypeUnifiedNative ] options:@[ videoOptions ]]; self.adLoader.delegate = self; [self.adLoader loadRequest:[GADRequest request]]; } - (void)adLoader:(GADAdLoader *)adLoader didReceiveUnifiedNativeAd:(GADUnifiedNativeAd *)nativeAd { // Set the videoController's delegate to be notified of video events. nativeAd.mediaContent.videoController.delegate = self; } // GADVideoControllerDelegate methods - (void)videoControllerDidPlayVideo:(nonnull GADVideoController *)videoController { // Implement this method to receive a notification when the video controller // begins playing the ad. } - (void)videoControllerDidPauseVideo:(nonnull GADVideoController *)videoController { // Implement this method to receive a notification when the video controller // pauses the ad. } - (void)videoControllerDidEndVideoPlayback:(nonnull GADVideoController *)videoController { // Implement this method to receive a notification when the video controller // stops playing the ad. } - (void)videoControllerDidMuteVideo:(nonnull GADVideoController *)videoController { // Implement this method to receive a notification when the video controller // mutes the ad. } - (void)videoControllerDidUnmuteVideo:(nonnull GADVideoController *)videoController { // Implement this method to receive a notification when the video controller // unmutes the ad. } @end
Consulta las políticas y directrices de los anuncios nativos para obtener más información sobre cómo renderizarlos.