插頁式廣告會全螢幕顯示,覆蓋整個應用程式的介面,直到使用者關閉為止。這類廣告顯示的時間點通常都是在應用程式流程中的自然轉換點,例如操作後的空檔或遊戲關卡之間的暫停時間。應用程式顯示插頁式廣告時,使用者可選擇輕觸廣告前往到達網頁,或是關閉廣告返回應用程式。 研究案例。
本指南說明如何在 iOS 應用程式中整合中繼插頁廣告。
必要條件
- 完成入門指南。
一律使用測試廣告進行測試
建構及測試應用程式時,請務必使用測試廣告,而非實際的正式廣告。否則帳戶可能會遭到停權。
如要輕鬆測試廣告載入情形,請使用專用的測試廣告單元 ID
,適用於 iOS 插頁式廣告:
ca-app-pub-3940256099942544/4411468910
這個 ID 經過特別設定,可針對每項要求傳回測試廣告,您可以在編寫、測試及偵錯時,自由地在自己的應用程式中使用。發布應用程式前,請務必將其替換為您自己的廣告單元 ID。
如要進一步瞭解 Mobile Ads SDK 測試廣告的運作方式,請參閱「測試廣告」。
導入作業
整合插頁式廣告的主要步驟如下:
- 載入廣告。
- 註冊回呼。
- 顯示廣告。
載入廣告
載入廣告時,請使用 GADInterstitialAd
類別的 load(adUnitID:request)
方法。
Swift
fileprivate func loadInterstitial() async {
do {
interstitial = try await InterstitialAd.load(
with: "ca-app-pub-3940256099942544/4411468910", request: Request())
interstitial?.fullScreenContentDelegate = self
} catch {
print("Failed to load interstitial ad with error: \(error.localizedDescription)")
}
}
SwiftUI
import GoogleMobileAds
class InterstitialViewModel: NSObject, FullScreenContentDelegate {
private var interstitialAd: InterstitialAd?
func loadAd() async {
do {
interstitialAd = try await InterstitialAd.load(
with: "ca-app-pub-3940256099942544/4411468910", request: Request())
interstitialAd?.fullScreenContentDelegate = self
} catch {
print("Failed to load interstitial ad with error: \(error.localizedDescription)")
}
}
Objective-C
[GADInterstitialAd
loadWithAdUnitID:@"ca-app-pub-3940256099942544/4411468910"
request:[GADRequest request]
completionHandler:^(GADInterstitialAd *ad, NSError *error) {
if (error) {
NSLog(@"Failed to load interstitial ad with error: %@", [error localizedDescription]);
return;
}
self.interstitial = ad;
self.interstitial.fullScreenContentDelegate = self;
}];
註冊回呼
如要接收簡報事件的通知,您必須指派傳回廣告的 GADFullScreenContentDelegate to the
fullScreenContentDelegate` 屬性:
Swift
interstitial?.fullScreenContentDelegate = self
SwiftUI
interstitialAd?.fullScreenContentDelegate = self
Objective-C
self.interstitial.fullScreenContentDelegate = self;
GADFullScreenContentDelegate
通訊協定會處理廣告成功或無法顯示,以及廣告遭到關閉時的回呼。以下程式碼說明如何實作通訊協定:
Swift
func adDidRecordImpression(_ ad: FullScreenPresentingAd) {
print("\(#function) called")
}
func adDidRecordClick(_ ad: FullScreenPresentingAd) {
print("\(#function) called")
}
func ad(_ ad: FullScreenPresentingAd, didFailToPresentFullScreenContentWithError error: Error) {
print("\(#function) called with error: \(error.localizedDescription)")
// Clear the interstitial ad.
interstitial = nil
}
func adWillPresentFullScreenContent(_ ad: FullScreenPresentingAd) {
print("\(#function) called")
}
func adWillDismissFullScreenContent(_ ad: FullScreenPresentingAd) {
print("\(#function) called")
}
func adDidDismissFullScreenContent(_ ad: FullScreenPresentingAd) {
print("\(#function) called")
// Clear the interstitial ad.
interstitial = nil
}
SwiftUI
func adDidRecordImpression(_ ad: FullScreenPresentingAd) {
print("\(#function) called")
}
func adDidRecordClick(_ ad: FullScreenPresentingAd) {
print("\(#function) called")
}
func ad(
_ ad: FullScreenPresentingAd,
didFailToPresentFullScreenContentWithError error: Error
) {
print("\(#function) called")
}
func adWillPresentFullScreenContent(_ ad: FullScreenPresentingAd) {
print("\(#function) called")
}
func adWillDismissFullScreenContent(_ ad: FullScreenPresentingAd) {
print("\(#function) called")
}
func adDidDismissFullScreenContent(_ ad: FullScreenPresentingAd) {
print("\(#function) called")
// Clear the interstitial ad.
interstitialAd = nil
}
Objective-C
- (void)adDidRecordImpression:(id<GADFullScreenPresentingAd>)ad {
NSLog(@"%s called", __PRETTY_FUNCTION__);
}
- (void)adDidRecordClick:(id<GADFullScreenPresentingAd>)ad {
NSLog(@"%s called", __PRETTY_FUNCTION__);
}
- (void)ad:(id<GADFullScreenPresentingAd>)ad
didFailToPresentFullScreenContentWithError:(NSError *)error {
NSLog(@"%s called with error: %@", __PRETTY_FUNCTION__, error.localizedDescription);
// Clear the interstitial ad.
self.interstitial = nil;
}
- (void)adWillPresentFullScreenContent:(id<GADFullScreenPresentingAd>)ad {
NSLog(@"%s called", __PRETTY_FUNCTION__);
}
- (void)adWillDismissFullScreenContent:(id<GADFullScreenPresentingAd>)ad {
NSLog(@"%s called", __PRETTY_FUNCTION__);
}
- (void)adDidDismissFullScreenContent:(id<GADFullScreenPresentingAd>)ad {
NSLog(@"%s called", __PRETTY_FUNCTION__);
// Clear the interstitial ad.
self.interstitial = nil;
}
GADInterstitialAd
是只能使用一次的物件。也就是說,插頁式廣告顯示後就無法再次顯示。最佳做法是在 GADFullScreenContentDelegate
的 adDidDismissFullScreenContent:
方法中載入另一個插頁式廣告,這樣前一個插頁式廣告關閉後,系統就會立即開始載入下一個插頁式廣告。
顯示廣告
插頁式廣告應在應用程式流程中的自然暫停時間顯示,例如遊戲關卡之間,或使用者完成工作後。
Swift
ad.present(from: self!)
SwiftUI
監聽檢視區塊中的 UI 事件,判斷何時顯示廣告。
var body: some View {
// ...
}
.onChange(of: countdownTimer.isComplete) { newValue in
showGameOverAlert = newValue
}
.alert(isPresented: $showGameOverAlert) {
Alert(
title: Text("Game Over"),
message: Text("You lasted \(countdownTimer.countdownTime) seconds"),
dismissButton: .cancel(
Text("OK"),
action: {
viewModel.showAd()
}))
從檢視模型顯示插頁式廣告:
func showAd() {
guard let interstitialAd = interstitialAd else {
return print("Ad wasn't ready.")
}
interstitialAd.present(from: nil)
}
Objective-C
[self.interstitial presentFromRootViewController:self];
最佳做法
- 請考慮插頁式廣告是否適合您的應用程式。
- 插頁式廣告最適合在有自然轉換點的應用程式中放送。 使用者在應用程式中完成操作時 (例如分享圖片或遊戲破關),就會形成這類空檔。由於使用者預期動作會中斷,因此很容易在不打斷使用者體驗的情況下,顯示中介層廣告。請務必考量要在應用程式工作流程的哪些時間點顯示中繼廣告,以及使用者可能採取的行動。
- 顯示插頁式廣告時,請記得暫停動作。
- 我們提供數種插頁式廣告格式:文字、圖像、影片等。請務必確保應用程式在顯示中介層廣告時,也會暫停使用部分資源,讓廣告能充分利用這些資源。舉例來說,當您呼叫顯示中繼廣告時,請務必暫停應用程式產生的任何音訊輸出。您可以在
adDidDismissFullScreenContent:
事件處理常式中繼續播放音效,使用者與廣告互動完畢後,系統就會叫用該事件處理常式。此外,建議在顯示廣告時暫停任何需要大量運算的作業 (例如遊戲迴圈)。確保使用者不會遇到圖像緩慢或無回應,或是影片斷斷續續的問題。 - 預留充足的載入時間。
- 除了確保在適當時間顯示插頁式廣告外,也請務必確保使用者不必等待廣告載入。在您打算顯示廣告前預先載入,可確保應用程式在需要顯示插頁式廣告時,已備妥完全載入的廣告。
- 不要讓應用程式廣告氾濫。
- 雖然增加應用程式中的中插廣告頻率,似乎是提高收益的好方法,但這也可能破壞使用者體驗,並降低點擊率。確保使用者不會因為太常受到干擾,而無法享受使用應用程式的樂趣。
- 請勿使用載入完成回呼來顯示中繼插頁式廣告。
- 這可能會導致使用者體驗不佳。請改為在需要顯示廣告前預先載入。然後檢查
canPresentFromRootViewController:error:
方法中的GADInterstitialAd
,確認是否已準備好顯示。
其他資源
GitHub 上的範例
查看您偏好語言的完整插頁式廣告範例:
Mobile Ads Garage 影片教學課程
成功案例
後續步驟
- 如果尚未建立插頁式廣告單元,請在 AdMob 使用者介面中建立。
- 進一步瞭解廣告指定目標和中繼廣告規範。
- 進一步瞭解使用者隱私權。