Wymagania wstępne
- Integracja formatu reklamy natywnej
GADMediaContent
Reklamy natywne zapewniają dostęp do klasy GADMediaContent
, która jest używana do pobierania
informacje o treściach multimedialnych, którymi może być film lub obraz. Jest także
służy do sterowania nasłuchiwaniem odtwarzania reklamy wideo w przypadku zdarzeń odtwarzania. Masz dostęp do:
za pomocą właściwości .mediaContent
w reklamie.
GADMediaContent
zawiera informacje, takie jak format obrazu i czas trwania filmu.
fragment kodu poniżej pokazuje, jak uzyskać współczynnik proporcji i czas trwania reklamy natywnej.
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
Obiekt GADMediaContent
ma odniesienie do
GADVideoController
.
obiektu. Obiekt GADVideoController
umożliwia wydawcom odpowiadanie na filmy.
zdarzeń.
Reklamy wyświetlane w ramach elementów zamówienia mogą
są również kontrolowane za pomocą GADVideoController
.
Ten obiekt GADVideoController
można uzyskać, wywołując
GADMediaContent.videoController
Wywołania zwrotne zdarzeń wideo
Aby obsługiwać konkretne zdarzenia wideo, musisz utworzyć klasę, która implementuje
GADVideoControllerDelegate
protokołu. Wszystkie metody protokołu są opcjonalne.
Poniższy przykład pokazuje, jak wdrożyć protokół przekazywania:
Swift
class ViewController: GADNativeAdLoaderDelegate, 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: [GADAdLoaderAdTypeNative], options: [videoOptions]) adLoader?.delegate = self adLoader?.load(GADRequest()) } func adLoader( _ adLoader: GADAdLoader?, didReceive nativeAd: GADNativeAd? ) { // 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 () <GADNativeAdLoaderDelegate, 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:@[ GADAdLoaderAdTypeNative ] options:@[ videoOptions ]]; self.adLoader.delegate = self; [self.adLoader loadRequest:[GAMRequest request]]; } - (void)adLoader:(GADAdLoader *)adLoader didReceiveNativeAd:(GADNativeAd *)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
Zapoznaj się z zasadami dotyczącymi reklam natywnych wytycznych, w których znajdziesz więcej wskazówek na temat tego, do renderowania reklam natywnych.