插頁式廣告會全螢幕顯示,覆蓋整個應用程式的介面,直到使用者關閉廣告為止。這類廣告通常出現在應用程式流程中的自然轉換點,例如操作或遊戲關卡之間的空檔。應用程式顯示插頁式廣告時,使用者可輕觸廣告前往到達網頁,也可以關閉廣告返回應用程式。如需應用實例,請參閱這份個案研究。
本指南說明如何在 iOS 應用程式中整合插頁式廣告。
先決條件
- 完成入門指南的步驟。
請務必使用測試廣告進行測試
建構及測試應用程式時,請務必使用測試廣告,而非實際運作中的廣告,否則可能導致帳戶遭停權。
載入測試廣告最簡單的方法,是使用 iOS 插頁式廣告專用的測試廣告單元 ID:
ca-app-pub-3940256099942544/4411468910
這種 ID 經特別設定,會針對每個請求傳回測試廣告。您可在編寫程式碼、測試及偵錯時,在自家應用程式中使用測試用 ID,但別忘了在發布應用程式前,將這類 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
指派給傳回廣告的 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:
事件處理常式來繼續播放音效。此外,顯示廣告時,建議暫停密集的運算工作 (例如遊戲迴圈),以免使用者碰到畫面遲緩/停頓或影片播放不流暢的問題。 - 給予充分的載入時間。
- 除了在適當時機顯示插頁式廣告,也應該全力避免使用者苦等廣告載入。提前載入插頁式廣告,可確保應用程式有已經完整載入的廣告可用,必要時可立即顯示。
- 避免廣告疲勞轟炸。
- 提高插頁式廣告在應用程式中的展示頻率或許能提高收益,但也可能破壞使用者體驗及降低點閱率。請不要頻繁打斷使用者,確保愉快的應用程式操作體驗。
- 不可使用載入完成回呼顯示插頁式廣告。
- 這麼做可能造成使用體驗不佳。建議您預先載入廣告,然後檢查
GADInterstitialAd
的canPresentFromRootViewController:error:
方法,確認廣告已經就緒,隨時可以顯示。
其他資源
GitHub 上的範例
請選取您慣用的程式語言,查看完整的插頁式廣告範例: