插頁式廣告會全螢幕顯示,覆蓋整個應用程式的介面,直到使用者關閉為止。這類廣告通常出現在應用程式流程中的自然轉換點,比如畫面切換階段或遊戲關卡之間的空檔。應用程式顯示插頁式廣告時,使用者可以輕觸廣告前往連結目標網址,也可以關閉廣告,返回應用程式。個案研究
本指南說明如何在 iOS 應用程式中整合插頁式廣告。
前置作業
- 完整閱讀入門指南。
請一律使用測試廣告進行測試
建構及測試應用程式時,請務必使用測試廣告,而非實際的正式廣告,若未遵守此規定,可能導致帳戶遭到停權。
載入測試廣告最簡單的方法,是使用 iOS 插頁式廣告專用的測試廣告單元 ID:
/21775744923/example/interstitial
這類 ID 經特別設定,可針對每個請求傳回測試廣告。您可在編寫程式碼、測試及偵錯時,將其用於自己的應用程式。但發布前記得要換成自己的廣告單元 ID。
如要進一步瞭解 Mobile Ads SDK 測試廣告的運作方式,請參閱「測試廣告」。
導入作業
整合插頁式廣告的主要步驟如下:
- 載入廣告。
- 註冊接收回呼。
- 顯示廣告。
載入廣告
載入廣告時,請使用 GAMInterstitialAd 類別的 load(adUnitID:request) 方法。
Swift
fileprivate func loadInterstitial() async {
do {
interstitial = try await AdManagerInterstitialAd.load(
with: "/21775744923/example/interstitial", request: AdManagerRequest())
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
[GAMInterstitialAd loadWithAdManagerAdUnitID:@"/21775744923/example/interstitial"
request:[GAMRequest request]
completionHandler:^(GAMInterstitialAd *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;
}
GAMInterstitialAd 物件只能使用一次,這表示插頁式廣告不能重複顯示。最佳做法是在 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:事件處理常式來繼續播放音效。此外,在顯示廣告期間,建議暫停密集的運算工作 (例如遊戲迴圈),以免使用者在觀看時,出現畫面延遲、操作不順或影片斷斷續續的問題。 - 確保載入時間充足。
- 除了在適當時機顯示插頁式廣告外,也要避免讓使用者等候廣告載入。提前載入插頁式廣告,確保應用程式有已經完整載入的廣告可用,能在需要時立即顯示。
- 避免廣告疲勞轟炸。
- 提高插頁式廣告在應用程式內的顯示頻率,看似能增加收益,但也可能破壞使用者體驗並降低點閱率。切勿頻繁打斷使用者,以免他們無法好好使用您的應用程式。
- 請勿使用載入完成的回呼顯示插頁式廣告。
- 這種做法可能導致使用者體驗不佳。建議預先載入廣告,然後檢查
GAMInterstitialAd的canPresentFromRootViewController:error:方法,確認廣告已經就緒,隨時可以顯示。
GitHub 範例程式碼
您可以選取慣用的程式語言,查看完整的插頁式廣告範例: