插頁式廣告

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

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

必要條件

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

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

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

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

我們已特別將每個請求設為傳回測試廣告;編寫程式碼、測試及偵錯時,您可以在自己的應用程式中自由使用這項工具。但請注意,發布應用程式前,請務必先將它換成自己的廣告單元 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 上的範例

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

成功案例

後續步驟