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:
ca-app-pub-3940256099942544/5575463023
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
:
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;
}
And implement its AppOpenAdManagerDelegate
protocol:
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
Load an ad
The next step is to load an app open ad:
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];
}];
}
Show an ad
The next step is to show an app open ad. If no ad is available, attempt to load a new ad.
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;
}
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
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];
}
Handle presentation callbacks
To receive notifications for presentation events, you must assign the
GADFullScreenContentDelegate
to the fullScreenContentDelegate`s property of
the returned ad:
Swift
appOpenAd?.fullScreenContentDelegate = self
Objective-C
self.appOpenAd.fullScreenContentDelegate = self;
In particular, you'll want to request the next app open ad once the first one
finishes presenting. The following code shows how to implement the protocol
in your AppOpenAdManager
file:
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];
}
Consider ad expiration
To make sure 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
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];
}
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.