原生广告是通过界面组件向用户呈现的广告素材资源 都是平台原生的它们使用您已用过的类进行展示 ,并且能以与应用的视觉设计相称的格式呈现。
加载原生广告时,您的应用会收到一个包含其素材资源的广告对象, 然后应用(而不是 Google 移动广告 SDK) 来显示它们
一般来说,要成功植入原生广告,需要完成两部分工作: 使用 SDK 加载广告,然后在您的应用中展示广告内容。
本页介绍了如何使用 SDK 加载 原生广告。
前提条件
- 完成入门指南。
始终使用测试广告进行测试
在构建和测试应用时,请务必使用测试广告 实际投放的广告。
要加载测试广告,最简便的方法就是使用我们的专用测试广告单元 ID iOS 设备上的原生广告:
ca-app-pub-3940256099942544/3986624511
该测试广告单元 ID 已经过专门配置,可为每个请求返回测试广告,而且您可以 在自己应用的编码、测试和调试过程中使用。只需确保您 请将其替换为您自己的广告单元 ID,然后再发布应用。
如需详细了解 Google 移动广告 SDK 的测试广告如何运作,请参阅 测试广告。
加载广告
原生广告使用
GADAdLoader
类,该类根据
GADAdLoaderDelegate
协议。
初始化广告加载器
您必须先初始化广告加载器,然后才能加载广告。
以下代码演示了如何初始化 GADAdLoader
:
Swift
adLoader = GADAdLoader(adUnitID: "ca-app-pub-3940256099942544/3986624511",
// The UIViewController parameter is optional.
rootViewController: rootViewController,
adTypes: [ .native ],
options: [ ... ad loader options objects ... ])
adLoader.delegate = self
Objective-C
self.adLoader = [[GADAdLoader alloc]
initWithAdUnitID:@"ca-app-pub-3940256099942544/3986624511"
// The UIViewController parameter is nullable.
rootViewController:rootViewController
adTypes:@[ GADAdLoaderAdTypeNative ]
options:@[ ... ad loader options objects ... ]];
self.adLoader.delegate = self;
您需要一个广告单元 ID(可以使用测试 ID)、要在
adTypes
数组用于指定要请求的原生格式,以及
options
参数中设置的选项。可能的
options
参数的值可以在设置原生广告
选项页面。
adTypes
数组应包含
此常量:
实现广告加载程序委托
广告加载程序代理需要实现特定于您的广告类型的协议。
对于原生广告,GADNativeAdLoaderDelegate
协议包括一条消息
在原生广告加载完毕后发送给代理
Swift
public func adLoader(_ adLoader: GADAdLoader,
didReceive nativeAd: GADNativeAd)
Objective-C
- (void)adLoader:(GADAdLoader *)adLoader
didReceiveNativeAd:(GADNativeAd *)nativeAd;
提出广告请求
初始化 GADAdLoader
后,调用其 loadRequest:
方法以
请求广告:
Swift
adLoader.load(GADRequest())
Objective-C
[self.adLoader loadRequest:[GADRequest request]];
通过
loadRequest:
方法
GADAdLoader
接受相同的
GADRequest
对象作为横幅广告和插页式广告。您可以使用请求对象将
定位信息,就像
会与其他广告类型不同
加载多个广告(可选)
要在单个请求中加载多个广告,请将
GADMultipleAdsAdLoaderOptions
对象(在初始化 GADAdLoader
时)。
Swift
let multipleAdOptions = GADMultipleAdsAdLoaderOptions()
multipleAdOptions.numberOfAds = 5;
adLoader = GADAdLoader(adUnitID: "ca-app-pub-3940256099942544/3986624511",
// The UIViewController parameter is optional.
rootViewController: self,
adTypes: [ .native ],
options: [ multipleAdOptions ])
Objective-C
GADMultipleAdsAdLoaderOptions *multipleAdsOptions =
[[GADMultipleAdsAdLoaderOptions alloc] init];
multipleAdsOptions.numberOfAds = 5;
self.adLoader = [[GADAdLoader alloc]
initWithAdUnitID:@"ca-app-pub-3940256099942544/3986624511"
// The UIViewController parameter is nullable.
rootViewController:rootViewController
adTypes:@[ GADAdLoaderAdTypeNative ]
options:@[ multipleAdsOptions ]];
每个请求的广告数量上限为 5 个 SDK 会返回所请求的广告的确切数量。
尽管来自 Google 服务器的广告来自不同的平台, 预留广告资源或第三方买方并不一定是唯一的
如果您使用中介功能,请勿使用 GADMultipleAdsAdLoaderOptions
类。
因为针对多个原生广告的请求目前不适用于
已针对中介进行配置
确定加载完成时间
在应用调用 loadRequest:
后,它可以使用
调用:
adLoader:didFailToReceiveAdWithError:
位于“GADAdLoaderDelegate
”中adLoader:didReceiveNativeAd:
位于“GADNativeAdLoaderDelegate
”中
针对单个广告的请求会导致对这些方法之一进行一次调用。
请求多个广告将导致至少对上述调用进行一次回调 方法,但不超过请求的广告数量上限。
此外,GADAdLoaderDelegate
还提供 adLoaderDidFinishLoading
回调。此委托方法用于指明广告加载程序已完成加载
广告,并且不会针对该请求报告其他广告或错误。这里有
在一次加载多个原生广告时如何使用该示例:
Swift
class ViewController: UIViewController, GADNativeAdLoaderDelegate {
var adLoader: GADAdLoader!
override func viewDidLoad() {
super.viewDidLoad()
let multipleAdOptions = GADMultipleAdsAdLoaderOptions()
multipleAdOptions.numberOfAds = 5;
adLoader = GADAdLoader(adUnitID: "ca-app-pub-3940256099942544/3986624511",
// The UIViewController parameter is optional.
rootViewController: rootViewController,
adTypes: [ .native ],
options: [ multipleAdOptions ])
adLoader.delegate = self
adLoader.load(GADRequest())
}
func adLoader(_ adLoader: GADAdLoader,
didReceive nativeAd: GADNativeAd) {
// A native ad has loaded, and can be displayed.
}
func adLoaderDidFinishLoading(_ adLoader: GADAdLoader) {
// The adLoader has finished loading ads, and a new request can be sent.
}
}
Objective-C
@interface ViewController () <GADNativeAdLoaderDelegate, GADVideoControllerDelegate>
@property(nonatomic, strong) GADAdLoader *adLoader;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
GADMultipleAdsAdLoaderOptions *multipleAdsOptions =
[[GADMultipleAdsAdLoaderOptions alloc] init];
multipleAdsOptions.numberOfAds = 5;
self.adLoader = [[GADAdLoader alloc]
initWithAdUnitID:@"ca-app-pub-3940256099942544/3986624511"
// The UIViewController parameter is nullable.
rootViewController:rootViewController
adTypes:@[ GADAdLoaderAdTypeNative ]
options:@[ multipleAdsOptions ]];
self.adLoader.delegate = self;
[self.adLoader loadRequest:[GADRequest request]];
}
- (void)adLoader:(GADAdLoader *)adLoader
didReceiveNativeAd:(GADNativeAd *)nativeAd {
// A native ad has loaded, and can be displayed.
}
- (void)adLoaderDidFinishLoading:(GADAdLoader *) adLoader {
// The adLoader has finished loading ads, and a new request can be sent.
}
@end
处理失败的请求
上述协议扩展了 GADAdLoaderDelegate
协议,该协议定义了
消息。
Swift
public func adLoader(_ adLoader: GADAdLoader,
didFailToReceiveAdWithError error: NSError)
Objective-C
- (void)adLoader:(GADAdLoader *)adLoader
didFailToReceiveAdWithError:(NSError *)error;
接收有关原生广告事件的通知
若要在发生与原生广告互动相关的事件时收到通知,请将委托 属性:
Swift
nativeAd.delegate = self
Objective-C
nativeAd.delegate = self;
然后,实施
GADNativeAdDelegate
以接收以下委托调用:
Swift
func nativeAdDidRecordImpression(_ nativeAd: GADNativeAd) {
// The native ad was shown.
}
func nativeAdDidRecordClick(_ nativeAd: GADNativeAd) {
// The native ad was clicked on.
}
func nativeAdWillPresentScreen(_ nativeAd: GADNativeAd) {
// The native ad will present a full screen view.
}
func nativeAdWillDismissScreen(_ nativeAd: GADNativeAd) {
// The native ad will dismiss a full screen view.
}
func nativeAdDidDismissScreen(_ nativeAd: GADNativeAd) {
// The native ad did dismiss a full screen view.
}
func nativeAdWillLeaveApplication(_ nativeAd: GADNativeAd) {
// The native ad will cause the app to become inactive and
// open a new app.
}
Objective-C
- (void)nativeAdDidRecordImpression:(GADNativeAd *)nativeAd {
// The native ad was shown.
}
- (void)nativeAdDidRecordClick:(GADNativeAd *)nativeAd {
// The native ad was clicked on.
}
- (void)nativeAdWillPresentScreen:(GADNativeAd *)nativeAd {
// The native ad will present a full screen view.
}
- (void)nativeAdWillDismissScreen:(GADNativeAd *)nativeAd {
// The native ad will dismiss a full screen view.
}
- (void)nativeAdDidDismissScreen:(GADNativeAd *)nativeAd {
// The native ad did dismiss a full screen view.
}
- (void)nativeAdWillLeaveApplication:(GADNativeAd *)nativeAd {
// The native ad will cause the app to become inactive and
// open a new app.
}
最佳做法
加载广告时,请遵循以下规则。
在列表中使用原生广告的应用应预缓存广告列表。
预缓存广告时,请清除缓存,并在一小时后重新加载。
在上一个请求之前,不要对
GADAdLoader
再次调用loadRequest:
完成加载,如adLoaderDidFinishLoading:
所示。
展示您的广告
加载广告后,剩下的工作就是将其展示给您的用户。 请访问原生高级广告 指南了解具体方法。