Native ad options

Native ads have many advanced features that allow you to make additional customizations and make the best possible ad experience. This guide shows you how to use the advanced features of native ads.

Prerequisites

Asset controls

Preferred media aspect ratio controls

Media Aspect Ratio Controls let you specify a preference for the aspect ratio of ad creatives.

Set GADNativeAdMediaAdLoaderOptions mediaAspectRatio with a GADMediaAspectRatio

  • When unset, the returned ad can have any media aspect ratio.

  • When set, you will be able to improve the user experience by specifying the preferred type of aspect ratio.

The following example instructs the SDK to prefer a return image or video with a specific aspect ratio.

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

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

Image download control

Image download control lets you decide if image assets or only URIs are returned by the SDK.

Set GADNativeAdImageAdLoaderOptions disableImageLoading with a BOOL value.
  • Image download control are disabled by default.

  • When disabled, the Google Mobile Ads SDK populates both the image and the URI for you.

  • When enabled, the SDK instead populates just the URI, allowing you to download the actual images at your discretion.

The following example instructs the SDK to return just the 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 ]];

Image payload controls

Some ads have a series of images rather than just one. Use this feature to indicate whether your app is prepared to display all the images or just one.

Set GADNativeAdImageAdLoaderOptions shouldRequestMultipleImages with a BOOL value.
  • Image payload controls are disabled by default.

  • When disabled, your app instructs the SDK to provide just the first image for any assets that contain a series.

  • When enabled, your app indicates that it is prepared to display all the images for any assets that have more than one.

The following example instructs the SDK to return multiple image assets.

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

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

AdChoices placements

AdChoices position controls

The AdChoices position controls lets you choose which corner to render the AdChoices icon.

Set GADNativeAdViewAdOptions preferredAdChoicesPosition with a GADAdChoicesPosition value.

  • If unset, the AdChoices icon position is set to the top right.

  • If set, AdChoices is placed at the custom position as requested.

The following example demonstrates how to set a custom AdChoices image position.

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 custom view

The AdChoices custom view feature lets you position the AdChoices icon in a custom location. This is different from AdChoices position controls, which only allows specification of one of the four corners.

Set the GADNativeAd.adChoicesView property with a GADAdChoicesView prior to rendering and the AdChoices content will render inside the GADAdChoicesView.

The following example demonstrates how to set a custom AdChoices view. The AdChoices icon will render inside the 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;
}

Video controls

Start mute behavior

The start muted behavior lets you disable or enable a video's starting audio.

Set GADVideoOptions startMuted with a BOOL value.
  • The start muted behavior is enabled by default.

  • When disabled, your app requests the video should begin with audio.

  • When enabled, your app requests that the video should begin with audio muted.

The following example shows how to start the video with un-muted audio.

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

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

Custom playback controls

This lets you request custom video input controls to play, pause, or mute the video.

Set GADVideoOptions customControlsRequested with a BOOL value.
  • Custom playback control are disabled by default.

  • When disabled, your video will show SDK rendered input controls.

  • If the ad does have video content and custom controls are enabled, you should then display your custom controls along with the ad, as the ad won't show any controls itself. The controls can then call the relevant methods on the GADVideoController.

The following example shows how request a video with custom playback controls.

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

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

Check if custom controls are enabled

Because it's not known at request time whether the returned ad will allow custom video controls, you must check whether it has custom controls enabled.

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
}

自定义点击手势

自定义点击手势是一项原生广告功能,支持将广告视图上的滑动操作注册为广告点击。它旨在与使用滑动手势进行内容导航的应用搭配使用。本指南介绍了如何在原生广告上启用自定义点击手势。

使用您选择的滑动方向初始化 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 ]];

监听滑动手势事件

记录滑动手势点击时,除了现有的 nativeAdDidRecordClick: 委托方法之外,Google 移动广告 SDK 还会在 GADNativeAdDelegate 上调用 nativeAdDidRecordSwipeGestureClick: 委托方法。

#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 进行呈现的广告联盟不会响应自定义点击方向设置。