Skip to content

Commit

Permalink
Using nullable view controller parameter for App Open Ads
Browse files Browse the repository at this point in the history
PiperOrigin-RevId: 619566995
  • Loading branch information
Justin Malandruccolo authored and maddevrelgithubbot committed Mar 27, 2024
1 parent 4dc91a4 commit e6b3672
Show file tree
Hide file tree
Showing 14 changed files with 22 additions and 50 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -24,15 +24,8 @@ - (BOOL)application:(UIApplication *)application
}

- (void) applicationDidBecomeActive:(UIApplication *)application {
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"SELF.isKeyWindow == YES"];
UIWindow *keyWindow = [[application.windows filteredArrayUsingPredicate:predicate] firstObject];
UIViewController *rootViewController = keyWindow.rootViewController;
// Do not show app open ad if the current view controller is SplashViewController.
if (!rootViewController ||
[rootViewController isKindOfClass:[SplashViewController class]]) {
return;
}
[AppOpenAdManager.sharedInstance showAdIfAvailable:rootViewController];
// Show the app open ad when the app is foregrounded.
[AppOpenAdManager.sharedInstance showAdIfAvailable];
}

@end
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,6 @@

+ (nonnull AppOpenAdManager *)sharedInstance;
- (void)loadAd;
- (void)showAdIfAvailable:(nonnull UIViewController*)viewController;
- (void)showAdIfAvailable;

@end
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ - (void)loadAd {
}];
}

- (void)showAdIfAvailable:(nonnull UIViewController *)viewController {
- (void)showAdIfAvailable {
// If the app open ad is already showing, do not show the ad again.
if (_isShowingAd) {
NSLog(@"App open ad is already showing.");
Expand All @@ -114,7 +114,7 @@ - (void)showAdIfAvailable:(nonnull UIViewController *)viewController {
}
NSLog(@"App open ad will be displayed.");
_isShowingAd = YES;
[_appOpenAd presentFromRootViewController:viewController];
[_appOpenAd presentFromRootViewController:nil];
}

#pragma mark - GADFullScreenContentDelegate
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ - (void)decrementCounter {
[_countdownTimer invalidate];
_countdownTimer = nil;

[AppOpenAdManager.sharedInstance showAdIfAvailable:self];
[AppOpenAdManager.sharedInstance showAdIfAvailable];
}

- (void)startGoogleMobileAdsSDK {
Expand Down
11 changes: 2 additions & 9 deletions Objective-C/admob/AppOpenExample/AppOpenExample/AppDelegate.m
Original file line number Diff line number Diff line change
Expand Up @@ -24,15 +24,8 @@ - (BOOL)application:(UIApplication *)application
}

- (void) applicationDidBecomeActive:(UIApplication *)application {
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"SELF.isKeyWindow == YES"];
UIWindow *keyWindow = [[application.windows filteredArrayUsingPredicate:predicate] firstObject];
UIViewController *rootViewController = keyWindow.rootViewController;
// Do not show app open ad if the current view controller is SplashViewController.
if (!rootViewController ||
[rootViewController isKindOfClass:[SplashViewController class]]) {
return;
}
[AppOpenAdManager.sharedInstance showAdIfAvailable:rootViewController];
// Show the app open ad when the app is foregrounded.
[AppOpenAdManager.sharedInstance showAdIfAvailable];
}

@end
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,6 @@

+ (nonnull AppOpenAdManager *)sharedInstance;
- (void)loadAd;
- (void)showAdIfAvailable:(nonnull UIViewController*)viewController;
- (void)showAdIfAvailable;

@end
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ - (void)loadAd {
}];
}

- (void)showAdIfAvailable:(nonnull UIViewController *)viewController {
- (void)showAdIfAvailable {
// If the app open ad is already showing, do not show the ad again.
if (_isShowingAd) {
NSLog(@"App open ad is already showing.");
Expand All @@ -114,7 +114,7 @@ - (void)showAdIfAvailable:(nonnull UIViewController *)viewController {
}
NSLog(@"App open ad will be displayed.");
_isShowingAd = YES;
[_appOpenAd presentFromRootViewController:viewController];
[_appOpenAd presentFromRootViewController:nil];
}

#pragma mark - GADFullScreenContentDelegate
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ - (void)decrementCounter {
[_countdownTimer invalidate];
_countdownTimer = nil;

[AppOpenAdManager.sharedInstance showAdIfAvailable:self];
[AppOpenAdManager.sharedInstance showAdIfAvailable];
}

- (void)startGoogleMobileAdsSDK {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,14 +29,7 @@ class AppDelegate: UIResponder, UIApplicationDelegate {
}

func applicationDidBecomeActive(_ application: UIApplication) {
let rootViewController = application.windows.first(
where: { $0.isKeyWindow })?.rootViewController
if let rootViewController = rootViewController {
// Do not show app open ad if the current view controller is SplashViewController.
if rootViewController is SplashViewController {
return
}
AppOpenAdManager.shared.showAdIfAvailable(viewController: rootViewController)
}
// Show the app open ad when the app is foregrounded.
AppOpenAdManager.shared.showAdIfAvailable()
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ class AppOpenAdManager: NSObject {
isLoadingAd = false
}

func showAdIfAvailable(viewController: UIViewController) {
func showAdIfAvailable() {
// If the app open ad is already showing, do not show the ad again.
if isShowingAd {
print("App open ad is already showing.")
Expand All @@ -103,7 +103,7 @@ class AppOpenAdManager: NSObject {
if let ad = appOpenAd {
print("App open ad will be displayed.")
isShowingAd = true
ad.present(fromRootViewController: viewController)
ad.present(fromRootViewController: nil)
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ class SplashViewController: UIViewController, AppOpenAdManagerDelegate {
splashScreenLabel.text = "Done."
countdownTimer?.invalidate()

AppOpenAdManager.shared.showAdIfAvailable(viewController: self)
AppOpenAdManager.shared.showAdIfAvailable()
}

private func startGoogleMobileAdsSDK() {
Expand Down
11 changes: 2 additions & 9 deletions Swift/admob/AppOpenExample/AppOpenExample/AppDelegate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -29,14 +29,7 @@ class AppDelegate: UIResponder, UIApplicationDelegate {
}

func applicationDidBecomeActive(_ application: UIApplication) {
let rootViewController = application.windows.first(
where: { $0.isKeyWindow })?.rootViewController
if let rootViewController = rootViewController {
// Do not show app open ad if the current view controller is SplashViewController.
if rootViewController is SplashViewController {
return
}
AppOpenAdManager.shared.showAdIfAvailable(viewController: rootViewController)
}
// Show the app open ad when the app is foregrounded.
AppOpenAdManager.shared.showAdIfAvailable()
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ class AppOpenAdManager: NSObject {
isLoadingAd = false
}

func showAdIfAvailable(viewController: UIViewController) {
func showAdIfAvailable() {
// If the app open ad is already showing, do not show the ad again.
if isShowingAd {
print("App open ad is already showing.")
Expand All @@ -103,7 +103,7 @@ class AppOpenAdManager: NSObject {
if let ad = appOpenAd {
print("App open ad will be displayed.")
isShowingAd = true
ad.present(fromRootViewController: viewController)
ad.present(fromRootViewController: nil)
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ class SplashViewController: UIViewController, AppOpenAdManagerDelegate {
splashScreenLabel.text = "Done."
countdownTimer?.invalidate()

AppOpenAdManager.shared.showAdIfAvailable(viewController: self)
AppOpenAdManager.shared.showAdIfAvailable()
}

private func startGoogleMobileAdsSDK() {
Expand Down

0 comments on commit e6b3672

Please sign in to comment.