插頁式廣告是全螢幕廣告,會覆蓋應用程式的介面,直到使用者關閉為止。這類廣告顯示的時間點通常都是在應用程式流程中的自然轉換點,例如操作後的空檔或遊戲關卡之間的暫停時間。當應用程式顯示插頁式廣告時,使用者可以選擇輕觸廣告前往到達網頁,或是關閉廣告返回應用程式。 個案研究。
本指南說明如何將插頁式廣告整合至 iOS 應用程式。
必要條件
- Google Mobile Ads SDK 8.0.0 以上版本。
- 完成入門指南。
請務必使用測試廣告進行測試
建構及測試應用程式時,請務必使用測試廣告,而非實際的正式版廣告。否則可能導致帳戶停權。
如要載入測試廣告,最簡單的方法是使用 iOS 插頁廣告專用的測試廣告單元 ID:
/21775744923/example/interstitial
這項廣告單元已特別設定為針對每項要求傳回測試廣告,您可以在編寫程式碼、測試及偵錯時,在自己的應用程式中自由使用這項廣告單元。只要在發布應用程式前,將其替換為自己的廣告單元 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 上的範例
查看您偏好的語言的完整插頁式廣告範例: