插頁式廣告

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

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

先備知識

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

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

建構及測試應用程式時,請務必使用測試廣告,而非即時的實際廣告。否則可能導致帳戶遭到停權。

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

這項機制經過特別設定,可針對每個請求傳回測試廣告,您可以在編寫程式碼、進行測試及偵錯時,自由用於自家的應用程式。但別忘了在發布應用程式前,用自己的廣告單元 ID 取代這個 ID。

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

導入作業

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

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

載入廣告

您可以在 GADInterstitialAd 類別上使用靜態 loadWithAdUnitID:request:completionHandler: 方法載入廣告。載入方法需要廣告單元 ID、GADRequest 物件以及廣告載入成功或失敗時呼叫的完成處理常式。系統會在完成處理常式中以參數的形式提供載入的 GADInterstitialAd 物件。以下範例說明如何在 ViewController 類別中載入 GADInterstitialAd

Swift

import GoogleMobileAds
import UIKit

class ViewController: UIViewController {

  private var interstitial: GADInterstitialAd?

  override func viewDidLoad() {
    super.viewDidLoad()

    do {
      interstitial = try await GADInterstitialAd.load(
        withAdUnitID: "ca-app-pub-3940256099942544/4411468910", request: GADRequest())
    } catch {
      print("Failed to load interstitial ad with error: \(error.localizedDescription)")
    }
  }
}

Objective-C

@import GoogleMobileAds;
@import UIKit;

@interface ViewController ()

@property(nonatomic, strong) GADInterstitialAd *interstitial;

@end

@implementation ViewController

- (void)viewDidLoad {
  [super viewDidLoad];
  GADRequest *request = [GADRequest request];
  [GADInterstitialAd loadWithAdUnitID:@"ca-app-pub-3940256099942544/4411468910"
                              request:request
                    completionHandler:^(GADInterstitialAd *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: GADInterstitialAd?

  override func viewDidLoad() {
    super.viewDidLoad()

    do {
      interstitial = try await GADInterstitialAd.load(
        withAdUnitID: "ca-app-pub-3940256099942544/4411468910", request: GADRequest())
      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) GADInterstitialAd *interstitial;

@end

@implementation ViewController

- (void)viewDidLoad {
  [super viewDidLoad];
  GADRequest *request = [GADRequest request];
  [GADInterstitialAd loadWithAdUnitID:@"ca-app-pub-3940256099942544/4411468910"
                              request: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;
  }];
}

/// 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.");
}

GADInterstitialAd 是一次性的物件。這表示插頁式廣告一旦顯示,就無法再次顯示。最佳做法是在 GADFullScreenContentDelegateadDidDismissFullScreenContent: 方法中載入另一個插頁式廣告,這樣上一個插頁式廣告一關閉後就會立即開始載入。

顯示廣告

插頁式廣告應在應用程式流程中的自然暫停期間顯示。在遊戲關卡之間,或使用者完成任務後,是不錯的範例。以下範例說明如何在 UIViewController 的其中一種動作方法中執行此操作:

Swift

@IBAction func doSomething(_ sender: Any) {
  guard let interstitial = interstitial else {
    return print("Ad wasn't ready.")
  }

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

Objective-C

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

最佳做法

請思考插頁式廣告是否適合您應用程式的廣告類型。
插頁式廣告最適合包含自然轉換點的應用程式。 應用程式內的工作結束時 (例如分享圖片或完成遊戲關卡),即會產生此點。由於使用者期待廣告播放中斷,因此可輕鬆放送插頁式廣告,以免干擾使用者體驗。請務必考量您在應用程式工作流程中的哪個階段會顯示插頁式廣告,以及使用者可能的反應。
提醒您,顯示插頁式廣告時應將動作暫停。
插頁式廣告有多種類型,分別是文字、圖像、影片等。請務必確保應用程式顯示插頁式廣告時,也會暫停使用部分資源,以便廣告加以利用。舉例來說,當您呼叫顯示插頁式廣告時,請務必暫停應用程式產生的任何音訊輸出。您可以在 adDidDismissFullScreenContent: 事件處理常式中繼續播放音訊,當使用者完成與廣告互動時就會叫用這個處理常式。此外,建議您在廣告顯示時,暫時停止所有密集的運算工作 (例如遊戲迴圈)。以確保使用者不會遇到速度緩慢或不回應的圖像,或發生延遲的影片。
提供足夠的載入時間。
確保能在適當時間顯示插頁式廣告,同樣重要的是確保使用者不必等待廣告載入。在您打算顯示前預先載入廣告,可確保應用程式及時顯示已完全載入的插頁式廣告。
不要讓應用程式廣告氾濫。
增加應用程式內插頁式廣告的顯示頻率也許是增加收益的好方法,但也可能破壞使用者體驗並降低點閱率。請確保使用者不會經常受到干擾,因此無法再使用您的應用程式。
請勿使用載入完成回呼來顯示插頁式廣告。
這會對使用者體驗造成負面影響。請改為在需要顯示前先預先載入廣告。接著,查看 GADInterstitialAd 上的 canPresentFromRootViewController:error: 方法,確認是否已準備好顯示它。

其他資源

GitHub 上的範例

行動廣告車庫影片教學課程

成功案例

後續步驟