插頁式廣告

插頁式廣告是全螢幕廣告,會覆蓋應用程式介面,直到 使用者關閉的內容這類廣告通常出現在 應用程式的流程,例如活動之間或暫停期間 遊戲關卡使用者可選擇在應用程式顯示插頁式廣告時 讓使用者輕觸廣告前往到達網頁,或關閉廣告並返回 加入 App Engine 應用程式 個案研究

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

先備知識

  • Google Mobile Ads SDK 8.0.0 以上版本。
  • 完成入門指南

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

建構及測試應用程式時,請務必使用測試廣告,而非 現場及正式環境廣告否則可能導致帳戶遭到停權。

要載入測試廣告,最簡單的方法就是使用專屬測試廣告單元編號 iOS 插頁式廣告:
/6499/example/interstitial

這項機制經過特別設定,可針對每個請求傳回測試廣告 在您的程式設計、測試和偵錯時,可以免費使用應用程式。只需製作 務必先用廣告單元 ID 取代廣告單元,再發布應用程式。

若要進一步瞭解 Mobile Ads SDK 測試廣告的運作方式,請參閱: 測試廣告

導入作業

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

  1. 載入廣告。
  2. 註冊回呼。
  3. 顯示廣告並處理獎勵事件。

載入廣告

系統是透過 上的 loadWithAdManagerAdUnitID:request:completionHandler: 方法 GAMInterstitialAd 類別。載入方法時 廣告單元 ID、GAMRequest 物件和 廣告載入成功或失敗時呼叫的完成處理常式。 所載入的 GAMInterstitialAd 物件是 參數。以下範例說明如何載入 在您的ViewController類別中GAMInterstitialAd

Swift

import GoogleMobileAds
import UIKit

class ViewController: UIViewController {

  private var interstitial: GAMInterstitialAd?

  override func viewDidLoad() {
    super.viewDidLoad()

    do {
      interstitial = try await GAMInterstitialAd.load(
        withAdUnitID: "/6499/example/interstitial", request: GAMRequest())
    } catch {
      print("Failed to load interstitial ad with error: \(error.localizedDescription)")
    }
  }
}

Objective-C

@import GoogleMobileAds;
@import UIKit;

@interface ViewController ()

@property(nonatomic, strong) GAMInterstitialAd *interstitial;

@end

@implementation ViewController

- (void)viewDidLoad {
  [super viewDidLoad];
  GAMRequest *request = [GAMRequest request];
  [GAMInterstitialAd loadWithAdManagerAdUnitID:@"/6499/example/interstitial"
      request:request
      completionHandler:^(GAMInterstitialAd *ad, NSError *error) {
    if (error) {
      NSLog(@"Failed to load interstitial ad with error: %@", [error localizedDescription]);
      return;
    }
    self.interstitial = ad;
  }];
}

註冊回呼

您必須執行 GADFullScreenContentDelegate 通訊協定並指派給 傳回廣告的 fullScreenContentDelegate 屬性。 GADFullScreenContentDelegate 通訊協定會處理廣告發生時的回呼 是否會成功或失敗,以及在關閉時啟動。下列 程式碼顯示如何執行通訊協定,並將其指派給廣告:

Swift

import GoogleMobileAds
import UIKit

class ViewController: UIViewController, GADFullScreenContentDelegate {

  private var interstitial: GAMInterstitialAd?

  override func viewDidLoad() {
    super.viewDidLoad()

    do {
      interstitial = try await GAMInterstitialAd.load(
        withAdUnitID: "/6499/example/interstitial", request: GAMRequest())
      interstitial?.fullScreenContentDelegate = self
    } catch {
      print("Failed to load interstitial ad with error: \(error.localizedDescription)")
    }
  }

  /// Tells the delegate that the ad failed to present full screen content.
  func ad(_ ad: GADFullScreenPresentingAd, didFailToPresentFullScreenContentWithError error: Error) {
    print("Ad did fail to present full screen content.")
  }

  /// Tells the delegate that the ad will present full screen content.
  func adWillPresentFullScreenContent(_ ad: GADFullScreenPresentingAd) {
    print("Ad will present full screen content.")
  }

  /// Tells the delegate that the ad dismissed full screen content.
  func adDidDismissFullScreenContent(_ ad: GADFullScreenPresentingAd) {
    print("Ad did dismiss full screen content.")
  }
}

Objective-C

@import GoogleMobileAds;
@import UIKit;

@interface ViewController () <GADFullScreenContentDelegate>

@property(nonatomic, strong) GAMInterstitialAd *interstitial;

@end

@implementation ViewController

- (void)viewDidLoad {
  [super viewDidLoad];
  GAMRequest *request = [GAMRequest request];
  [GAMInterstitialAd loadWithAdManagerAdUnitID:@"/6499/example/interstitial"
      request: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;
  }];
}

/// Tells the delegate that the ad failed to present full screen content.
- (void)ad:(nonnull id<GADFullScreenPresentingAd>)ad
didFailToPresentFullScreenContentWithError:(nonnull NSError *)error {
    NSLog(@"Ad did fail to present full screen content.");
}

/// Tells the delegate that the ad will present full screen content.
- (void)adWillPresentFullScreenContent:(nonnull id<GADFullScreenPresentingAd>)ad {
    NSLog(@"Ad will present full screen content.");
}

/// Tells the delegate that the ad dismissed full screen content.
- (void)adDidDismissFullScreenContent:(nonnull id<GADFullScreenPresentingAd>)ad {
  NSLog(@"Ad did dismiss full screen content.");
}

GAMInterstitialAd 是一次性的物件。這個 這表示插頁式廣告一旦顯示,就無法再次顯示。最佳 是在影片播放器中載入另一則插頁式廣告 GADFullScreenContentDelegateadDidDismissFullScreenContent: 方法,因此 上一個插頁式廣告剛好載入 已關閉。

顯示廣告

插頁式廣告必須在應用程式流程中的自然暫停期間顯示。 在遊戲關卡之間,或是使用者完成任務後,就是不錯的範例。 以下範例說明如何在 UIViewController:

Swift

guard let interstitial = interstitial else {
  return print("Ad wasn't ready.")
}

// The UIViewController parameter is an optional.
interstitial.present(fromRootViewController: nil)

Objective-C

if (self.interstitial) {
  // The UIViewController parameter is nullable.
  [self.interstitial presentFromRootViewController:nil];
} else {
  NSLog(@"Ad wasn't ready");
}

最佳做法

請思考插頁式廣告是否適合您應用程式的廣告類型。
插頁式廣告最適合包含自然轉換點的應用程式。 應用程式內的工作結論,例如分享圖片或完成 就是所謂的遊戲層級由於使用者預期在 您就能輕鬆顯示插頁式廣告,而且不會中斷 無須專人管理請務必考慮在應用程式工作流程中的哪個階段 顯示插頁式廣告以及使用者的回應方式
提醒您,顯示插頁式廣告時應將動作暫停。
插頁式廣告分成幾種不同類型的插頁式廣告:文字、圖像、 影片等。請務必確認應用程式顯示 也會暫停使用部分資源,以便讓廣告 善加利用這些原則舉例來說,如果您呼叫以顯示 插頁式廣告,請務必暫停應用程式產生的任何音訊輸出。 您可在以下位置繼續播放音效: adDidDismissFullScreenContent: 事件處理常式,系統會在使用者完成互動時叫用這個處理常式 與廣告相關。此外,建議您暫時停止 像是遊戲迴圈等作業這能確保 使用者不會遇到執行速度緩慢或沒有回應的圖像,或發生延遲的情形 影片。
提供足夠的載入時間。
同樣重要的是,務必在 並確保使用者不必費心 等待載入完成在預定放送前提早載入廣告 可確保您的應用程式在準備就緒時,已完全載入插頁式廣告 。
不要讓應用程式廣告氾濫。
雖然增加應用程式內插頁式廣告的顯示頻率, 不但有助於增加收益,也可能損及使用者體驗 導致點閱率降低確認使用者不常使用 幹擾,無法繼續享受您的應用程式。
請勿使用載入完成回呼來顯示插頁式廣告。
這會對使用者體驗造成負面影響。請在 表示需要出示的標誌接著檢查 canPresentFromRootViewController:error: 方法。 「GAMInterstitialAd」看看是否已準備好 。

GitHub 上的範例

後續步驟