原生广告选项

请选择平台Android iOS

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

前提条件

素材资源控件

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

首选媒体宽高比控件

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

使用 GADMediaAspectRatio 设置 GADNativeAdMediaAdLoaderOptions mediaAspectRatio

  • 如果未设置,则返回的广告可以具有任何媒体宽高比。

  • 设置后,您可以通过指定首选的宽高比类型来改善用户体验。

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

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

GADAdLoader* adLoader = [[GADAdLoader alloc] initWithAdUnitID:@"ca-app-pub-3940256099942544/3986624511"
                                           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:@"ca-app-pub-3940256099942544/3986624511"
                                           rootViewController:self
                                                      adTypes:@[ GADAdLoaderAdTypeNative ]
                                                      options:@[ nativeOption ]];

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

  • 如果此属性处于停用状态,则您的应用会指示 SDK 仅提供包含一系列图片的任何素材资源的第一张图片。

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

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

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

GADAdLoader* adLoader = [[GADAdLoader alloc] initWithAdUnitID:@"ca-app-pub-3940256099942544/3986624511"
                                           rootViewController:self
                                                      adTypes:@[ GADAdLoaderAdTypeNative ]
                                                      options:@[ nativeOption ]];

“广告选项”图标位置

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

“广告选项”位置控制

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

使用 GADAdChoicesPosition 值设置 GADNativeAdViewAdOptions preferredAdChoicesPosition

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

  • 如果设置了此字段,AdChoices 会放置在所请求的自定义位置。

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

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

GADAdLoader* adLoader = [[GADAdLoader alloc] initWithAdUnitID:@"ca-app-pub-3940256099942544/3986624511"
                                           rootViewController:self
                                                      adTypes:@[ GADAdLoaderAdTypeNative ]
                                                      options:@[ nativeOptions ]];

“广告选项”自定义视图

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

在渲染之前,使用 GADAdChoicesView 设置 GADNativeAd.adChoicesView 属性,然后 AdChoices 内容将渲染到 GADAdChoicesView 中。

以下示例演示了如何设置自定义 AdChoices 视图。“广告选项”图标将呈现在 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:@"ca-app-pub-3940256099942544/3986624511"
                                           rootViewController:self
                                                      adTypes:@[ GADAdLoaderAdTypeNative ]
                                                      options:@[ nativeOptions ]];

自定义播放控件

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

使用 BOOL 值设置 GADVideoOptions customControlsRequested

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

  • 如果停用此功能,您的视频将显示由 SDK 渲染的输入控件。

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

GADVideoController

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

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

GADAdLoader* adLoader = [[GADAdLoader alloc] initWithAdUnitID:@"ca-app-pub-3940256099942544/3986624511"
                                           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:@"ca-app-pub-3940256099942544/3986624511"
      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 进行呈现的广告来源不会响应自定义点击方向设置。