This guide is intended for publishers integrating app open ads.
App open ads are a special ad format intended for publishers wishing to monetize their app load screens. App open ads can be closed by your users at any time. App open ads can be shown when users bring your app to the foreground.
App open ads automatically show a small branding area so users know they're in your app. Here is an example of what an app open ad looks like:
At a high level, here are the steps required to implement app open ads:
- Create a manager class that loads an ad before you need to display it.
- Show the add during app foregrounding events.
- Handle presentation callbacks.
Prerequisites
- Follow the setup instructions in our Get Started guide.
- Know how to configure your device as a test device.
Always test with test ads
When building and testing your apps, make sure you use test ads rather than live, production ads. Failure to do so can lead to suspension of your account.
The easiest way to load test ads is to use our dedicated test ad unit ID for app open ads:
/21775744923/example/app-open
It's been specially configured to return test ads for every request, and you're free to use it in your own apps while coding, testing, and debugging. Just make sure you replace it with your own ad unit ID before publishing your app.
For more information about how the Mobile Ads SDK's test ads work, see Test Ads.
Implement a manager class
Your ad should show quickly, so it's best to load your ad before you need to display it. That way, you'll have an ad ready to go as soon as the user enters your app. Implement a manager class to make ad requests ahead of when you need to show the ad.
Create a new singleton class called AppOpenAdManager
and fill it out as
follows:
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
Load an ad
The next step is to fill out the loadAd()
method.
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;
}];
}
Show an ad
The next step is to fill out the showAdIfAvailable()
method. If no ad is
available, the method attempts to load an ad.
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];
}
Show the ad during app foregrounding events
When the application becomes active, call showAdIfAvailable()
to show an ad if
one is available, or loads a new one.
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
Handle presentation callbacks
When your app displays an app open ad, you should rely on the
GADFullScreenContentDelegate
to handle certain presentation events. In
particular, you'll want to request the next app open ad once the first one
finishes presenting.
In your AppOpenAdManager
class, add the following:
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
Consider ad expiration
To ensure you don't show an expired ad, you can add a method to the app delegate that checks the elapsed time since your ad reference loaded.
In your AppOpenAdManager
add a Date
property called loadTime
and set the
property when your ad loads. You can then add a method that returns true
if
less than a certain number of hours have passed since your ad loaded. Make sure
you check the validity of your ad reference before you try to show the ad.
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
Cold starts and loading screens
The documentation assumes that you only show app open ads when users foreground your app when it is suspended in memory. "Cold starts" occur when your app is launched but was not previously suspended in memory.
An example of a cold start is when a user opens your app for the first time. With cold starts, you won't have a previously loaded app open ad that's ready to be shown right away. The delay between when you request an ad and receive an ad back can create a situation where users are able to briefly use your app before being surprised by an out of context ad. This should be avoided because it is a bad user experience.
The preferred way to use app open ads on cold starts is to use a loading screen to load your game or app assets, and to only show the ad from the loading screen. If your app has completed loading and has sent the user to the main content of your app, don't show the ad.
Best practices
Google built app open ads to help you monetize your app's loading screen, but it's important to keep best practices in mind so that your users enjoy using your app. Make sure to:
- Wait to show your first app open ad until after your users have used your app a few times.
- Show app open ads during times when your users would otherwise be waiting for your app to load.
- If you have a loading screen under the app open ad, and your loading screen
completes loading before the ad is dismissed, you may want to dismiss your
loading screen in the
adDidDismissFullScreenContent
method.
Complete example on GitHub
Next steps
Learn more about user privacy.