原生高级广告

展示系统定义的原生广告格式

加载原生广告后,您的应用会通过 GADAdLoaderDelegate 协议消息之一接收原生广告对象。然后,就由您的应用负责展示广告了,尽管不一定要立即展示广告。为了更轻松地展示系统定义的广告格式,SDK 提供了一些实用资源。

想要试试更方便的入门方法吗?

测试版:试用原生广告模板

GADUnifiedNativeAdView

对于 GADUnifiedNativeAd,有一个对应的“广告视图”类:GADUnifiedNativeAdView。此广告视图类是 UIView,供发布商用来展示广告。例如,单个 GADUnifiedNativeAdView 可以展示 GADUnifiedNativeAd 的单个实例。用于展示该广告的素材资源的每个 UIView 对象都应为 GADUnifiedNativeAdView 对象的子视图。

例如,如果您在 UITableView 中展示广告,则其中一个单元格的视图层次结构可能如下所示:

GADUnifiedNativeAdView 类还提供了用于注册每个单独的素材资源所用视图的 IBOutlets,以及一个用于注册 GADUnifiedNativeAd 对象本身的方法。如果以这种方式注册视图,SDK 就可以自动处理诸如以下任务:

  • 记录点击次数。
  • 记录展示次数(当第一个像素出现在屏幕上时)。
  • 显示广告选择叠加层。

广告选择叠加层

对于通过AdMob补余广告、Ad Exchange 或 AdSense 投放的非直销原生广告,SDK 会添加一个广告选择叠加层。请在原生广告视图中任选您喜欢的一角留出空间,用于展示自动插入的广告选择徽标。此外,在将广告选择叠加层放置在广告内容上时,请确保该图标不会被遮挡,让用户一眼就能看到。如需详细了解此叠加层的外观和功能,请参阅程序化原生广告植入指南

广告标示

在展示程序化原生广告时,您必须展示广告标示,以指明该视图是广告。

代码示例

让我们来了解一下如何使用从 xib 文件动态加载的视图来展示原生广告。此方法非常适合使用经过配置的 GADAdLoaders 请求多种广告格式的情况。

布置 UIViews

第一步是布置展示原生广告素材资源的 UIViews。与创建任何其他 xib 文件时一样,您可以在 Interface Builder 中执行此操作。原生广告的布局方式可能如下所示:

请注意图片右上角的自定义类值。该值设置为

GADUnifiedNativeAdView。这是用于显示 GADUnifiedNativeAd 的广告视图类。

此外,您还需要为 GADMediaView 设置自定义类,用于显示广告视频或图片。

将输出口与视图相关联

在视图设置完毕并已给布局分配适当的广告视图类后,请将广告视图的素材资源输出口与创建的 UIViews 关联起来。 以下是将广告视图的素材资源输出口与为广告创建的 UIViews 相关联的方法:

在输出口面板中,GADUnifiedNativeAdView 中的输出口已与 Interface Builder 中布置的 UIViews 相关联。这使 SDK 可以了解哪个 UIView 显示哪个素材资源。同时请务必注意,这些输出口表示广告中可点击的视图。

展示广告

完成布局并关联输出口后,最后一步是向应用添加代码,以便在加载广告后进行展示。以下是在上面定义的视图中展示广告的方法:

Swift

// Mark: - GADUnifiedNativeAdLoaderDelegate
func adLoader(_ adLoader: GADAdLoader, didReceive nativeAd: GADUnifiedNativeAd) {
  print("Received unified native ad: \(nativeAd)")
  refreshAdButton.isEnabled = true
  // Create and place ad in view hierarchy.
  let nibView = Bundle.main.loadNibNamed("UnifiedNativeAdView", owner: nil, options: nil)?.first
  guard let nativeAdView = nibView as? GADUnifiedNativeAdView else {
    return
  }
  setAdView(nativeAdView)

  // Associate the native ad view with the native ad object. This is
  // required to make the ad clickable as well as populate the media view.
  nativeAdView.nativeAd = nativeAd

  // Populate the native ad view with the native ad assets.
  // The headline is guaranteed to be present in every native ad.
  (nativeAdView.headlineView as? UILabel)?.text = nativeAd.headline

  // These assets are not guaranteed to be present. Check that they are before
  // showing or hiding them.
  (nativeAdView.bodyView as? UILabel)?.text = nativeAd.body
  nativeAdView.bodyView?.isHidden = nativeAd.body == nil

  (nativeAdView.callToActionView as? UIButton)?.setTitle(nativeAd.callToAction, for: .normal)
  nativeAdView.callToActionView?.isHidden = nativeAd.callToAction == nil

  (nativeAdView.iconView as? UIImageView)?.image = nativeAd.icon?.image
  nativeAdView.iconView?.isHidden = nativeAd.icon == nil

  (nativeAdView.starRatingView as? UIImageView)?.image = imageOfStars(fromStarRating:nativeAd.starRating)
  nativeAdView.starRatingView?.isHidden = nativeAd.starRating == nil

  (nativeAdView.storeView as? UILabel)?.text = nativeAd.store
  nativeAdView.storeView?.isHidden = nativeAd.store == nil

  (nativeAdView.priceView as? UILabel)?.text = nativeAd.price
  nativeAdView.priceView?.isHidden = nativeAd.price == nil

  (nativeAdView.advertiserView as? UILabel)?.text = nativeAd.advertiser
  nativeAdView.advertiserView?.isHidden = nativeAd.advertiser == nil

  // In order for the SDK to process touch events properly, user interaction
  // should be disabled.
  nativeAdView.callToActionView?.isUserInteractionEnabled = false
}

Objective-C

#pragma mark GADUnifiedNativeAdLoaderDelegate implementation

- (void)adLoader:(GADAdLoader *)adLoader didReceiveUnifiedNativeAd:(GADUnifiedNativeAd *)nativeAd {
  NSLog(@"Received unified native ad: %@", nativeAd);
  self.refreshButton.enabled = YES;

  // Create and place ad in view hierarchy.
  GADUnifiedNativeAdView *nativeAdView =
      [[NSBundle mainBundle] loadNibNamed:@"UnifiedNativeAdView" owner:nil options:nil].firstObject;
  [self setAdView:nativeAdView];

  // Associate the native ad view with the native ad object. This is
  // required to make the ad clickable as well as populate the media view.
  nativeAdView.nativeAd = nativeAd;

  // Populate the native ad view with the native ad assets.
  // The headline is guaranteed to be present in every native ad.
  ((UILabel *)nativeAdView.headlineView).text = nativeAd.headline;

  // These assets are not guaranteed to be present. Check that they are before
  // showing or hiding them.
  ((UILabel *)nativeAdView.bodyView).text = nativeAd.body;
  nativeAdView.bodyView.hidden = nativeAd.body ? NO : YES;

  [((UIButton *)nativeAdView.callToActionView)setTitle:nativeAd.callToAction
                                              forState:UIControlStateNormal];
  nativeAdView.callToActionView.hidden = nativeAd.callToAction ? NO : YES;

    ((UIImageView *)nativeAdView.iconView).image = nativeAd.icon.image;
  nativeAdView.iconView.hidden = nativeAd.icon ? NO : YES;

  ((UIImageView *)nativeAdView.starRatingView).image = [self imageForStars:nativeAd.starRating];
  nativeAdView.starRatingView.hidden = nativeAd.starRating ? NO : YES;

  ((UILabel *)nativeAdView.storeView).text = nativeAd.store;
  nativeAdView.storeView.hidden = nativeAd.store ? NO : YES;

  ((UILabel *)nativeAdView.priceView).text = nativeAd.price;
  nativeAdView.priceView.hidden = nativeAd.price ? NO : YES;

  ((UILabel *)nativeAdView.advertiserView).text = nativeAd.advertiser;
  nativeAdView.advertiserView.hidden = nativeAd.advertiser ? NO : YES;

  // In order for the SDK to process touch events properly, user interaction
  // should be disabled.
  nativeAdView.callToActionView.userInteractionEnabled = NO;
}

GADMediaView

图片和视频素材资源通过 GADMediaView 向用户展示。这是可在 xib 文件中定义或动态构建的 UIView。像所有其他素材资源视图一样,应该将其放在 GADNativeAdView 的视图层次结构中。

但与其他素材资源视图不同的是,应用不需要用其素材资源手动填充 GADMediaView。设置 nativeAd 属性后,SDK 会自动处理此填充事宜:

  • 如果有视频素材资源可用,则系统会对其进行缓冲处理,并开始在 GADMediaView 内播放。
  • 如果广告不包含视频素材资源,则会改为下载第一个图片素材资源,并将其放置在 GADMediaView 内。

    以下是原生高级广告的代码段示例:(Swift | Objective-C) 该示例展示了如何通过原生广告与原生广告视图的关联,以原生广告素材资源填充 GADMediaView

Swift

nativeAdView.nativeAd = nativeAd

Objective-C

nativeAdView.nativeAd = nativeAd;

确保在原生广告视图的 Interface Builder 文件中,您已将视图自定义类设置为 GADMediaView,并已将其连接到 mediaView 输出口。

原生视频广告

除了图片、文字和数字外,有些原生广告会包含视频素材资源。并不是每个广告都包含视频素材资源;如果广告中包含此类素材资源,也不需要相关应用播放这些视频。

GADVideoController

GADVideoController 类用于获取有关视频素材资源的信息。

GADUnifiedNativeAd 提供 videoController 属性,该属性可为每个广告显示 GADVideoController

Swift

let videoController = myUnifiedNativeAd.videoController

Objective-C

GADVideoController *videoController = myUnifiedNativeAd.videoController

即使广告不包含视频素材资源,此属性也一定不会是 nil

GADVideoController 提供以下查询视频状态的方法:

  • hasVideoContent - 如果广告中有视频素材资源,该方法为 true,否则为 false。
  • aspectRatio - 视频的宽高比(宽度除以高度)或 0(零)(如果没有视频素材资源)。

此外,应用还可以为 GADVideoController 设置 GADVideoControllerDelegate,以便在视频素材资源生命周期内发生事件时收到通知。GADVideoControllerDelegate 提供一条可选消息 videoControllerDidEndVideoPlayback,该消息会在视频播放结束时发送出去。

以下是 GADVideoControllerDelegate 的实际应用示例:

Swift

class ViewController: UIViewController, GADUnifiedNativeAdLoaderDelegate,
    GADVideoControllerDelegate {

  func adLoader(_ adLoader: GADAdLoader, didReceive nativeAd:
                GADUnifiedNativeAd) {
    ...
    nativeAd.videoController.delegate = self
    ...
  }
  ...
  func videoControllerDidEndVideoPlayback(_ videoController: Any!) {
    // Here apps can take action knowing video playback is finished.
    // This is handy for things like unmuting audio, and so on.
  }
}

Objective-C

@interface ViewController () <GADUnifiedNativeAdLoaderDelegate,
        GADVideoControllerDelegate>

- (void)adLoader:(GADAdLoader *)adLoader
    didReceiveNativeAd:(GADUnifiedNativeAd) *)nativeAd {
  ...
  nativeAd.videoController.delegate = self;
  ...
}
...

- (void)videoControllerDidEndVideoPlayback:(GADVideoController *)videoController {
  // Here apps can take action knowing video playback is finished.
  // This is handy for things like unmuting audio, and so on.
}

@end

*   Read the [Native ads policies and
guidelines](//support.google.com/admob/answer/6329638) for more guidance on how
to render your native ads.

发送以下问题的反馈:

此网页
需要帮助?请访问我们的支持页面