Native Ad Options
Ad Request Timeout (default: 60 seconds)
If no ad response is received within the specified time (in seconds) after the ad request, the existing request is invalidated and the adLoader:didFailWithError:responseInfo: method of GFPAdLoaderDelegate is called.
- Swift
- Objective-C
self.adLoader?.requestTimeoutInterval = ...
self.adLoader.requestTimeoutInterval = ...
GFPNativeAdRenderingSetting
This section covers only the most commonly used options. For the full list of all options provided by GFPNativeAdRenderingSetting, refer to Native Rendering Options.
DFP adChoicesView Position Setting
The DFP adChoiceView is rendered as an overlay. Therefore, one of the four corners where the adChoicesView will be automatically inserted must be left empty. The default position of the DFP adChoicesView is the top-right corner, and it can be configured via GFPNativeAdRenderingSetting's preferredAdChoicesViewPosition.
- Swift
- Objective-C
let renderingSetting = GFPNativeAdRenderingSetting()
renderingSetting.preferredAdChoicesViewPosition = .topRightCorner
let nativeOption = GFPAdNativeOptions()
nativeOption.renderingSetting = renderingSetting
GFPNativeAdRenderingSetting *setting = [[GFPNativeAdRenderingSetting alloc] init];
setting.preferredAdChoicesViewPosition = GFPAdChoicesViewPositionTopRightCorner;
GFPAdNativeOptions *nativeOptions = [[GFPAdNativeOptions alloc] init];
nativeOptions.renderingSetting = setting;
The Google documentation states that manually registering an adChoiceView renders it directly onto the registered view rather than as an overlay, but this does not yet work correctly. If you want the adChoiceView rendered at a position other than the top-right, this setting is required.
Using Native Ads Without a Media View
To use a native ad without a media view (for example, a native ad composed only of an icon and a title/CTA button), set hasMediaView = NO on GFPNativeAdRenderingSetting. (The default value is YES.)
If the presence of a media view in the native view does not match the GFPNativeAdRenderingSetting.hasMediaView state, an error will occur at the time of native ad rendering.
- Swift
- Objective-C
let setting = GFPNativeAdRenderingSetting()
setting.hasMediaView = false
let nativeOption = GFPAdNativeOptions()
nativeOption.renderingSetting = setting
GFPNativeAdRenderingSetting *setting = [[GFPNativeAdRenderingSetting alloc] init];
setting.hasMediaView = NO;
GFPAdNativeOptions *nativeOptions = [[GFPAdNativeOptions alloc] init];
nativeOptions.renderingSetting = setting;
Native Ad Lazy Loading
Enabling Lazy Loading
When lazy loading is enabled in the native rendering options, the native ad view object and text assets are first delivered via
GFPAdLoaderDelegate.adLoader(_ unifiedAdLoader: GFPAdLoader!, didReceive nativeAd: GFPNativeAd!),
while the media view, icon view, and other assets are loaded asynchronously.
- Swift
- Objective-C
self.adLoader = GFPAdLoader(unitID: "UnitId", rootViewController: self, adParam: adParam)
let nativeOption = GFPAdNativeOptions()
nativeOptions.renderingSetting.useLazyMediaLoading = true
self.adLoader?.setNativeDelegate(self, nativeOptions: nativeOption)
self.adLoader?.delegate = self
self.adLoader?.loadAd()
self.adLoader = [[GFPAdLoader alloc] initWithUnitID:self.unitID
rootViewController:self
adParam:adParam];
GFPAdNativeOptions *nativeOptions = [[GFPAdNativeOptions alloc] init];
nativeOptions.renderingSetting.useLazyMediaLoading = YES;
[self.adLoader setNativeDelegate:self nativeOptions:nativeOptions];
// Request ad
self.adLoader.delegate = self;
[self.adLoader loadAd];
Showing a Placeholder
A placeholder can be shown while media is loading asynchronously. The placeholder appears directly on top of the ad view and is automatically scaled to fit the view element's size.
- Swift
- Objective-C
func adLoader(_ unifiedAdLoader: GFPAdLoader!, didReceive nativeAd: GFPNativeAd!) {
self.nativeAdView.nativeAd = nativeAd
self.nativeAdView.mediaView?.showPlaceholder { imageView in
imageView.image = UIImage(named: "my_media_placeholder")
imageView.contentMode = .scaleAspectFill
imageView.clipsToBounds = true
}
self.nativeAdView.showIconPlaceholder { imageView in
imageView.image = UIImage(named: "my_icon_placeholder")
imageView.contentMode = .scaleAspectFill
imageView.clipsToBounds = true
}
- (void)adLoader:(GFPAdLoader *)unifiedAdLoader didReceiveNativeAd:(GFPNativeAd *)nativeAd {
self.nativeAdView.nativeAd = nativeAd
[self.nativeAdView.mediaView showPlaceholderWith:^(UIImageView *imageView) {
imageView.image = [UIImage imageNamed:@"my_media_placeholder"];
imageView.contentMode = UIViewContentModeScaleAspectFill;
imageView.clipsToBounds = YES;
}];
[self.nativeAdView showIconPlaceholderWith:^(UIImageView *imageView) {
imageView.image = [UIImage imageNamed:@"my_icon_placeholder"];
imageView.contentMode = UIViewContentModeScaleAspectFill;
imageView.clipsToBounds = YES;
}];
}
Async Media Loading Callbacks
When all media (media view, icon view, etc.) have finished loading asynchronously,
nativeAdDidLoadMediaData(_:) of GFPNativeAdDelegate is called.
If any asset fails to load,
nativeAdDidFail(toLoadMediaData:) is called.
- Swift
- Objective-C
// GFPNativeAdDelegate
func nativeAdDidLoadMediaData(_ nativeAd: GFPNativeAd) {
self.nativeAdView.removePlaceholders() // Optional for safety.
}
func nativeAdDidFail(toLoadMediaData nativeAd: GFPNativeAd) {
// Example showing new placeholders on failure.
self.nativeAdView.mediaView?.showPlaceholder { imageView in
imageView.image = UIImage(named: "my_fallback_media_placeholder")
imageView.contentMode = .scaleAspectFill
imageView.clipsToBounds = true
}
self.nativeAdView.showIconPlaceholder { imageView in
imageView.image = UIImage(named: "my_fallback_icon_placeholder")
imageView.contentMode = .scaleAspectFill
imageView.clipsToBounds = true
}
}
// GFPNativeAdDelegate
- (void)nativeAdDidLoadMediaData:(GFPNativeAd *)nativeAd {
[self.nativeAdView removePlaceholders]; // Optional for safety.
}
- (void)nativeAdDidFailToLoadMediaData:(GFPNativeAd *)nativeAd {
// Example showing new placeholders on failure.
[self.nativeAdView.mediaView showPlaceholderWith:^(UIImageView *imageView) {
imageView.image = [UIImage imageNamed:@"my_fallback_media_placeholder"];
imageView.contentMode = UIViewContentModeScaleAspectFill;
imageView.clipsToBounds = YES;
}];
[self.nativeAdView showIconPlaceholderWith:^(UIImageView *imageView) {
imageView.image = [UIImage imageNamed:@"my_fallback_icon_placeholder"];
imageView.contentMode = UIViewContentModeScaleAspectFill;
imageView.clipsToBounds = YES;
}];
}
GFPContentInfo
When applying a NativeNormal type ad as a Communication Ad, you must inject ContentInfo.
- Swift
- Objective-C
let adParam = GFPAdParam()
adParam.contentInfo = GFPContentInfo(
sourceType: "0001",
subtype: "menu",
sourceId: "30907206:7")
GFPContentInfo *contentInfo = [[GFPContentInfo alloc] initWithSourceType:@"0001" subtype:@"menu" sourceId:@"30907206:7"];
adParam.contentInfo = contentInfo;