原生广告选项

请选择平台: Android iOS

原生广告具有许多高级功能,可让您进行更多自定义,并尽可能打造出色的广告体验。本指南介绍了如何使用原生广告的高级功能。

前提条件

素材资源控件

配置原生广告素材资源控件。

首选媒体宽高比控件

借助媒体宽高比控件,您可以指定广告素材的宽高比偏好设置。

使用 GADMediaAspectRatio 设置 GADNativeAdMediaAdLoaderOptions mediaAspectRatio

  • 如果未设置此控件,则返回的广告可以采用任意媒体宽高比。

  • 如果设置了此控件,您将能够指定首选的宽高比类型来改善用户体验。

以下示例指示 SDK 优先返回采用特定宽高比的图片或视频。

GADNativeAdMediaAdLoaderOptions *nativeOption = [[GADNativeAdMediaAdLoaderOptions alloc] init];
nativeOption.mediaAspectRatio = GADMediaAspectRatioAny;

GADAdLoader* adLoader = [[GADAdLoader alloc] initWithAdUnitID:@"/21775744923/example/native"
                                           rootViewController:self
                                                      adTypes:@[ GADAdLoaderAdTypeNative ]
                                                      options:@[ nativeOption ]];

图片下载控件

借助图片下载控件,您可以决定 SDK 是返回图片素材资源还是仅返回 URI。

使用 BOOL 值设置 GADNativeAdImageAdLoaderOptions disableImageLoading

  • 图片下载控件默认处于停用状态。

  • 停用后,Google 移动广告 SDK 会为您填充图片和 URI。

  • 启用后,SDK 会仅填充 URI,从而允许您自行决定是否下载实际图片。

以下示例指示 SDK 仅返回 URI。

GADNativeAdImageAdLoaderOptions *nativeOption = [[GADNativeAdImageAdLoaderOptions alloc] init];
nativeOption.disableImageLoading = YES;

GADAdLoader* adLoader = [[GADAdLoader alloc] initWithAdUnitID:@"/21775744923/example/native"
                                           rootViewController:self
                                                      adTypes:@[ GADAdLoaderAdTypeNative ]
                                                      options:@[ nativeOption ]];

  • 图片载荷控件默认处于停用状态。

  • 停用后,您的应用会指示 SDK 对于包含一系列图片的任何素材资源,仅提供第一张图片。

  • 启用后,您的应用表示它已做好准备,可以为包含多张图片的任何素材资源展示所有图片。

以下示例指示 SDK 返回多个图片素材资源。

GADNativeAdImageAdLoaderOptions *nativeOption = [[GADNativeAdImageAdLoaderOptions alloc] init];
nativeOption.shouldRequestMultipleImages = YES;

GADAdLoader* adLoader = [[GADAdLoader alloc] initWithAdUnitID:@"/21775744923/example/native"
                                           rootViewController:self
                                                      adTypes:@[ GADAdLoaderAdTypeNative ]
                                                      options:@[ nativeOption ]];

“广告选项”图标位置

自定义“广告选项”图标的显示。

“广告选项”位置控件

借助广告选项位置控件,您可以选择在哪个角落呈现“广告选项”图标。

使用 GADAdChoicesPosition 值设置 GADNativeAdViewAdOptions preferredAdChoicesPosition

  • 如果未设置,则“广告选项”图标位置会设置为右上角。

  • 如果设置了此控件,“广告选项”图标会放置在所请求的自定义位置。

以下示例演示了如何设置自定义“广告选项”图标位置。

GADNativeAdViewAdOptions *nativeOptions = [[GADNativeAdViewAdOptions alloc] init];
nativeOptions.preferredAdChoicesPosition = GADAdChoicesPositionTopLeftCorner;

GADAdLoader* adLoader = [[GADAdLoader alloc] initWithAdUnitID:@"/21775744923/example/native"
                                           rootViewController:self
                                                      adTypes:@[ GADAdLoaderAdTypeNative ]
                                                      options:@[ nativeOptions ]];

“广告选项”自定义视图

借助“广告选项”自定义视图功能,您可以将“广告选项”图标放置在自定义位置。这与“广告选项”位置控件不同,后者仅允许指定放置在屏幕的四个角之一。

在呈现之前,使用 GADAdChoicesView 设置 GADNativeAd.adChoicesView 属性,然后“广告选项”内容将呈现到 GADAdChoicesView 中。

以下示例演示了如何设置自定义“广告选项”视图。“广告选项”图标将呈现在 GADAdChoicesView 内。

Objective-C

- (void)adLoader:(GADAdLoader *)adLoader didReceiveNativeAd:(GADNativeAd *)nativeAd {

    ...

    
    GADAdChoicesView *customAdChoicesView =
        [[GADAdChoicesView alloc] initWithFrame: CGRectMake(..., ..., ..., ...)];

    [nativeAdView addSubview:customAdChoicesView];
    nativeAdView.adChoicesView = customAdChoicesView;

    // Associate the native ad view with the native ad object. This is
    // required to make the ad clickable.
    // Note: this should always be done after populating the ad views.
    nativeAdView.nativeAd = nativeAd;
}

Swift

func adLoader(_ adLoader: AdLoader, didReceive nativeAd: NativeAd) {
    refreshAdButton.isEnabled = true

    ...
    // Define a custom position for the AdChoices icon.
    let customRect = CGRect(x: 100, y: 100, width: 15, height: 15)
    let customAdChoicesView = AdChoicesView(frame: customRect)
    nativeAdView.addSubview(customAdChoicesView)
    nativeAdView.adChoicesView = customAdChoicesView

    // Associate the native ad view with the native ad object. This is
    // required to make the ad clickable.
    // Note: this should always be done after populating the ad views.
    nativeAdView.nativeAd = nativeAd;
}

视频控件

配置视频素材资源行为。

开始播放时是否静音

借助“开始播放时是否静音”选项,您可以在开始播放视频时停用或启用音频。

使用 BOOL 值设置 GADVideoOptions startMuted

  • “开始播放时是否静音”选项默认处于启用状态。

  • 停用后,您的应用会请求从视频开头播放音频。

  • 启用后,您的应用会请求视频在开始播放时静音。

以下示例展示了如何在开始播放视频时,对音频取消静音。

GADVideoOptions *nativeOptions = [[GADVideoOptions alloc] init];
nativeOptions.startMuted = NO;

GADAdLoader* adLoader = [[GADAdLoader alloc] initWithAdUnitID:@"/21775744923/example/native"
                                           rootViewController:self
                                                      adTypes:@[ GADAdLoaderAdTypeNative ]
                                                      options:@[ nativeOptions ]];

自定义播放控件

这样一来,您就可以请求自定义视频输入控件来对视频进行播放、暂停或静音操作。

使用 BOOL 值设置 GADVideoOptions customControlsRequested

  • 自定义播放控件默认处于停用状态。

  • 停用后,您的视频将显示由 SDK 呈现的输入控件。

如果广告确实包含视频内容,并且已启用自定义控件,那么您应在展示广告的同时显示自定义控件,因为广告本身不会显示任何控件。然后,控件可以对以下对象调用相关方法:

GADVideoController

以下示例展示了如何请求包含自定义播放控件的视频。

GADVideoOptions *nativeOptions = [[GADVideoOptions alloc] init];
nativeOptions.customControlsRequested = YES;

GADAdLoader* adLoader = [[GADAdLoader alloc] initWithAdUnitID:@"/21775744923/example/native"
                                           rootViewController:self
                                                      adTypes:@[ GADAdLoaderAdTypeNative ]
                                                      options:@[ nativeOptions ]];

检查是否已启用自定义控件

由于在请求时无法知道返回的广告是否允许使用自定义视频控件,因此您必须检查它是否已启用自定义控件。

Objective-C

- (void)adLoader:(GADAdLoader *)adLoader
    didReceiveNativeAd:(GADNativeAd*)nativeAd {

  GADVideoController *videoController = nativeAd.mediaContent.videoController;
  BOOL canShowCustomControls = videoController.customControlsEnabled;

}

Swift

func adLoader(_ adLoader: AdLoader, didReceive nativeAd: NativeAd) {
  let videoController = nativeAd.mediaContent.videoController
  let canShowCustomControls = videoController?.customControlsEnabled() == true
}

呈现自定义视频控件

根据以下最佳实践呈现自定义视频控件:

  1. 将自定义控件视图作为原生广告视图的子视图呈现。采用这种做法后,开放式衡量可见度计算会将自定义控件视为友好遮挡。
  2. 避免在整个媒体视图上呈现不可见的叠加层。叠加层会遮挡媒体视图上的点击,从而对原生广告效果产生负面影响。请改为创建一个仅够容纳控件的小叠加层。

自定义点击手势

自定义点击手势是一项原生广告功能,可让广告视图上的滑动操作注册为广告点击。它旨在与通过滑动操作手势浏览内容的应用搭配使用。本指南介绍了如何在原生广告中启用自定义点击手势。

使用所选的滑动方向初始化 GADNativeAdCustomClickGestureOptions 实例。您还需要指明是否允许将点按视为点击。

  • 自定义点击手势默认处于停用状态。

  • 停用后,只有点按操作会被视为点击。

  • 启用后,滑动手势将被视为点击,您可以指定点按操作是否仍可被视为点击。

以下示例展示了如何实现向右滑动这一自定义手势,同时保留正常的点按行为。

GADNativeAdCustomClickGestureOptions *swipeGestureOptions = [[GADNativeAdCustomClickGestureOptions alloc]
        initWithSwipeGestureDirection:UISwipeGestureRecognizerDirectionRight
                          tapsAllowed:YES];

// The following sample ad unit ID has been enabled for custom click gestures
// and can be used for testing.
self.adLoader = [[GADAdLoader alloc]
        initWithAdUnitID:@"/21775744923/example/native"
      rootViewController:self
                 adTypes:@[ GADAdLoaderAdTypeNative ]
                 options:@[ swipeGestureOptions ]];

监听滑动手势事件

当系统记录到滑动点击手势时,Google 移动广告 SDK 会对 GADNativeAdDelegate 调用 nativeAdDidRecordSwipeGestureClick: 代理方法,同时也会调用现有的 nativeAdDidRecordClick: 代理方法。

#pragma mark - GADNativeAdDelegate implementation

// Called when a swipe gesture click is recorded.
- (void)nativeAdDidRecordSwipeGestureClick:(GADNativeAd *)nativeAd {
  NSLog(@"A swipe gesture click has occurred.");
}

// Called when a swipe gesture click or a tap click is recorded, as configured in
// GADNativeAdCustomClickGestureOptions.
- (void)nativeAdDidRecordClick:(GADNativeAd *)nativeAd {
  NSLog(@"A swipe gesture click or tap click has occurred.");
}

中介

自定义点击手势仅适用于 Google 移动广告 SDK 呈现的原生广告。需要使用第三方 SDK 进行呈现的广告来源不会响应自定义点击方向设置。