原生高级广告

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

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

GADNativeAdView

对于 GADNativeAd,则存在相应的“广告视图”类:GADNativeAdView。此广告视图类是一个 UIView,发布商应使用它来展示广告。 例如,单个 GADNativeAdView 可以显示 GADNativeAd 的单个实例。用于展示该广告素材资源的每个 UIView 对象都应为该 GADNativeAdView 对象的子视图。

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

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

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

“广告选择”叠加层

对于间接原生广告(通过 AdMob回填或通过 Ad Exchange 或 AdSense 投放的广告),SDK 会添加“广告选择”叠加层。 请在原生广告视图的首选角中留出空间,以便自动插入广告选择徽标。此外,请确保将广告选择叠加层放置在容易被用户看到的内容上。 如需详细了解叠加层的外观和功能,请参阅程序化原生广告植入指南

广告标示

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

代码示例

让我们来看看如何使用从 xib 文件动态加载的视图展示原生广告。在使用配置为请求多种格式的 GADAdLoaders 时,这可能是一个非常实用的方法。

布局 UIView

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

请注意图片右上角的“自定义类”值。已设为

GADNativeAdView。这是用于显示 GADNativeAd 的广告视图类。

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

将插座与视图相关联

视图就位并将正确的广告视图类分配到布局后,请将广告视图的素材资源输出口与您创建的 UIViews 相关联。以下是将广告视图的素材资源输出口与为广告创建的 UIViews 相关联的方法:

在插座面板中,GADNativeAdView 中的插座已链接到 Interface Builder 中列出的 UIViews。以便 SDK 知道哪个 UIView 会显示哪个素材资源。另请务必注意,这些输出口表示广告中可点击的视图。

展示广告

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

Swift

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

  // Set ourselves as the native ad delegate to be notified of native ad events.
  nativeAd.delegate = self

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

  // This app uses a fixed width for the GADMediaView and changes its height to match the aspect
  // ratio of the media it displays.
  if let mediaView = nativeAdView.mediaView, nativeAd.mediaContent.aspectRatio > 0 {
    let heightConstraint = NSLayoutConstraint(
      item: mediaView,
      attribute: .height,
      relatedBy: .equal,
      toItem: mediaView,
      attribute: .width,
      multiplier: CGFloat(1 / nativeAd.mediaContent.aspectRatio),
      constant: 0)
    heightConstraint.isActive = true
  }

  // 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

  // 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
}

Objective-C

#pragma mark GADNativeAdLoaderDelegate implementation

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

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

  // Set the mediaContent on the GADMediaView to populate it with available
  // video/image asset.
  nativeAdView.mediaView.mediaContent = nativeAd.mediaContent;

  // 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;

  // Associate the native ad view with the native ad object. This is
  // required to make the ad clickable.
  nativeAdView.nativeAd = nativeAd;
}

我们的 GitHub 代码库包含以 Swift 和 Objective-C 编写的原生高级广告 的完整实现方案。

下载原生高级广告示例

GADMediaView

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

与所有素材资源视图一样,媒体视图也需要填充其内容。这是使用 GADMediaView 中的 mediaContent 属性设置的。GADNativeAdmediaContent 属性包含可传递到 GADMediaView 的媒体内容。

下面是原生高级广告示例 (Swift | Objective-C) 中的代码段,展示了如何使用 GADNativeAd 中的 GADMediaContent 填充 GADMediaView 原生广告素材资源:

Swift

nativeAdView.mediaView?.mediaContent = nativeAd.mediaContent

Objective-C

nativeAdView.mediaView.mediaContent = nativeAd.mediaContent;

确保在原生广告视图的接口构建器文件中,将视图自定义类设置为 GADMediaView,并且将其连接到 mediaView 插座。

更改图片内容模式

GADMediaView 类在显示图片时遵循 UIView contentMode 属性。若想在 GADMediaView 中更改图片的缩放方式,请在 GADMediaViewcontentMode 属性上设置相应的 UIViewContentMode

例如,要在图片显示时填充 GADMediaView(广告中不包含视频),请使用以下代码:

Swift

nativeAdView.mediaView?.contentMode = .aspectFill

Objective-C

nativeAdView.mediaView.contentMode = UIViewContentModeAspectFill;

GADMediaContent 内容

GADMediaContent 类包含与原生广告的媒体内容相关的数据,并使用 GADMediaView 类进行显示。在 GADMediaView mediaContent 属性上设置时:

  • 如果有视频素材资源可用,系统会对其进行缓冲,并开始在 GADMediaView 中播放。您可以通过检查 hasVideoContent 来判断是否有视频素材资源可用。

  • 如果广告不包含视频素材资源,则系统会改为下载 mainImage 素材资源,并将其放置在 GADMediaView 中。

后续步骤

详细了解用户隐私