顯示系統定義的原生廣告格式
載入原生廣告時,應用程式會透過其中一個 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
,顯示原生廣告素材資源。
您可以在 Interface Builder 執行這項操作,與建立其他 xib 檔案時相同。原生廣告的版面配置可能如下:
請注意圖片右上方的「Custom Class」值,已設為
GADNativeAdView
。這是用於顯示 GADNativeAd
的廣告檢視區塊類別。
您也需要為 GADMediaView
設定自訂類別,用於顯示廣告的影片或圖片。
將通路連結至檢視區塊
設定完檢視區塊,並為版面配置指派正確的廣告檢視區塊類別後,請將廣告檢視區塊的素材資源通路連結至您建立的 UIViews
。以下說明如何將廣告檢視區塊的素材資源通路,連結至為廣告建立的 UIViews
:
在通路面板中,GADNativeAdView
的通路已連結至 Interface Builder 中配置的 UIViews
。這可讓 SDK 瞭解哪個 UIView
會顯示哪個素材資源。另請務必注意,這些通路代表廣告中可點擊的檢視區塊。
顯示廣告
完成版面配置並連結通路後,請在應用程式中加入下列程式碼,在廣告載入完畢後顯示:
Swift
// Mark: - NativeAdLoaderDelegate
func adLoader(_ adLoader: AdLoader, didReceive nativeAd: NativeAd) {
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? NativeAdView 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 MediaView 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
// 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
}
SwiftUI
建立檢視區塊模型
建立檢視區塊模型,用於載入原生廣告,並發布原生廣告資料變更:
import GoogleMobileAds
class NativeAdViewModel: NSObject, ObservableObject, NativeAdLoaderDelegate {
@Published var nativeAd: NativeAd?
private var adLoader: AdLoader!
func refreshAd() {
adLoader = AdLoader(
adUnitID: "ca-app-pub-3940256099942544/3986624511",
// The UIViewController parameter is optional.
rootViewController: nil,
adTypes: [.native], options: nil)
adLoader.delegate = self
adLoader.load(Request())
}
func adLoader(_ adLoader: AdLoader, didReceive nativeAd: NativeAd) {
// Native ad data changes are published to its subscribers.
self.nativeAd = nativeAd
nativeAd.delegate = self
}
func adLoader(_ adLoader: AdLoader, didFailToReceiveAdWithError error: Error) {
print("\(adLoader) failed with error: \(error.localizedDescription)")
}
}
建立 UIViewRepresentable
為 NativeView
建立 UIViewRepresentable
,並訂閱 ViewModel
類別中的資料變更:
private struct NativeAdViewContainer: UIViewRepresentable {
typealias UIViewType = NativeAdView
// Observer to update the UIView when the native ad value changes.
@ObservedObject var nativeViewModel: NativeAdViewModel
func makeUIView(context: Context) -> NativeAdView {
return
Bundle.main.loadNibNamed(
"NativeAdView",
owner: nil,
options: nil)?.first as! NativeAdView
}
func updateUIView(_ nativeAdView: NativeAdView, context: Context) {
guard let nativeAd = nativeViewModel.nativeAd else { return }
// Each UI property is configurable using your native ad.
(nativeAdView.headlineView as? UILabel)?.text = nativeAd.headline
nativeAdView.mediaView?.mediaContent = nativeAd.mediaContent
(nativeAdView.bodyView as? UILabel)?.text = nativeAd.body
(nativeAdView.iconView as? UIImageView)?.image = nativeAd.icon?.image
(nativeAdView.starRatingView as? UIImageView)?.image = imageOfStars(from: nativeAd.starRating)
(nativeAdView.storeView as? UILabel)?.text = nativeAd.store
(nativeAdView.priceView as? UILabel)?.text = nativeAd.price
(nativeAdView.advertiserView as? UILabel)?.text = nativeAd.advertiser
(nativeAdView.callToActionView as? UIButton)?.setTitle(nativeAd.callToAction, for: .normal)
// 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
}
將檢視區塊新增至檢視區塊階層
下列程式碼示範如何將 UIViewRepresentable
新增至檢視區塊階層:
struct NativeContentView: View {
// Single source of truth for the native ad data.
@StateObject private var nativeViewModel = NativeAdViewModel()
var body: some View {
ScrollView {
VStack(spacing: 20) {
// Updates when the native ad data changes.
NativeAdViewContainer(nativeViewModel: nativeViewModel)
.frame(minHeight: 300) // minHeight determined from xib.
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 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 上的完整範例
請點按對應的 GitHub 連結,查看分別以 Swift、SwiftUI 和 Objective-C 整合原生廣告的完整範例。
Swift 原生進階範例 SwiftUI 原生廣告範例 Objective-C 原生進階範例GADMediaView
系統會透過 GADMediaView
向使用者顯示圖片和影片素材資源。這是 UIView
,可以在 xib 檔案中定義或動態建構,而且與任何其他素材資源檢視區塊一樣,應加入 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
中。
後續步驟
進一步瞭解使用者隱私。