原生广告选项

原生广告具有许多高级功能 从而尽可能提供最佳的广告体验。本指南介绍了 如何使用原生广告的高级功能

前提条件

素材资源控件

首选媒体宽高比控件

通过媒体宽高比控件,您可以指定 广告素材。

设置“GADNativeAdMediaAdLoaderOptions mediaAspectRatio” (具有 GADMediaAspectRatio)。

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

  • 设置后,您可以通过指定 首选宽高比类型

以下示例指示 SDK 优先选择带有 特定宽高比。

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

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

图片下载控件

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

设置“GADNativeAdImageAdLoaderOptions disableImageLoading” 并设置为 BOOL 值。
  • 默认情况下,图片下载控件处于停用状态。

  • 停用后,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 ]];

映像载荷控件

有些广告会有一系列图片,而不只是一张图片。使用此功能可实现以下目的: 指示您的应用是准备好显示所有图片还是仅显示一张图片。

设置“GADNativeAdImageAdLoaderOptions shouldRequestMultipleImages” 并设置为 BOOL 值。
  • 默认情况下,图片载荷控件处于停用状态。

  • 停用后,您的应用会指示 SDK 仅提供 第一张图片。

  • 启用后,您的应用会表明已准备好展示所有图片 。

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

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

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

“广告选项”展示位置

“广告选项”位置控件

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

设置“GADNativeAdViewAdOptions preferredAdChoicesPosition” (带有 GADAdChoicesPosition) 值。

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

  • 如果已设置,则会按照请求将“广告选项”放置在自定义位置。

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

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

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

“广告选择”自定义视图

<ph type="x-smartling-placeholder">

通过“广告选项”自定义视图功能,您可以将“广告选择”图标放置在 自定义位置。这与“广告选择”位置控件不同 用于指定四个角中的其中一个。

设置 GADNativeAd.adChoicesView 属性:GADAdChoicesView 并且“广告选择”内容会在 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: GADAdLoader, didReceive nativeAd: GADNativeAd) {
    refreshAdButton.isEnabled = true

    ...
    // Define a custom position for the AdChoices icon.
    let customRect = CGRect(x: 100, y: 100, width: 15, height: 15)
    let customAdChoicesView = GADAdChoicesView(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;
}

视频控件

启动静音行为

通过静音开始播放行为,您可以停用或启用视频的起始音频。

设置“GADVideoOptions startMuted” 并设置为 BOOL 值。
  • 默认情况下,静音启动行为处于启用状态。

  • 停用此功能后,您的应用请求的视频应以 音频。

  • 启用后,您的应用会请求视频开始时应设为静音。

以下示例展示了如何在取消静音的情况下开始播放视频。

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

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

自定义播放控件

<ph type="x-smartling-placeholder">

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

设置“GADVideoOptions customControlsRequested” 并设置为 BOOL 值。
  • 自定义播放控件默认处于停用状态。

  • 停用后,您的视频将显示 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: GADAdLoader, didReceive nativeAd: GADNativeAd) {
        let videoController = nativeAd.mediaContent.videoController
        let canShowCustomControls = videoController?.customControlsEnabled() == true
}

自定义点击手势

<ph type="x-smartling-placeholder">

自定义点击手势是一项原生广告功能,可让您在广告视图上滑动手指, 被记录为广告点击它适用于使用滑动手势的应用 用于内容导航的手势。本指南介绍了如何启用自定义点击 手势。

初始化 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 会调用 nativeAdDidRecordSwipeGestureClick: GADNativeAdDelegate 上的委托方法 ,除了现有的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 不响应自定义点击路线设置。