本指南適用於整合應用程式開啟頁面廣告的發布商。
應用程式開啟頁面廣告是一種特殊廣告格式,適合希望藉由應用程式載入畫面賺取收益的發布商。使用者隨時可以關閉應用程式開啟頁面廣告。 應用程式開啟頁面廣告會在使用者將應用程式切換至前景時出現。
應用程式開啟頁面廣告會自動保留一小塊畫面,向使用者顯示應用程式的品牌資訊。以下是應用程式開啟頁面廣告的範例:
如要導入應用程式開啟頁面廣告,大致需要完成下列步驟:
- 建立管理員類別,在需要顯示廣告前載入廣告。
- 在應用程式前景事件期間顯示廣告。
- 處理簡報回呼。
必要條件
一律使用測試廣告進行測試
建構及測試應用程式時,請務必使用測試廣告,而非實際的正式廣告。否則帳戶可能會遭到停權。
如要載入測試廣告,最簡單的方法是使用應用程式開頭廣告專用的測試廣告單元 ID:
ca-app-pub-3940256099942544/5575463023
這個 ID 經過特別設定,可針對每項要求傳回測試廣告,您可以在編寫、測試及偵錯時,自由地在自己的應用程式中使用。發布應用程式前,請務必將其替換為您自己的廣告單元 ID。
如要進一步瞭解 Mobile Ads SDK 測試廣告的運作方式,請參閱「測試廣告」。
實作管理員類別
廣告應能快速顯示,因此最好在需要顯示廣告前載入廣告。這樣一來,使用者一進入應用程式,您就能立即放送廣告。請實作管理員類別,在需要顯示廣告前提出廣告請求。
建立名為 AppOpenAdManager
的新單例類別:
Swift
class AppOpenAdManager: NSObject {
/// The app open ad.
var appOpenAd: AppOpenAd?
/// Maintains a reference to the delegate.
weak var appOpenAdManagerDelegate: AppOpenAdManagerDelegate?
/// Keeps track of if an app open ad is loading.
var isLoadingAd = false
/// Keeps track of if an app open ad is showing.
var isShowingAd = false
/// Keeps track of the time when an app open ad was loaded to discard expired ad.
var loadTime: Date?
/// For more interval details, see https://support.google.com/admob/answer/9341964
let timeoutInterval: TimeInterval = 4 * 3_600
static let shared = AppOpenAdManager()
Objective-C
@interface AppOpenAdManager ()
/// The app open ad.
@property(nonatomic, strong, nullable) GADAppOpenAd *appOpenAd;
/// Keeps track of if an app open ad is loading.
@property(nonatomic, assign) BOOL isLoadingAd;
/// Keeps track of if an app open ad is showing.
@property(nonatomic, assign) BOOL isShowingAd;
/// Keeps track of the time when an app open ad was loaded to discard expired ad.
@property(nonatomic, strong, nullable) NSDate *loadTime;
@end
/// For more interval details, see https://support.google.com/admob/answer/9341964
static const NSInteger kTimeoutInterval = 4;
@implementation AppOpenAdManager
+ (nonnull AppOpenAdManager *)sharedInstance {
static AppOpenAdManager *instance = nil;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
instance = [[AppOpenAdManager alloc] init];
});
return instance;
}
並實作其 AppOpenAdManagerDelegate
通訊協定:
Swift
protocol AppOpenAdManagerDelegate: AnyObject {
/// Method to be invoked when an app open ad life cycle is complete (i.e. dismissed or fails to
/// show).
func appOpenAdManagerAdDidComplete(_ appOpenAdManager: AppOpenAdManager)
}
Objective-C
@protocol AppOpenAdManagerDelegate <NSObject>
/// Method to be invoked when an app open ad life cycle is complete (i.e. dismissed or fails to
/// show).
- (void)adDidComplete;
@end
載入廣告
下一步是載入應用程式開啟廣告:
Swift
func loadAd() async {
// Do not load ad if there is an unused ad or one is already loading.
if isLoadingAd || isAdAvailable() {
return
}
isLoadingAd = true
do {
appOpenAd = try await AppOpenAd.load(
with: "ca-app-pub-3940256099942544/5575463023", request: Request())
appOpenAd?.fullScreenContentDelegate = self
loadTime = Date()
} catch {
print("App open ad failed to load with error: \(error.localizedDescription)")
appOpenAd = nil
loadTime = nil
}
isLoadingAd = false
}
Objective-C
- (void)loadAd {
// Do not load ad if there is an unused ad or one is already loading.
if ([self isAdAvailable] || self.isLoadingAd) {
return;
}
self.isLoadingAd = YES;
[GADAppOpenAd loadWithAdUnitID:@"ca-app-pub-3940256099942544/5575463023"
request:[GADRequest request]
completionHandler:^(GADAppOpenAd * _Nullable appOpenAd, NSError * _Nullable error) {
self.isLoadingAd = NO;
if (error) {
NSLog(@"App open ad failed to load with error: %@", error);
self.appOpenAd = nil;
self.loadTime = nil;
return;
}
self.appOpenAd = appOpenAd;
self.appOpenAd.fullScreenContentDelegate = self;
self.loadTime = [NSDate date];
}];
}
顯示廣告
下一步是顯示應用程式開啟廣告。如果沒有可用的廣告,請嘗試載入新廣告。
Swift
func showAdIfAvailable() {
// If the app open ad is already showing, do not show the ad again.
if isShowingAd {
return print("App open ad is already showing.")
}
// If the app open ad is not available yet but is supposed to show, load
// a new ad.
if !isAdAvailable() {
print("App open ad is not ready yet.")
// The app open ad is considered to be complete in this example.
appOpenAdManagerDelegate?.appOpenAdManagerAdDidComplete(self)
// Load a new ad.
return
}
if let appOpenAd {
appOpenAd.present(from: nil)
isShowingAd = true
}
}
Objective-C
- (void)showAdIfAvailable {
// If the app open ad is already showing, do not show the ad again.
if (self.isShowingAd) {
NSLog(@"App open ad is already showing.");
return;
}
// If the app open ad is not available yet but is supposed to show, load
// a new ad.
if (![self isAdAvailable]) {
NSLog(@"App open ad is not ready yet.");
// The app open ad is considered to be complete in this example.
[self adDidComplete];
// Load a new ad.
return;
}
[self.appOpenAd presentFromRootViewController:nil];
self.isShowingAd = YES;
}
在應用程式前景事件期間顯示廣告
應用程式啟動時,請呼叫 showAdIfAvailable()
顯示廣告 (如有),或載入新廣告。
Swift
func applicationDidBecomeActive(_ application: UIApplication) {
// Show the app open ad when the app is foregrounded.
AppOpenAdManager.shared.showAdIfAvailable()
}
Objective-C
- (void) applicationDidBecomeActive:(UIApplication *)application {
// Show the app open ad when the app is foregrounded.
[AppOpenAdManager.sharedInstance showAdIfAvailable];
}
處理簡報回呼
如要接收簡報事件的通知,請將 GADFullScreenContentDelegate
指派給傳回廣告的 `fullScreenContentDelegate` 屬性:
Swift
appOpenAd?.fullScreenContentDelegate = self
Objective-C
self.appOpenAd.fullScreenContentDelegate = self;
具體來說,您會在第一個廣告結束顯示後,要求顯示下一個應用程式開啟頁面廣告。下列程式碼顯示如何在 AppOpenAdManager
檔案中實作通訊協定:
Swift
func adDidRecordImpression(_ ad: FullScreenPresentingAd) {
print("App open ad recorded an impression.")
}
func adDidRecordClick(_ ad: FullScreenPresentingAd) {
print("App open ad recorded a click.")
}
func adWillDismissFullScreenContent(_ ad: FullScreenPresentingAd) {
print("App open ad will be dismissed.")
}
func adWillPresentFullScreenContent(_ ad: FullScreenPresentingAd) {
print("App open ad will be presented.")
}
func adDidDismissFullScreenContent(_ ad: FullScreenPresentingAd) {
print("App open ad was dismissed.")
appOpenAd = nil
isShowingAd = false
appOpenAdManagerDelegate?.appOpenAdManagerAdDidComplete(self)
Task {
await loadAd()
}
}
func ad(
_ ad: FullScreenPresentingAd,
didFailToPresentFullScreenContentWithError error: Error
) {
print("App open ad failed to present with error: \(error.localizedDescription)")
appOpenAd = nil
isShowingAd = false
appOpenAdManagerDelegate?.appOpenAdManagerAdDidComplete(self)
Task {
await loadAd()
}
}
Objective-C
- (void)adDidRecordImpression:(nonnull id<GADFullScreenPresentingAd>)ad {
NSLog(@"App open ad recorded an impression.");
}
- (void)adDidRecordClick:(nonnull id<GADFullScreenPresentingAd>)ad {
NSLog(@"App open ad recorded a click.");
}
- (void)adWillPresentFullScreenContent:(nonnull id<GADFullScreenPresentingAd>)ad {
NSLog(@"App open ad will be presented.");
}
- (void)adWillDismissFullScreenContent:(nonnull id<GADFullScreenPresentingAd>)ad {
NSLog(@"App open ad will be dismissed.");
}
- (void)adDidDismissFullScreenContent:(nonnull id<GADFullScreenPresentingAd>)ad {
NSLog(@"App open ad was dismissed.");
self.appOpenAd = nil;
self.isShowingAd = NO;
[self adDidComplete];
[self loadAd];
}
- (void)ad:(nonnull id<GADFullScreenPresentingAd>)ad
didFailToPresentFullScreenContentWithError:(nonnull NSError *)error {
NSLog(@"App open ad failed to present with error: %@", error.localizedDescription);
self.appOpenAd = nil;
self.isShowingAd = NO;
[self adDidComplete];
[self loadAd];
}
考慮廣告效期
為避免顯示已失效的廣告,您可以在應用程式委派中新增方法,檢查廣告參照載入後經過的時間。
在 AppOpenAdManager
中,新增名為 loadTime
的 Date
屬性,並在廣告載入時設定該屬性。接著,您可以新增方法,在廣告載入後經過的時間少於特定時數時傳回 true
。請務必先檢查廣告參照的有效性,再嘗試顯示廣告。
Swift
private func wasLoadTimeLessThanNHoursAgo(timeoutInterval: TimeInterval) -> Bool {
// Check if ad was loaded more than n hours ago.
if let loadTime = loadTime {
return Date().timeIntervalSince(loadTime) < timeoutInterval
}
return false
}
private func isAdAvailable() -> Bool {
// Check if ad exists and can be shown.
return appOpenAd != nil && wasLoadTimeLessThanNHoursAgo(timeoutInterval: timeoutInterval)
}
Objective-C
- (BOOL)wasLoadTimeLessThanNHoursAgo:(int)n {
// Check if ad was loaded more than n hours ago.
NSDate *now = [NSDate date];
NSTimeInterval timeIntervalBetweenNowAndLoadTime = [now timeIntervalSinceDate:self.loadTime];
double secondsPerHour = 3600.0;
double intervalInHours = timeIntervalBetweenNowAndLoadTime / secondsPerHour;
return intervalInHours < n;
}
- (BOOL)isAdAvailable {
// Check if ad exists and can be shown.
return _appOpenAd && [self wasLoadTimeLessThanNHoursAgo:kTimeoutInterval];
}
冷啟動和載入畫面
這份文件假設您只會在使用者將應用程式從記憶體中暫停狀態帶到前景時,顯示應用程式開啟頁面廣告。如果應用程式啟動時未在記憶體中暫停,就會發生「冷啟動」。
舉例來說,使用者第一次開啟應用程式時,就是冷啟動。 冷啟動時,您不會有先前載入的應用程式開啟頁面廣告,因此無法立即顯示。如果您要求廣告後,廣告過一段時間才傳回,使用者可能會在看到與內容無關的廣告前,短暫使用您的應用程式。這會造成使用者體驗不佳,建議您不要採用。
如要在冷啟動時放送應用程式開啟頁面廣告,建議使用載入畫面載入遊戲或應用程式素材資源,並只在載入畫面中顯示廣告。如果應用程式已完成載入,並將使用者導向應用程式的主要內容,請勿顯示廣告。
最佳做法
Google 推出應用程式開啟廣告,協助您透過應用程式的載入畫面營利,但請務必遵守最佳做法,確保使用者享有良好的應用程式體驗。請務必:
- 等使用者用過應用程式幾次之後,再放送第一則應用程式開啟頁面廣告。
- 在使用者等待應用程式載入時,放送應用程式開啟頁面廣告。
- 如果載入畫面顯示在應用程式開啟頁面廣告的背景中,且載入畫面在廣告關閉前就完成載入,建議您利用
adDidDismissFullScreenContent
方法關閉載入畫面。
GitHub 上的完整範例
後續步驟
進一步瞭解使用者隱私權。