顯示系統定義的原生廣告格式
原生廣告載入時,應用程式會透過其中一個 GADAdLoaderDelegate
通訊協定訊息接收原生廣告物件。接著,您的應用程式會負責顯示廣告 (但不一定要立即執行這項操作)。為方便您顯示系統定義的廣告格式,SDK 提供一些實用資源。
GADNativeAdView
GADNativeAd
有一個對應的「廣告瀏覽」類別:GADNativeAdView
。廣告檢視類別是發布商用於顯示廣告的 UIView
。舉例來說,單一 GADNativeAdView
可顯示 GADNativeAd
的單一執行個體。用來顯示該廣告素材資源的每個 UIView
物件都必須是該 GADNativeAdView
物件的子檢視畫面。
舉例來說,如果您在 UITableView
中顯示廣告,其中一個儲存格的檢視區塊階層可能如下所示:
GADNativeAdView
類別也提供 IBOutlets
,用於註冊每個資產使用的檢視畫面,以及註冊 GADNativeAd
物件本身的方法。以這種方式註冊檢視畫面,可讓 SDK 自動處理以下工作:
- 正在記錄點擊。
- 記錄曝光 (系統可在螢幕上顯示第一個像素時)。
- 顯示 AdChoices 重疊廣告。
AdChoices 重疊廣告
對於間接原生廣告 (透過 Ad Manager候補廣告或 Ad Exchange 或 AdSense 放送),SDK 會新增 AdChoices 重疊廣告。請在原生廣告檢視的偏好的角落留下空間,以供系統自動插入的 AdChoices 標誌。此外,請務必將 AdChoices 重疊元素放在讓人容易看到圖示的內容上。如要進一步瞭解重疊廣告的外觀和功能,請參閱程式輔助原生廣告導入指南。
程式輔助原生廣告的廣告歸因
顯示程式輔助原生廣告時,您必須顯示廣告歸屬,以表明資料檢視是廣告。 請參閱這個頁面查看政策規範。
程式碼範例
現在來看看如何使用從 xib 檔案動態載入的檢視畫面來顯示原生廣告。使用設為 GADAdLoaders
要求多種格式時,這非常實用。
配置 UIView
第一步是配置用來顯示原生廣告素材資源的 UIViews
。
您可以在介面建構工具中執行這項操作,方法與建立任何其他 xib 檔案時相同。原生廣告的版面配置可能如下所示:
請注意圖片右上方的「Custom Class」值。已設為
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 編寫的原生自訂顯示廣告 的完整實作。
GADMediaView
圖片和影片素材資源會透過 GADMediaView
向使用者顯示。這是可以在 xib 檔案中定義或動態建構的 UIView
。和任何其他素材資源檢視畫面一樣,這類檔案應置於 GADNativeAdView
的檢視區塊階層中。
和所有素材資源檢視畫面一樣,媒體檢視畫面也必須填入內容。這是使用 GADMediaView
上的 mediaContent
屬性進行設定。GADNativeAd
的 mediaContent
屬性包含可傳遞至 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
中的縮放方式,請在 GADMediaView
的 contentMode
屬性上設定對應的 UIViewContentMode
以完成此操作。
例如,如要在顯示圖片時填滿 GADMediaView
(廣告沒有影片):
Swift
nativeAdView.mediaView?.contentMode = .aspectFill
Objective-C
nativeAdView.mediaView.contentMode = UIViewContentModeAspectFill;
GADMediaContent
GADMediaContent
類別會保留與原生廣告媒體內容相關的資料,這些內容透過 GADMediaView
類別顯示。在 GADMediaView
mediaContent
屬性上設定時:
如果有可用的影片資產,系統會緩衝並在
GADMediaView
中開始播放。您可以檢查hasVideoContent
,判斷是否有影片素材資源。如果廣告不包含影片素材資源,系統會改為下載
mainImage
素材資源,並放在GADMediaView
中。
後續步驟
進一步瞭解使用者隱私。