原生廣告是廣告素材資源,透過平台原生 UI 元件向使用者顯示。這類廣告在顯示時採用的類別,與您在分鏡腳本中使用的類別相同,並能調整成符合應用程式視覺設計的格式。
載入原生廣告時,應用程式會收到含有素材資源的廣告物件,並由應用程式 (而非 Google Mobile Ads SDK) 負責顯示廣告素材。
廣義來說,成功導入原生廣告須完成兩個步驟:首先使用 SDK 載入廣告,接著在應用程式中顯示廣告素材。
本頁面說明如何使用 SDK 載入原生廣告。
前置作業
- 完成入門指南的步驟。
請一律使用測試廣告進行測試
建構及測試應用程式時,請務必使用測試廣告,而非實際運作中的廣告。
載入測試廣告最簡單的方法,是使用 iOS 原生廣告專用的測試廣告單元 ID:
/21775744923/example/native
這類 ID 經特別設定,可針對每個請求傳回測試廣告。您可在編寫程式碼、測試及偵錯時,將其用於自己的應用程式。發布應用程式前,請務必將這類 ID 替換為自己的廣告單元 ID。
如要進一步瞭解 Google Mobile Ads SDK 測試廣告,請參閱「啟用測試廣告」。
載入廣告
原生廣告是以 GADAdLoader 類別載入,並根據 GADAdLoaderDelegate 協定傳送訊息至委派物件。
除了系統定義的原生格式外,您也可以自行建立自訂原生廣告格式,用來展示直接銷售的原生廣告。自訂原生廣告格式能將任意結構化資料傳遞至應用程式。這類廣告會以 GADCustomNativeAd 類別表示。
初始化廣告載入器
您必須先初始化廣告載入器,才能載入廣告。下列程式碼示範如何初始化 GADAdLoader:
Swift
將 nativeAdUnitID 替換為廣告單元 ID。
Objective-C
將 kNativeAdUnitID 替換為廣告單元 ID。
您必須提供廣告單元 ID (可使用測試 ID)、要傳遞至 adTypes 陣列的常數 (指定要請求哪些原生格式),以及任何要在 options 參數中設定的選項。請參閱「設定原生廣告選項」頁面,查看 options 參數的可能值清單。
adTypes 陣列應包含下列一或多個常數:
實作廣告載入器委派物件
廣告載入器委派物件必須導入廣告類型專屬的通訊協定。如果是原生廣告,GADNativeAdLoaderDelegate 通訊協定會包含原生廣告載入後傳送至委派物件的訊息。
Swift
func adLoader(_ adLoader: AdLoader, didReceive nativeAd: NativeAd) {
// To be notified of events related to the native ad interactions, set the delegate property
// of the native ad
nativeAd.delegate = self
// TODO: Display the native ad.
}
Objective-C
- (void)adLoader:(GADAdLoader *)adLoader didReceiveNativeAd:(GADNativeAd *)nativeAd {
// To be notified of events related to the native ad interactions, set the delegate property
// of the native ad
nativeAd.delegate = self;
// TODO: Display the native ad.
}
GADCustomNativeAdLoaderDelegate 協定會在自訂範本廣告載入後通知委派物件。
Swift
func adLoader(_ adLoader: AdLoader, didReceive customNativeAd: CustomNativeAd) {
// To be notified of events related to the custom native ad interactions, set the delegate
// property of the native ad
customNativeAd.delegate = self
// TODO: Display the custom native ad.
}
Objective-C
- (void)adLoader:(GADAdLoader *)adLoader
didReceiveCustomNativeAd:(GADCustomNativeAd *)customNativeAd {
// To be notified of events related to the custom native ad interactions, set the delegate
// property of the native ad
customNativeAd.delegate = self;
// TODO: Display the custom native ad.
}
請求廣告
初始化 GADAdLoader 後,請呼叫其 loadRequest: 方法來請求廣告:
Swift
adLoader.load(AdManagerRequest())
Objective-C
[self.adLoader loadRequest:[GAMRequest request]];
GADAdLoader 中的 loadRequest: 方法,接受與橫幅廣告和插頁式廣告相同的 GAMRequest 物件。如同其他廣告類型,您可以使用請求物件新增指定目標資訊。
載入多則廣告 (選用)
在初始化 GADAdLoader 時設定 GADMultipleAdsAdLoaderOptions 物件,即可在單一請求中載入多則廣告。
Swift
Objective-C
每個請求最多可傳回五則廣告,且 SDK 不一定會傳回所請求的確切數量。
系統傳回的 Google 廣告不會重複,但預訂廣告空間或第三方買家的廣告有可能重複。
若您採用中介服務,請勿使用 GADMultipleAdsAdLoaderOptions 類別,因為已設定中介服務的廣告單元 ID,無法請求多個原生廣告。
判斷載入作業何時完成
應用程式呼叫 loadRequest: 後,即可透過下列呼叫方法取得請求結果:
GADAdLoaderDelegate的adLoader:didFailToReceiveAdWithError:方法GADNativeAdLoaderDelegate的adLoader:didReceiveNativeAd:方法
如果請求單一廣告,系統會對上述其中一種方法發出一次呼叫。
如果要求多則廣告,系統至少會對上述方法執行一次回呼,但不會超過要求的廣告數量上限。
此外,GADAdLoaderDelegate 也提供 adLoaderDidFinishLoading 回呼。這個委派方法表示廣告載入器已完成載入廣告,且不會再回報該請求的其他廣告或錯誤。以下範例說明如何在一次載入多個原生廣告時,使用這個方法:
Swift
func adLoaderDidFinishLoading(_ adLoader: AdLoader) {
// The adLoader has finished loading ads.
}
Objective-C
- (void)adLoaderDidFinishLoading:(GADAdLoader *)adLoader {
// The adLoader has finished loading ads.
}
處理失敗的請求
通訊協定會擴充 GADAdLoaderDelegate 通訊協定,定義廣告無法載入時傳送的訊息。
Swift
func adLoader(_ adLoader: AdLoader, didFailToReceiveAdWithError error: any Error) {
// The adLoader failed to receive an ad.
}
Objective-C
- (void)adLoader:(GADAdLoader *)adLoader didFailToReceiveAdWithError:(NSError *)error {
// The adLoader failed to receive an ad.
}
接收原生廣告事件通知
如要接收與原生廣告互動相關的事件通知,請設定原生廣告的委派物件屬性:
Swift
// Set the delegate before making an ad request.
adLoader.delegate = self
Objective-C
// Set the delegate before making an ad request.
self.adLoader.delegate = self;
接著,導入 GADNativeAdDelegate 來接收下列委派物件呼叫:
Swift
func nativeAdDidRecordImpression(_ nativeAd: NativeAd) {
// The native ad was shown.
}
func nativeAdDidRecordClick(_ nativeAd: NativeAd) {
// The native ad was clicked on.
}
func nativeAdWillPresentScreen(_ nativeAd: NativeAd) {
// The native ad will present a full screen view.
}
func nativeAdWillDismissScreen(_ nativeAd: NativeAd) {
// The native ad will dismiss a full screen view.
}
func nativeAdDidDismissScreen(_ nativeAd: NativeAd) {
// The native ad did dismiss a full screen view.
}
func nativeAdWillLeaveApplication(_ nativeAd: NativeAd) {
// The native ad will cause the app to become inactive and
// open a new app.
}
Objective-C
- (void)nativeAdDidRecordImpression:(GADNativeAd *)nativeAd {
// The native ad was shown.
}
- (void)nativeAdDidRecordClick:(GADNativeAd *)nativeAd {
// The native ad was clicked on.
}
- (void)nativeAdWillPresentScreen:(GADNativeAd *)nativeAd {
// The native ad will present a full screen view.
}
- (void)nativeAdWillDismissScreen:(GADNativeAd *)nativeAd {
// The native ad will dismiss a full screen view.
}
- (void)nativeAdDidDismissScreen:(GADNativeAd *)nativeAd {
// The native ad did dismiss a full screen view.
}
- (void)nativeAdWillLeaveApplication:(GADNativeAd *)nativeAd {
// The native ad will cause the app to become inactive and
// open a new app.
}
最佳做法
載入廣告時,請遵守下列規則。
如果應用程式採用清單中的原生廣告,則應預先快取廣告清單。
如有預先快取廣告,請清除快取,過一小時後再重新載入廣告。
請勿在
GADAdLoader上再次呼叫loadRequest:,除非adLoaderDidFinishLoading:表示更早的要求已載入完畢。只限快取需要的原生廣告。例如預先快取時,只快取要在螢幕上立即顯示的廣告。原生廣告的記憶體用量很大,如果快取後不刪除,會占用過多記憶體。
不再使用原生廣告時,請將其刪除。
顯示廣告
載入廣告後,接下來就只剩下向使用者顯示廣告。相關操作說明請參閱原生進階指南。