插頁式廣告

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

本指南說明如何將插頁式廣告整合至 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()
    let request = GADRequest()
    GADInterstitialAd.load(withAdUnitID: "ca-app-pub-3940256099942544/4411468910",
                                request: request,
                      completionHandler: { [self] ad, error in
                        if let error = error {
                          print("Failed to load interstitial ad with error: \(error.localizedDescription)")
                          return
                        }
                        interstitial = ad
                      }
    )
  }
}

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

class ViewController: UIViewController, GADFullScreenContentDelegate {

  private var interstitial: GADInterstitialAd?

  override func viewDidLoad() {
    super.viewDidLoad()
    let request = GADRequest()
    GADInterstitialAd.load(withAdUnitID: "ca-app-pub-3940256099942544/4411468910",
                                request: request,
                      completionHandler: { [self] ad, error in
                        if let error = error {
                          print("Failed to load interstitial ad with error: \(error.localizedDescription)")
                          return
                        }
                        interstitial = ad
                        interstitial?.fullScreenContentDelegate = self
                      }
    )
  }

  /// 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

@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) {
  if interstitial != nil {
    interstitial.present(fromRootViewController: self)
  } else {
    print("Ad wasn't ready")
  }
}

Objective-C

- (IBAction)doSomething:(id)sender {
  ...
  if (self.interstitial) {
    [self.interstitial presentFromRootViewController:self];
  } else {
    NSLog(@"Ad wasn't ready");
  }
}

最佳做法

請思考插頁式廣告是否為您應用程式的合適的廣告類型。
在自然轉換點的應用程式最適合使用插頁式廣告。 在應用程式中完成工作 (例如分享圖片或完成遊戲關卡) 則會建立這類點。由於使用者預期動作會中斷,因此很容易顯示插頁式廣告,避免干擾使用者體驗。請務必考量應用程式工作流程中的哪個階段會放送插頁式廣告,以及使用者可能的反應。
顯示插頁式廣告時,別忘了暫停動作。
插頁式廣告有許多類型:文字、圖像、影片等等。請務必確認當應用程式顯示插頁式廣告時,也會暫停使用某些資源,讓廣告能受益。舉例來說,當您呼叫顯示插頁式廣告時,請務必暫停應用程式產生的任何音訊輸出。您可以在 adDidDismissFullScreenContent: 事件處理常式中繼續播放音訊,當使用者完成與廣告互動時,系統便會叫用這個處理常式。此外,請考慮在廣告顯示時,暫停任何密集運算工作 (例如遊戲迴圈)。如此可確保使用者不會遇到速度緩慢或無反應的圖像或斷斷續續的影片。
提供適當的載入時間。
務必確保你能在適當時機顯示插頁式廣告,同樣要確保使用者不必等待廣告載入。在使用者有意顯示廣告之前就先預先載入廣告,這樣當應用程式顯示時,您的廣告就能顯示已完全載入的插頁式廣告。
不要讓應用程式廣告氾濫。
雖然提高應用程式內插頁式廣告的展示頻率,似乎是提高收益的好方法,但也可能降低使用者體驗並降低點閱率。請確保使用者不會經常受到干擾,以免他們無法再使用您的應用程式。
請勿使用載入完成回呼來顯示插頁式廣告。
這會造成使用者體驗不佳。而是在預先載入之前先預先載入廣告。然後檢查 GADInterstitialAd 上的 canPresentFromRootViewController:error: 方法,確認是否能夠顯示。

其他資源

GitHub 上的範例

行動廣告指南影片教學課程

成功案例

後續步驟