本指南面向植入开屏广告的发布商。
开屏广告是一种特殊的广告格式,适合希望通过应用加载页面创收的发布商。用户可以随时关闭开屏广告。 开屏广告可以在用户将您的应用调到前台时展示。
开屏广告会自动在一小块区域内展示品牌信息,让用户知道他们是在 。开屏广告的外观示例如下:
概括来讲,植入开屏广告需要执行以下步骤:
- 创建一个管理器类,用于在您需要展示广告之前加载广告。
- 在应用前台事件期间显示添加。
- 处理展示回调。
前提条件
务必用测试广告进行测试
在构建和测试应用时,请确保使用的是测试广告,而不是实际投放的广告。否则,可能会导致您的账号被暂停。
加载测试广告最简便的方法就是使用我们针对应用的专用测试广告单元 ID 打开广告:
/21775744923/example/app-open
该测试广告单元 ID 已经过专门配置,可为每个请求返回测试广告, 在自己应用的编码、测试和调试过程中随意使用。只需制作 请务必在发布应用前用您自己的广告单元 ID 替换该测试广告单元。
如需详细了解移动广告 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: "/21775744923/example/app-open", request: GAMRequest())
} 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:@"/21775744923/example/app-open"
request:[GAMRequest 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: "/21775744923/example/app-open", request: GAMRequest())
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:@"/21775744923/example/app-open"
request:[GAMRequest 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: "/21775744923/example/app-open", request: GAMRequest())
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:@"/21775744923/example/app-open"
request:[GAMRequest 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 上的完整示例
后续步骤
详细了解用户隐私。