插頁式廣告會全螢幕顯示,覆蓋整個應用程式的介面,直到使用者關閉為止。這類廣告通常出現在應用程式流程中的自然轉換點,比如畫面切換階段或遊戲關卡之間的空檔。應用程式顯示插頁式廣告時,使用者可以輕觸廣告前往連結目標網址,或關閉廣告返回應用程式。個案研究
本指南說明如何在 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 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;
}
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 範例程式碼
您可以依照偏好的程式語言,查看完整的插頁式廣告範例: