原生广告具有许多高级功能,让您可以进行额外的自定义设置,尽可能提供最佳的广告体验。本指南介绍了如何使用原生广告的高级功能。
前提条件
- 集成原生广告格式。
素材资源控件
首选媒体宽高比控件
通过媒体宽高比控件,您可以指定广告素材的宽高比偏好设置。
使用 GADMediaAspectRatio
设置 GADNativeAdMediaAdLoaderOptions mediaAspectRatio
如果未设置此政策,返回的广告可采用任意媒体宽高比。
设置完毕后,您可以通过指定首选宽高比类型来提升用户体验。
以下示例指示 SDK 优先选择具有特定宽高比的返回图片或视频。
GADNativeAdMediaAdLoaderOptions *nativeOption = [[GADNativeAdMediaAdLoaderOptions alloc] init];
nativeOption.mediaAspectRatio = GADMediaAspectRatioAny;
GADAdLoader* adLoader = [[GADAdLoader alloc] initWithAdUnitID:@"/6499/example/native"
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.
SetGADNativeAdImageAdLoaderOptions 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:@"/6499/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:@"/6499/example/native"
rootViewController:self
adTypes:@[ GADAdLoaderAdTypeNative ]
options:@[ nativeOption ]];
“广告选择”展示位置
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:@"/6499/example/native"
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;
}
视频控件
Start mute behavior
The start muted behavior lets you disable or enable a video's starting audio.
SetGADVideoOptions 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:@"/6499/example/native"
rootViewController:self
adTypes:@[ GADAdLoaderAdTypeNative ]
options:@[ nativeOptions ]];
Custom playback controls
This lets you request custom video input controls to play, pause, or mute the video.
SetGADVideoOptions customControlsRequested
with a BOOL
value.
Custom playback control are disabled by default.
When disabled, your video will show SDK rendered input controls.
- When enabled you can then use
GADVideoController play
,GADVideoController pause
, andGADVideoController setMute
to control the video ad.
- 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:@"/6499/example/native"
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
}
Custom click gestures
Custom click gestures is a native ads feature that enables swipes on ad views to be registered as ad clicks. It is designed to work with apps that use swipe gestures for content navigation. This guide shows how to enable custom click gestures on your native ads.
Initialize aGADNativeAdCustomClickGestureOptions
instance with your selected swipe direction. You also need to indicate whether
you want taps to be allowed as clicks.
Custom click gestures is disabled by default.
When disabled, only taps will count as clicks.
When enabled, swipe gestures will be counted as clicks, and you can specify whether taps can still count as clicks.
The following example shows you how to implement a custom swipe gesture to the right and preserves normal tap behavior.
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:@"/6499/example/native"
rootViewController:self
adTypes:@[ GADAdLoaderAdTypeNative ]
options:@[ swipeGestureOptions ]];
Listen for swipe gesture events
When a swipe gesture click is recorded, the Google Mobile Ads SDK invokes the
nativeAdDidRecordSwipeGestureClick:
delegate method on GADNativeAdDelegate
, in addition to the existing nativeAdDidRecordClick:
delegate method.
#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.");
}
Mediation
Custom click gestures only work on native ads that Google Mobile Ads SDK renders. Ad sources that require third-party SDKs for rendering, don't respond to the custom click directions setting.