廣告預先載入是 Google Mobile Ads SDK 中由 Google 管理的廣告載入功能,可代您管理廣告載入和快取。如要預先載入廣告,您必須變更廣告載入的管理方式。如要使用廣告預先載入功能提升成效,請停用自訂快取,並將這項責任委派給 Google Mobile Ads SDK。
相較於手動載入廣告,預先載入廣告具有下列優點:
- 參照管理:保存已載入的廣告,因此您不必維護參照,直到準備好顯示廣告為止。
- 自動重新載入:從快取中取出廣告時,系統會自動載入新廣告。
- 管理重試:使用指數輪詢自動重試失敗的要求。
- 到期處理:在廣告到期前 (通常是一小時後) 自動重新整理廣告。
- 快取最佳化:如果使用的快取大小超過一,Google Mobile Ads SDK 會最佳化快取順序,以放送最佳廣告。
本指南說明如何設定預先載入廣告、檢查預先載入廣告是否可用,以及顯示預先載入廣告。
必要條件
繼續進行本教學課程前,請先完成下列項目:
- 安裝 Google Mobile Ads SDK 12.6.0 以上版本。 如需舊版的開發人員資源,請參閱 11.12.0 - 12.2.0,但我們建議使用 12.6.0 以上版本,並按照本指南操作。
- 設定 Google Mobile Ads SDK。
下載並在專案中加入預先載入預覽標頭。
選用:下載並執行廣告預先載入範例應用程式。
開始預先載入廣告
如要開始預先載入廣告,請呼叫 preload()。請只在應用程式啟動時呼叫此方法一次。呼叫 preload() 後,Google Mobile Ads SDK 會自動預先載入廣告,並重試預先載入設定的失敗要求。
以下範例會開始預先載入廣告:
Swift
// Start the preloading initialization process.
let request = Request()
let interstitialConfig = PreloadConfigurationV2(
adUnitID: Constants.interstitialAdUnitID, request: request)
InterstitialAdPreloader.shared.preload(
for: Constants.interstitialAdUnitID, configuration: interstitialConfig, delegate: self)
Objective-C
// Start the preloading initialization process.
GADRequest *request = [GADRequest request];
GADPreloadConfigurationV2 *interstitialConfig = [[GADPreloadConfigurationV2 alloc] initWithAdUnitID:interstitialAdUnitID request:request];
[GADInterstitialAdPreloader.sharedInstance preloadForPreloadID:interstitialAdUnitID
configuration:interstitialConfig
delegate:self];
取得並顯示預先載入的廣告
使用廣告預先載入功能時,Google Mobile Ads SDK 會保留快取廣告。
如要顯示廣告,請呼叫 adWithPreloadID()。Google Mobile Ads SDK 會擷取可用的廣告,並在背景自動預先載入下一則廣告。
請在準備好顯示廣告後再呼叫這個方法。將廣告保留在快取中,Google Mobile Ads SDK可自動重新整理過期的廣告,並執行快取最佳化。
以下範例會擷取並顯示預先載入的廣告:
Swift
private func showInterstitialAd() {
// Verify that the preloaded ad is available before polling.
guard isInterstitialAvailable() else {
printAndShowAlert("Preload interstitial ad is exhausted.")
return
}
// Polling returns the next available ad and load another ad in the background.
let ad = InterstitialAdPreloader.shared.ad(with: Constants.interstitialAdUnitID)
// Interact with the ad object as needed.
print("Interstitial ad response info: \(String(describing: ad?.responseInfo))")
ad?.paidEventHandler = { (value: AdValue) in
print("Interstitial ad paid event: \(value.value), \(value.currencyCode)")
}
ad?.fullScreenContentDelegate = self
ad?.present(from: self)
}
Objective-C
- (void)showInterstitialAd {
// Verify that the preloaded ad is available before polling.
if (![self isInterstitialAvailable]) {
[self logAndShowAlert:@"Preloaded interstitial ad is not available."];
return;
}
// Getting the preloaded ad loads another ad in the background.
GADInterstitialAd *ad = [GADInterstitialAdPreloader.sharedInstance adWithPreloadID:interstitialAdUnitID];
// Interact with the ad object as needed.
NSLog(@"Interstitial ad response info: %@", ad.responseInfo);
ad.paidEventHandler = ^(GADAdValue *_Nonnull value) {
NSLog(@"Interstitial ad paid event: %@ %@ ", value.value, value.currencyCode);
};
ad.fullScreenContentDelegate = self;
[ad presentFromRootViewController:self];
}
檢查預先載入廣告是否可用
如要查看廣告放送資格,請選擇下列其中一個選項:
取得預先載入廣告是否可用
以下範例會檢查廣告是否可用:
Swift
private func isInterstitialAvailable() -> Bool {
// Verify that an ad is available before polling.
return InterstitialAdPreloader.shared.isAdAvailable(with: Constants.interstitialAdUnitID)
}
Objective-C
- (BOOL)isInterstitialAvailable {
// Verify that an ad is available before polling.
return [GADInterstitialAdPreloader.sharedInstance isAdAvailableWithPreloadID:(interstitialAdUnitID)];
}
監聽預先載入廣告是否可用
註冊預先載入事件,即可在廣告預先載入成功、預先載入失敗或廣告快取空間用盡時收到通知。
預先載入事件的目的是為了進行分析。在預先載入事件回呼中:
- 不要呼叫
preload()。 - 除非廣告會立即顯示,否則請避免呼叫
adWithPreloadID()。
以下範例會註冊廣告事件:
Swift
func adAvailable(forPreloadID preloadID: String, responseInfo: ResponseInfo) {
// This callback indicates that an ad is available for the specified configuration.
// No action is required here, but updating the UI can be useful in some cases.
print("Ad preloaded successfully for ad preload ID: \(preloadID)")
// ...
}
func adsExhausted(forPreloadID preloadID: String) {
// This callback indicates that all the ads for the specified configuration have been
// consumed and no ads are available to show. No action is required here, but updating
// the UI can be useful in some cases.
// [Important] Don't call AdPreloader.shared.preload or AdPreloader.shared.ad
// from adsExhausted.
print("Ad exhausted for ad preload ID: \(preloadID)")
// ...
}
func adFailedToPreload(forPreloadID preloadID: String, error: Error) {
print(
"Ad failed to load with ad preload ID: \(preloadID), Error: \(error.localizedDescription)"
)
}
Objective-C
- (void)adAvailableForPreloadID:(nonnull NSString *)preloadID responseInfo:(nonnull GADResponseInfo *)responseInfo {
// This callback indicates that an ad is available for the specified configuration.
// No action is required here, but updating the UI can be useful in some cases.
NSLog(@"Ad preloaded successfully for ad unit ID: %@", preloadID);
// ...
}
- (void)adsExhaustedForPreloadID:(nonnull NSString *)preloadID {
// This callback indicates that all the ads for the specified configuration have been
// consumed and no ads are available to show. No action is required here, but updating
// the UI can be useful in some cases.
// [Important] Don't call [GAD<Format>AdPreloader preloadForPreloadID:] or
// [GAD<Format>AdPreloader adWithPreloadID:] from adsExhaustedForPreloadID.
NSLog(@"Ad exhausted for ad preload ID: %@", preloadID);
// ...
}
- (void)adFailedToPreloadForPreloadID:(nonnull NSString *)preloadID error:(nonnull NSError *)error {
NSLog(@"Ad failed to load with ad preload ID: %@, Error: %@", preloadID, error.localizedDescription);
}
停止預先載入廣告
如果不需要在工作階段中再次顯示預先載入 ID 的廣告,可以停止預先載入廣告。如要停止預先載入特定預先載入 ID 的廣告,請使用預先載入 ID 呼叫 stopPreloadingAndRemoveAdsForPreloadID()。
設定緩衝區大小
緩衝區大小會控管記憶體中預先載入的廣告數量。根據預設,Google 會調整緩衝區大小,以平衡記憶體用量和廣告放送延遲。如果應用程式會在載入下一個廣告前顯示廣告,您可以設定自訂緩衝區大小,增加記憶體中保留的廣告數量。建議緩衝區大小最多為四。
Swift
let preloadConfig = PreloadConfigurationV2(adUnitID: "/21775744923/example/rewarded")
preloadConfig.bufferSize = 3
Objective-C
GADPreloadConfigurationV2 *preloadConfig =
[[GADPreloadConfigurationV2 alloc] initWithAdUnitID:@"/21775744923/example/rewarded"];
preloadConfig.bufferSize = 3;