插頁式廣告

選取平台: Android iOS Unity Flutter

插頁式廣告會全螢幕顯示,覆蓋整個應用程式的介面,直到使用者關閉廣告為止。這類廣告通常在應用程式流程中的自然轉換點顯示,例如活動之間或遊戲關卡間的暫停時間。應用程式顯示插頁式廣告時,使用者可選擇輕觸廣告前往到達網頁,或是關閉廣告返回應用程式。個案研究

本指南說明如何在 iOS 應用程式整合插頁式廣告。

必備條件

請一律使用測試廣告進行測試

建構及測試應用程式時,請務必使用測試廣告,而非實際的正式廣告;違反者帳戶可能會遭到停權。

如要載入測試廣告,最簡單的方法是使用 iOS 插頁式廣告專用的測試廣告單元 ID:
ca-app-pub-3940256099942544/4411468910

這類 ID 經特別設定,可針對每個請求傳回測試廣告。您可在編寫程式碼、測試及偵錯時,將其用於自己的應用程式。發布應用程式前,請務必將這類 ID 替換為自己的廣告單元 ID。

如要進一步瞭解 Mobile Ads SDK 測試廣告的運作方式,請參閱「測試廣告」。

導入作業

整合插頁式廣告的主要步驟如下:

  1. 載入廣告。
  2. 註冊接收回呼。
  3. 顯示廣告。

載入廣告

載入廣告時,請使用 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 thefullScreenContentDelegate` 屬性:

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 物件只能使用一次,也就是說,插頁式廣告顯示後就無法再次顯示。最佳做法是在 GADFullScreenContentDelegateadDidDismissFullScreenContent: 方法中載入另一個插頁式廣告,這樣前一個插頁式廣告關閉後,系統就會立即開始載入下一個插頁式廣告。

顯示廣告

插頁式廣告應在應用程式流程中的自然停頓點顯示,例如遊戲關卡之間,或使用者完成操作後。

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 上的範例

查看以您偏好語言撰寫的插頁式廣告完整範例:

Mobile Ads Garage 教學影片

成功案例

後續步驟