本指南適用於整合應用程式開啟頁面廣告的發布商。
應用程式開啟頁面廣告是一種特殊廣告格式,適合想利用自家應用程式載入畫面營利的發布商。使用者可以隨時關閉應用程式開啟頁面廣告。應用程式開啟頁面廣告可在使用者將應用程式移至前景時顯示。
應用程式開啟頁面廣告會自動保留一小塊區域,向使用者顯示他們在應用程式中的樣子。以下是應用程式開啟頁面廣告的模樣:
大致來說,以下是導入應用程式開啟頁面廣告的必要步驟:
- 建立可在廣告顯示前載入廣告的管理員類別。
- 在應用程式前景事件期間顯示新增內容。
- 處理顯示回呼。
必要條件
一律使用測試廣告進行測試
建構及測試應用程式時,請務必使用測試廣告,而非正式版廣告。否則可能導致帳戶遭到停權。
如要載入測試廣告,最簡單的方法是使用應用程式開啟頁面廣告的專屬測試廣告單元 ID:
ca-app-pub-3940256099942544/5575463023
我們已特別將每個請求設為傳回測試廣告;編寫程式碼、測試及偵錯時,您可以在自己的應用程式中自由使用這項工具。但請注意,發布應用程式前,請務必先將它換成自己的廣告單元 ID。
如要進一步瞭解 Mobile Ads SDK 測試廣告的運作方式,請參閱「測試廣告」一文。
導入管理員類別
廣告應該能快速顯示,因此建議您在需要顯示廣告前先載入廣告。這樣一來,使用者一進入應用程式,就能立刻開始放送廣告。導入管理員類別,即可在需要顯示廣告時預先發出廣告請求。
建立名為 AppOpenAdManager
的新單例模式類別,並填入此類別,如下所示:
Swift
class AppOpenAdManager: NSObject {
var appOpenAd: GADAppOpenAd?
var isLoadingAd = false.
var isShowingAd = false
static let shared = AppOpenAdManager()
private func loadAd() async {
// TODO: Implement loading an ad.
}
func showAdIfAvailable() {
// TODO: Implement showing an ad.
}
private func isAdAvailable() -> Bool {
// Check if ad exists and can be shown.
return appOpenAd != nil
}
}
Objective-C
@interface AppOpenAdManager ()
@property(nonatomic, strong) GADAppOpenAd *appOpenAd;
@property(nonatomic, assign) BOOL isLoadingAd;
@property(nonatomic, assign) BOOL isShowingAd;
@end
@implementation AppOpenAdManager
+ (nonnull AppOpenAdManager *)sharedInstance {
static AppOpenAdManager *instance = nil;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
instance = [[AppOpenAdManager alloc] init];
});
return instance;
}
- (void)loadAd {
// TODO: Implement loading an ad.
}
// Add this method to the .h file.
- (void)showAdIfAvailable {
// TODO: Implement showing an ad.
}
- (BOOL)isAdAvailable {
// Check if ad exists and can be shown.
return self.appOpenAd != nil;
}
@end
載入廣告
下一步是填寫 loadAd()
方法。
Swift
private 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 GADAppOpenAd.load(
withAdUnitID: "ca-app-pub-3940256099942544/5575463023", request: GADRequest())
} catch {
print("App open ad failed to load with error: \(error.localizedDescription)")
}
isLoadingAd = false
}
Objective-C
- (void)loadAd {
// Do not load ad if there is an unused ad or one is already loading.
if (self.isLoadingAd || [self isAdAvailable]) {
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(@"Failed to load app open ad: %@", error);
return;
}
self.appOpenAd = appOpenAd;
}];
}
顯示廣告
下一步是填寫 showAdIfAvailable()
方法。如果沒有任何廣告可放送,這個方法會嘗試載入廣告。
Swift
func showAdIfAvailable() {
// If the app open ad is already showing, do not show the ad again.
guard !isShowingAd else { return }
// If the app open ad is not available yet but is supposed to show, load
// a new ad.
if !isAdAvailable() {
Task {
await loadAd()
}
return
}
if let ad = appOpenAd {
isShowingAd = true
ad.present(fromRootViewController: nil)
}
}
Objective-C
- (void)showAdIfAvailable {
// If the app open ad is already showing, do not show the ad again.
if (self.isShowingAd) {
return;
}
// If the app open ad is not available yet but is supposed to show, load a
// new ad.
if (![self isAdAvailable]) {
[self loadAd];
return;
}
self.isShowingAd = YES;
[self.appOpenAd presentFromRootViewController:nil];
}
在應用程式前景事件期間顯示廣告
應用程式啟用後,請呼叫 showAdIfAvailable()
以顯示廣告 (如有),或載入新廣告。
Swift
@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {
// ...
func applicationDidBecomeActive(_ application: UIApplication) {
// Show the app open ad when the app is foregrounded.
AppOpenAdManager.shared.showAdIfAvailable()
}
}
Objective-C
@implementation AppDelegate
// ...
- (void) applicationDidBecomeActive:(UIApplication *)application {
// Show the app open ad when the app is foregrounded.
[AppOpenAdManager.sharedInstance showAdIfAvailable];
}
@end
處理顯示回呼
應用程式顯示應用程式開啟頁面廣告時,您應該透過 GADFullScreenContentDelegate
處理特定呈現事件。請特別注意,在第一則廣告完成放送後,建議您請求下一個應用程式開啟頁面廣告。
在 AppOpenAdManager
類別中,新增以下內容:
Swift
class AppOpenAdManager: NSObject, GADFullScreenContentDelegate {
// ...
private 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 GADAppOpenAd.load(
withAdUnitID: "ca-app-pub-3940256099942544/5575463023", request: GADRequest())
appOpenAd?.fullScreenContentDelegate = self
} catch {
print("App open ad failed to load with error: \(error.localizedDescription)")
}
isLoadingAd = false
}
// ...
// MARK: - GADFullScreenContentDelegate methods
func adWillPresentFullScreenContent(_ ad: GADFullScreenPresentingAd) {
print("App open ad will be presented.")
}
func adDidDismissFullScreenContent(_ ad: GADFullScreenPresentingAd) {
appOpenAd = nil
isShowingAd = false
// Reload an ad.
Task {
await loadAd()
}
}
func ad(
_ ad: GADFullScreenPresentingAd,
didFailToPresentFullScreenContentWithError error: Error
) {
appOpenAd = nil
isShowingAd = false
// Reload an ad.
Task {
await loadAd()
}
}
}
Objective-C
@interface AppOpenAdManager () <GADFullScreenContentDelegate>
@property(nonatomic, strong) GADAppOpenAd *appOpenAd
@property(nonatomic, assign) BOOL isLoadingAd;
@property(nonatomic, assign) BOOL isShowingAd;
@end
@implementation AppOpenAdManager
// ...
- (void)loadAd {
// Do not load ad if there is an unused ad or one is already loading.
if (self.isLoadingAd || [self isAdAvailable]) {
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(@"Failed to load app open ad: %@", error);
return;
}
self.appOpenAd = appOpenAd;
self.appOpenAd.fullScreenContentDelegate = self;
}];
}
- (BOOL)isAdAvailable {
// Check if ad exists and can be shown.
return self.appOpenAd != nil;
}
// ...
#pragma mark - GADFullScreenContentDelegate methods
- (void)adWillPresentFullScreenContent:(nonnull id<GADFullScreenPresentingAd>)ad {
NSLog(@"App open ad is will be presented.");
}
- (void)adDidDismissFullScreenContent:(nonnull id<GADFullScreenPresentingAd>)ad {
self.appOpenAd = nil;
self.isShowingAd = NO;
// Reload an ad.
[self loadAd];
}
- (void)ad:(nonnull id<GADFullScreenPresentingAd>)ad
didFailToPresentFullScreenContentWithError:(nonnull NSError *)error {
self.appOpenAd = nil;
self.isShowingAd = NO;
// Reload an ad.
[self loadAd];
}
@end
考慮廣告效期
為確保不會顯示過期的廣告,您可以為應用程式委派項目新增方法,檢查廣告參照載入後經過的時間。
在 AppOpenAdManager
中,新增名為 loadTime
的 Date
屬性,並在廣告載入時設定屬性。接著,您可以新增一個方法,如果廣告載入後已超過特定時數,會傳回 true
。顯示廣告前,請務必先檢查廣告參照的有效性。
Swift
class AppOpenAdManager: NSObject, GADFullScreenContentDelegate {
var appOpenAd: GADAppOpenAd?
var isLoadingAd = false.
var isShowingAd = false
var loadTime: Date?
let fourHoursInSeconds = TimeInterval(3600 * 4)
// ...
private 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 GADAppOpenAd.load(
withAdUnitID: "ca-app-pub-3940256099942544/5575463023", request: GADRequest())
appOpenAd?.fullScreenContentDelegate = self
loadTime = Date()
} catch {
print("App open ad failed to load with error: \(error.localizedDescription)")
}
isLoadingAd = false
}
private func wasLoadTimeLessThanFourHoursAgo() -> Bool {
guard let loadTime = loadTime else { return false }
// Check if ad was loaded more than four hours ago.
return Date().timeIntervalSince(loadTime) < fourHoursInSeconds
}
private func isAdAvailable() -> Bool {
// Check if ad exists and can be shown.
return appOpenAd != nil && wasLoadTimeLessThanFourHoursAgo()
}
}
Objective-C
static NSTimeInterval const fourHoursInSeconds = 3600 * 4;
@interface AppOpenAdManager () <GADFullScreenContentDelegate>
@property(nonatomic, strong) GADAppOpenAd *appOpenAd
@property(nonatomic, assign) BOOL isLoadingAd;
@property(nonatomic, assign) BOOL isShowingAd;
@property(weak, nonatomic) NSDate *loadTime;
@end
@implementation AppOpenAdManager
// ...
- (void)loadAd {
// Do not load ad if there is an unused ad or one is already loading.
if (self.isLoadingAd || [self isAdAvailable]) {
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(@"Failed to load app open ad: %@", error);
return;
}
self.appOpenAd = appOpenAd;
self.appOpenAd.fullScreenContentDelegate = self;
self.loadTime = [NSDate date];
}];
}
- (BOOL)wasLoadTimeLessThanFourHoursAgo {
// Check if ad was loaded more than four hours ago.
return [[NSDate Date] timeIntervalSinceDate:self.loadTime] < fourHoursInSeconds;
}
- (BOOL)isAdAvailable {
// Check if ad exists and can be shown.
return self.appOpenAd != nil && [self wasLoadTimeLessThanFourHoursAgo];
}
@end
冷啟動和載入畫面
本文假設您的應用程式開啟頁面廣告只有在使用者前景運作時,才會在記憶體中暫停顯示。如果應用程式曾啟動,但尚未在記憶體中暫停,就會發生「冷啟動」。
冷啟動的例子之一,就是使用者首次開啟您的應用程式。冷啟動機制不會立即載入,但先前載入的應用程式開啟頁面廣告無法立即顯示,您提出廣告請求並收到廣告請求之間的延遲時間,可能會導致使用者能短暫使用您的應用程式,而再因內容外的廣告而感到出乎意料。請避免執行這項作業,以免造成使用者體驗不佳。
如要在冷啟動時使用應用程式開啟頁面廣告,建議您透過載入畫面載入遊戲或應用程式素材資源,並只在載入畫面中顯示廣告。如果應用程式已完成載入,並將使用者帶往應用程式的主要內容,請不要顯示廣告。
最佳做法
Google 建構的應用程式開啟頁面廣告可協助您透過應用程式的載入畫面營利,但請務必謹記最佳做法,讓使用者能盡情使用您的應用程式。請務必:
- 等使用者用過應用程式幾次之後,再放送第一則應用程式開啟頁面廣告。
- 在使用者可能等待系統載入應用程式的時段,顯示應用程式開啟頁面廣告。
- 如果您的應用程式開啟頁面廣告下方有一個載入畫面,且載入畫面在廣告關閉前就完成載入,建議您關閉
adDidDismissFullScreenContent
方法中的載入畫面。
完整 GitHub 上的範例
後續步驟
進一步瞭解使用者隱私。