分享您的意見,協助我們打造 Google Mobile Ads SDK 的藍圖。請在 2023 年 5 月 5 日以前完成 Google Mobile Ads SDK 2023 年年度問卷調查

原生進階

透過集合功能整理內容 你可以依據偏好儲存及分類內容。

顯示系統定義的原生廣告格式

載入原生廣告時,應用程式會透過其中一個 GADAdLoaderDelegate 通訊協定訊息接收原生廣告物件。接著,您的應用程式會負責顯示廣告 (但不一定會立即)。為了方便顯示系統定義的廣告格式,SDK 提供了一些實用資源。

GADNativeAdView

GADNativeAd 有對應的「廣告瀏覽」類別:GADNativeAdView。這個廣告資料檢視類別是 UIView,發布商應用來顯示廣告。 例如,單一 GADNativeAdView 可以顯示 GADNativeAd 的單一執行個體。用來顯示該廣告的每一個 UIView 物件都應成為該 GADNativeAdView 物件的子檢視畫面。

舉例來說,如果您在 UITableView 內顯示廣告,其中一個儲存格的檢視區塊階層可能如下所示:

GADNativeAdView 類別也提供 IBOutlets,用來註冊每個資產所使用的檢視畫面,以及登錄 GADNativeAd 物件的方法。以這種方式登錄檢視畫面,可讓 SDK 自動處理以下工作:

  • 正在記錄點擊。
  • 記錄曝光次數 (螢幕上顯示第一個像素時)。
  • 顯示 AdChoices 廣告標籤。

AdChoices 廣告標籤

如果是間接原生廣告 (透過 AdMob候補廣告、Ad Exchange 或 AdSense 放送),SDK 會新增 AdChoices 重疊廣告。 請將自訂廣告角的原生廣告空間,保留在自動插入的 AdChoices 標誌中。此外,請務必將 AdChoices 廣告標籤放在允許圖示可見的內容上。若要進一步瞭解重疊外觀和功能,請參閱程式輔助原生廣告導入指南

廣告標示

顯示程式輔助原生廣告時,您必須顯示廣告標示,表示該檢視畫面是廣告。

程式碼範例

讓我們來看看如何使用從 xib 檔案動態載入的檢視來顯示原生廣告。如果使用 GADAdLoaders 設定為要求多種格式,這可能很實用。

調整 UIView

第一步是配置用來顯示原生廣告素材資源的UIViews。 就像在建立任何其他 xib 檔案一樣,您可以在 Interface Builder 中完成這項操作。原生廣告的版面配置看起來可能會像這樣:

請留意圖片右上方的自訂類別值。已設為

GADNativeAdView。 這是用來顯示 GADNativeAd 的廣告檢視畫面類別。

此外,您也必須設定 GADMediaView 的自訂類別,用來顯示廣告的影片或圖片。

將插座連結至檢視畫面

檢視畫面設定完成,且已將正確的廣告檢視類別指派至版面配置後,請將廣告檢視區塊的資產資產連結至您建立的 UIViews。以下說明如何將廣告資料檢視的素材資源插座連結至為廣告建立的 UIViews

在插座面板中,GADNativeAdView 中的插座已連結到介面建構工具中列出的 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 撰寫的原生進階廣告 的完整導入項目。

下載原生進階廣告範例

GAD 媒體檢視

系統會透過 GADMediaView 向使用者顯示圖片和影片素材資源。這是可在 xib 檔案中定義或動態建構的 UIView。應該放在 GADNativeAdView 的檢視區塊階層中,就像任何其他資產檢視一樣。

和所有素材資源檢視畫面一樣,媒體檢視畫面也必須填入其內容。請使用 GADMediaView 上的 mediaContent 屬性進行設定。GADNativeAdmediaContent 屬性包含可傳送至 GADMediaView 的媒體內容。

以下提供 Native Advanced 範例 (Swift | Objective-C) 說明如何用 GADNativeAdGADMediaContent 填入原生廣告素材資源:

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 中。