From 51d1465196c916e5814b2fca7ad7596645a37695 Mon Sep 17 00:00:00 2001 From: LTphantom Date: Tue, 13 Aug 2024 15:37:41 -0700 Subject: [PATCH] Updating iOS rootViewController use from loading time to showing time for fullscreen ads (#1153) * Updating iOS rootViewController use from loading time to showing time for Fullscreen ads * Removed rootViewController reference for fullscreen ads --- .../google_mobile_ads/example/ios/Podfile | 2 +- .../ios/RunnerTests/FLTAppOpenAdTest.m | 23 ++++------- .../RunnerTests/FLTGamInterstitialAdTest.m | 10 +---- .../ios/RunnerTests/FLTGoogleMobileAdsTest.m | 39 ++++++++----------- .../ios/RunnerTests/FLTInterstitialAdTest.m | 24 ++++-------- .../ios/RunnerTests/FLTRewardedAdTest.m | 25 ++++-------- .../FLTRewardedInterstitialAdTest.m | 8 +--- .../ios/Classes/FLTAd_Internal.h | 10 ----- .../ios/Classes/FLTAd_Internal.m | 26 +++---------- .../ios/Classes/FLTGoogleMobileAdsPlugin.m | 37 +++++++++++------- 10 files changed, 71 insertions(+), 133 deletions(-) diff --git a/packages/google_mobile_ads/example/ios/Podfile b/packages/google_mobile_ads/example/ios/Podfile index 1cad84c20..03efaafd8 100644 --- a/packages/google_mobile_ads/example/ios/Podfile +++ b/packages/google_mobile_ads/example/ios/Podfile @@ -36,7 +36,7 @@ target 'Runner' do inherit! :search_paths # Matches test_spec dependency. - pod 'OCMock', '3.6' + pod 'OCMock', '3.9.3' end end diff --git a/packages/google_mobile_ads/example/ios/RunnerTests/FLTAppOpenAdTest.m b/packages/google_mobile_ads/example/ios/RunnerTests/FLTAppOpenAdTest.m index 511e39706..1122044e4 100644 --- a/packages/google_mobile_ads/example/ios/RunnerTests/FLTAppOpenAdTest.m +++ b/packages/google_mobile_ads/example/ios/RunnerTests/FLTAppOpenAdTest.m @@ -56,13 +56,9 @@ - (void)testLoadShowGAMRequest { // Helper method for testing with FLTAdRequest and FLTGAMAdRequest. - (void)testLoadShowAppOpenAd:(FLTAdRequest *)request gadOrGAMRequest:(GADRequest *)gadOrGAMRequest { - UIViewController *mockRootViewController = - OCMClassMock([UIViewController class]); - FLTAppOpenAd *ad = - [[FLTAppOpenAd alloc] initWithAdUnitId:@"testId" - request:request - rootViewController:mockRootViewController - adId:@1]; + FLTAppOpenAd *ad = [[FLTAppOpenAd alloc] initWithAdUnitId:@"testId" + request:request + adId:@1]; ad.manager = mockManager; // Stub the load call to invoke successful load callback. @@ -130,8 +126,7 @@ - (void)testLoadShowAppOpenAd:(FLTAdRequest *)request [ad show]; - OCMVerify([appOpenClassMock - presentFromRootViewController:[OCMArg isEqual:mockRootViewController]]); + OCMVerify([appOpenClassMock presentFromRootViewController:[OCMArg isNil]]); // Verify full screen callbacks. OCMVerify([mockManager adWillPresentFullScreenContent:[OCMArg isEqual:ad]]); @@ -162,13 +157,9 @@ - (void)testFailedToLoadGAMRequest { // Helper for testing failed to load. - (void)testFailedToLoad:(FLTAdRequest *)request { - UIViewController *mockRootViewController = - OCMClassMock([UIViewController class]); - FLTAppOpenAd *ad = - [[FLTAppOpenAd alloc] initWithAdUnitId:@"testId" - request:request - rootViewController:mockRootViewController - adId:@1]; + FLTAppOpenAd *ad = [[FLTAppOpenAd alloc] initWithAdUnitId:@"testId" + request:request + adId:@1]; ad.manager = mockManager; id appOpenClassMock = OCMClassMock([GADAppOpenAd class]); diff --git a/packages/google_mobile_ads/example/ios/RunnerTests/FLTGamInterstitialAdTest.m b/packages/google_mobile_ads/example/ios/RunnerTests/FLTGamInterstitialAdTest.m index c02b34fb1..da46fba0b 100644 --- a/packages/google_mobile_ads/example/ios/RunnerTests/FLTGamInterstitialAdTest.m +++ b/packages/google_mobile_ads/example/ios/RunnerTests/FLTGamInterstitialAdTest.m @@ -35,12 +35,9 @@ - (void)testLoadShowInterstitialAd { GAMRequest *gadRequest = OCMClassMock([GAMRequest class]); OCMStub([request asGAMRequest:[OCMArg any]]).andReturn(gadRequest); - UIViewController *mockRootViewController = - OCMClassMock([UIViewController class]); FLTGAMInterstitialAd *ad = [[FLTGAMInterstitialAd alloc] initWithAdUnitId:@"testId" request:request - rootViewController:mockRootViewController adId:@1]; ad.manager = mockManager; @@ -120,8 +117,8 @@ - (void)testLoadShowInterstitialAd { // Show the ad [ad show]; - OCMVerify([interstitialClassMock - presentFromRootViewController:[OCMArg isEqual:mockRootViewController]]); + OCMVerify( + [interstitialClassMock presentFromRootViewController:[OCMArg isNil]]); // Verify full screen callbacks. OCMVerify([mockManager adWillPresentFullScreenContent:[OCMArg isEqual:ad]]); @@ -145,12 +142,9 @@ - (void)testFailToLoad { GAMRequest *gadRequest = OCMClassMock([GAMRequest class]); OCMStub([request asGAMRequest:[OCMArg any]]).andReturn(gadRequest); - UIViewController *mockRootViewController = - OCMClassMock([UIViewController class]); FLTGAMInterstitialAd *ad = [[FLTGAMInterstitialAd alloc] initWithAdUnitId:@"testId" request:request - rootViewController:mockRootViewController adId:@1]; ad.manager = mockManager; diff --git a/packages/google_mobile_ads/example/ios/RunnerTests/FLTGoogleMobileAdsTest.m b/packages/google_mobile_ads/example/ios/RunnerTests/FLTGoogleMobileAdsTest.m index d5b50cae5..427b5e3df 100644 --- a/packages/google_mobile_ads/example/ios/RunnerTests/FLTGoogleMobileAdsTest.m +++ b/packages/google_mobile_ads/example/ios/RunnerTests/FLTGoogleMobileAdsTest.m @@ -249,11 +249,10 @@ - (void)testAdInstanceManagerOnNativeAdEvents { } - (void)testAdInstanceManagerOnRewardedAdUserEarnedReward { - FLTRewardedAd *ad = [[FLTRewardedAd alloc] - initWithAdUnitId:@"testId" - request:[[FLTAdRequest alloc] init] - rootViewController:OCMClassMock([UIViewController class]) - adId:@1]; + FLTRewardedAd *ad = + [[FLTRewardedAd alloc] initWithAdUnitId:@"testId" + request:[[FLTAdRequest alloc] init] + adId:@1]; [_manager loadAd:ad]; [_manager onRewardedAdUserEarnedReward:ad @@ -277,10 +276,9 @@ - (void)testAdInstanceManagerOnRewardedAdUserEarnedReward { - (void)testAdInstanceManagerOnRewardedInterstitialAdUserEarnedReward { FLTRewardedInterstitialAd *ad = [[FLTRewardedInterstitialAd alloc] - initWithAdUnitId:@"testId" - request:[[FLTAdRequest alloc] init] - rootViewController:OCMClassMock([UIViewController class]) - adId:@1]; + initWithAdUnitId:@"testId" + request:[[FLTAdRequest alloc] init] + adId:@1]; [_manager loadAd:ad]; [_manager @@ -369,11 +367,10 @@ - (void)testBannerEvents { } - (void)testFullScreenEventsRewardedAd { - FLTRewardedAd *rewardedAd = [[FLTRewardedAd alloc] - initWithAdUnitId:@"testId" - request:[[FLTAdRequest alloc] init] - rootViewController:OCMClassMock([UIViewController class]) - adId:@1]; + FLTRewardedAd *rewardedAd = + [[FLTRewardedAd alloc] initWithAdUnitId:@"testId" + request:[[FLTAdRequest alloc] init] + adId:@1]; [_manager loadAd:rewardedAd]; [_manager adWillPresentFullScreenContent:rewardedAd]; @@ -400,10 +397,9 @@ - (void)testFullScreenEventsRewardedAd { - (void)testFullScreenEventsRewardedInterstitialAd { FLTRewardedInterstitialAd *rewardedInterstitialAd = [[FLTRewardedInterstitialAd alloc] - initWithAdUnitId:@"testId" - request:[[FLTAdRequest alloc] init] - rootViewController:OCMClassMock([UIViewController class]) - adId:@1]; + initWithAdUnitId:@"testId" + request:[[FLTAdRequest alloc] init] + adId:@1]; [_manager loadAd:rewardedInterstitialAd]; [_manager adWillPresentFullScreenContent:rewardedInterstitialAd]; @@ -464,10 +460,9 @@ - (NSData *)getDataForEvent:(NSString *)name adId:(NSNumber *)adId { - (void)testAdClick { FLTRewardedInterstitialAd *rewardedInterstitialAd = [[FLTRewardedInterstitialAd alloc] - initWithAdUnitId:@"testId" - request:[[FLTAdRequest alloc] init] - rootViewController:OCMClassMock([UIViewController class]) - adId:@1]; + initWithAdUnitId:@"testId" + request:[[FLTAdRequest alloc] init] + adId:@1]; [_manager adDidRecordClick:rewardedInterstitialAd]; NSData *impressionData = [self getDataForEvent:@"adDidRecordClick" adId:@1]; diff --git a/packages/google_mobile_ads/example/ios/RunnerTests/FLTInterstitialAdTest.m b/packages/google_mobile_ads/example/ios/RunnerTests/FLTInterstitialAdTest.m index e850374a8..d3ef3c13d 100644 --- a/packages/google_mobile_ads/example/ios/RunnerTests/FLTInterstitialAdTest.m +++ b/packages/google_mobile_ads/example/ios/RunnerTests/FLTInterstitialAdTest.m @@ -35,13 +35,9 @@ - (void)testLoadShowInterstitialAd { GADRequest *gadRequest = OCMClassMock([GADRequest class]); OCMStub([request asGADRequest:[OCMArg any]]).andReturn(gadRequest); - UIViewController *mockRootViewController = - OCMClassMock([UIViewController class]); - FLTInterstitialAd *ad = - [[FLTInterstitialAd alloc] initWithAdUnitId:@"testId" - request:request - rootViewController:mockRootViewController - adId:@1]; + FLTInterstitialAd *ad = [[FLTInterstitialAd alloc] initWithAdUnitId:@"testId" + request:request + adId:@1]; ad.manager = mockManager; id interstitialClassMock = OCMClassMock([GADInterstitialAd class]); @@ -109,8 +105,8 @@ - (void)testLoadShowInterstitialAd { // Show the ad [ad show]; - OCMVerify([interstitialClassMock - presentFromRootViewController:[OCMArg isEqual:mockRootViewController]]); + OCMVerify( + [interstitialClassMock presentFromRootViewController:[OCMArg isNil]]); // Verify full screen callbacks. OCMVerify([mockManager adWillPresentFullScreenContent:[OCMArg isEqual:ad]]); @@ -129,13 +125,9 @@ - (void)testFailedToLoad { GADRequest *gadRequest = OCMClassMock([GADRequest class]); OCMStub([request asGADRequest:[OCMArg any]]).andReturn(gadRequest); - UIViewController *mockRootViewController = - OCMClassMock([UIViewController class]); - FLTInterstitialAd *ad = - [[FLTInterstitialAd alloc] initWithAdUnitId:@"testId" - request:request - rootViewController:mockRootViewController - adId:@1]; + FLTInterstitialAd *ad = [[FLTInterstitialAd alloc] initWithAdUnitId:@"testId" + request:request + adId:@1]; ad.manager = mockManager; id interstitialClassMock = OCMClassMock([GADInterstitialAd class]); diff --git a/packages/google_mobile_ads/example/ios/RunnerTests/FLTRewardedAdTest.m b/packages/google_mobile_ads/example/ios/RunnerTests/FLTRewardedAdTest.m index b74965cf7..71d6f66d8 100644 --- a/packages/google_mobile_ads/example/ios/RunnerTests/FLTRewardedAdTest.m +++ b/packages/google_mobile_ads/example/ios/RunnerTests/FLTRewardedAdTest.m @@ -50,13 +50,9 @@ - (void)testLoadShowRewardedAdGAMRequest { // Helper method for testing with FLTAdRequest and FLTGAMAdRequest. - (void)testLoadShowRewardedAd:(FLTAdRequest *)request gadOrGAMRequest:(GADRequest *)gadOrGAMRequest { - UIViewController *mockRootViewController = - OCMClassMock([UIViewController class]); - FLTRewardedAd *ad = - [[FLTRewardedAd alloc] initWithAdUnitId:@"testId" - request:request - rootViewController:mockRootViewController - adId:@1]; + FLTRewardedAd *ad = [[FLTRewardedAd alloc] initWithAdUnitId:@"testId" + request:request + adId:@1]; ad.manager = mockManager; // Stub the load call to invoke successful load callback. @@ -149,9 +145,8 @@ - (void)testLoadShowRewardedAd:(FLTAdRequest *)request // Show the ad and verify callbacks invoked [ad show]; - OCMVerify([rewardedClassMock - presentFromRootViewController:[OCMArg isEqual:mockRootViewController] - userDidEarnRewardHandler:[OCMArg any]]); + OCMVerify([rewardedClassMock presentFromRootViewController:[OCMArg isNil] + userDidEarnRewardHandler:[OCMArg any]]); [fullScreenContentDelegate adWillPresentFullScreenContent:rewardedClassMock]; OCMVerify([mockManager adWillPresentFullScreenContent:[OCMArg isEqual:ad]]); @@ -216,13 +211,9 @@ - (void)testFailedToLoadGAMRequest { // Helper for testing failed to load. - (void)testFailedToLoad:(FLTAdRequest *)request { - UIViewController *mockRootViewController = - OCMClassMock([UIViewController class]); - FLTRewardedAd *ad = - [[FLTRewardedAd alloc] initWithAdUnitId:@"testId" - request:request - rootViewController:mockRootViewController - adId:@1]; + FLTRewardedAd *ad = [[FLTRewardedAd alloc] initWithAdUnitId:@"testId" + request:request + adId:@1]; ad.manager = mockManager; id rewardedClassMock = OCMClassMock([GADRewardedAd class]); diff --git a/packages/google_mobile_ads/example/ios/RunnerTests/FLTRewardedInterstitialAdTest.m b/packages/google_mobile_ads/example/ios/RunnerTests/FLTRewardedInterstitialAdTest.m index 33d9384c5..0c5475638 100644 --- a/packages/google_mobile_ads/example/ios/RunnerTests/FLTRewardedInterstitialAdTest.m +++ b/packages/google_mobile_ads/example/ios/RunnerTests/FLTRewardedInterstitialAdTest.m @@ -50,12 +50,9 @@ - (void)testLoadShowRewardedInterstitialAdGAMRequest { // Helper method for testing with FLTAdRequest and FLTGAMAdRequest. - (void)testLoadShowRewardedInterstitialAd:(FLTAdRequest *)request gadOrGAMRequest:(GADRequest *)gadOrGAMRequest { - UIViewController *mockRootViewController = - OCMClassMock([UIViewController class]); FLTRewardedInterstitialAd *ad = [[FLTRewardedInterstitialAd alloc] initWithAdUnitId:@"testId" request:request - rootViewController:mockRootViewController adId:@1]; ad.manager = mockManager; @@ -155,7 +152,7 @@ - (void)testLoadShowRewardedInterstitialAd:(FLTAdRequest *)request [ad show]; OCMVerify([rewardedInterstitialClassMock - presentFromRootViewController:[OCMArg isEqual:mockRootViewController] + presentFromRootViewController:[OCMArg isNil] userDidEarnRewardHandler:[OCMArg any]]); // Verify full screen callbacks. @@ -202,12 +199,9 @@ - (void)testFailedToLoadGAMRequest { // Helper for testing failed to load. - (void)testFailedToLoad:(FLTAdRequest *)request { - UIViewController *mockRootViewController = - OCMClassMock([UIViewController class]); FLTRewardedInterstitialAd *ad = [[FLTRewardedInterstitialAd alloc] initWithAdUnitId:@"testId" request:request - rootViewController:mockRootViewController adId:@1]; ad.manager = mockManager; diff --git a/packages/google_mobile_ads/ios/Classes/FLTAd_Internal.h b/packages/google_mobile_ads/ios/Classes/FLTAd_Internal.h index 92d0fc151..7d8b79f65 100644 --- a/packages/google_mobile_ads/ios/Classes/FLTAd_Internal.h +++ b/packages/google_mobile_ads/ios/Classes/FLTAd_Internal.h @@ -214,8 +214,6 @@ @interface FLTInterstitialAd : FLTFullScreenAd - (instancetype _Nonnull)initWithAdUnitId:(NSString *_Nonnull)adUnitId request:(FLTAdRequest *_Nonnull)request - rootViewController: - (UIViewController *_Nonnull)rootViewController adId:(NSNumber *_Nonnull)adId; - (GADInterstitialAd *_Nullable)interstitial; - (NSString *_Nonnull)adUnitId; @@ -226,16 +224,12 @@ @interface FLTGAMInterstitialAd : FLTInterstitialAd - (instancetype _Nonnull)initWithAdUnitId:(NSString *_Nonnull)adUnitId request:(FLTGAMAdRequest *_Nonnull)request - rootViewController: - (UIViewController *_Nonnull)rootViewController adId:(NSNumber *_Nonnull)adId; @end @interface FLTRewardedAd : FLTFullScreenAd - (instancetype _Nonnull)initWithAdUnitId:(NSString *_Nonnull)adUnitId request:(FLTAdRequest *_Nonnull)request - rootViewController: - (UIViewController *_Nonnull)rootViewController adId:(NSNumber *_Nonnull)adId; - (GADRewardedAd *_Nullable)rewardedAd; - (void)setServerSideVerificationOptions: @@ -245,8 +239,6 @@ @interface FLTRewardedInterstitialAd : FLTFullScreenAd - (instancetype _Nonnull)initWithAdUnitId:(NSString *_Nonnull)adUnitId request:(FLTAdRequest *_Nonnull)request - rootViewController: - (UIViewController *_Nonnull)rootViewController adId:(NSNumber *_Nonnull)adId; - (GADRewardedInterstitialAd *_Nullable)rewardedInterstitialAd; - (void)setServerSideVerificationOptions: @@ -256,8 +248,6 @@ @interface FLTAppOpenAd : FLTFullScreenAd - (instancetype _Nonnull)initWithAdUnitId:(NSString *_Nonnull)adUnitId request:(FLTAdRequest *_Nonnull)request - rootViewController: - (UIViewController *_Nonnull)rootViewController adId:(NSNumber *_Nonnull)adId; - (GADAppOpenAd *_Nullable)appOpenAd; @end diff --git a/packages/google_mobile_ads/ios/Classes/FLTAd_Internal.m b/packages/google_mobile_ads/ios/Classes/FLTAd_Internal.m index 0a82ba796..aeff03b74 100644 --- a/packages/google_mobile_ads/ios/Classes/FLTAd_Internal.m +++ b/packages/google_mobile_ads/ios/Classes/FLTAd_Internal.m @@ -674,20 +674,17 @@ - (void)adDidRecordClick:(nonnull id)ad { @implementation FLTInterstitialAd { GADInterstitialAd *_interstitialView; FLTAdRequest *_adRequest; - UIViewController *_rootViewController; NSString *_adUnitId; } - (instancetype)initWithAdUnitId:(NSString *_Nonnull)adUnitId request:(FLTAdRequest *_Nonnull)request - rootViewController:(UIViewController *_Nonnull)rootViewController adId:(NSNumber *_Nonnull)adId { self = [super init]; if (self) { self.adId = adId; _adRequest = request; _adUnitId = [adUnitId copy]; - _rootViewController = rootViewController; } return self; } @@ -731,7 +728,7 @@ - (void)load { - (void)show { if (self.interstitial) { - [self.interstitial presentFromRootViewController:_rootViewController]; + [self.interstitial presentFromRootViewController:nil]; } else { NSLog(@"InterstitialAd failed to show because the ad was not ready."); } @@ -744,20 +741,17 @@ - (void)show { @implementation FLTGAMInterstitialAd { GAMInterstitialAd *_insterstitial; FLTGAMAdRequest *_adRequest; - UIViewController *_rootViewController; NSString *_adUnitId; } - (instancetype)initWithAdUnitId:(NSString *_Nonnull)adUnitId request:(FLTGAMAdRequest *_Nonnull)request - rootViewController:(UIViewController *_Nonnull)rootViewController adId:(NSNumber *_Nonnull)adId { self = [super init]; if (self) { self.adId = adId; _adRequest = request; _adUnitId = [adUnitId copy]; - _rootViewController = rootViewController; } return self; } @@ -797,7 +791,7 @@ - (void)load { - (void)show { if (self.interstitial) { - [self.interstitial presentFromRootViewController:_rootViewController]; + [self.interstitial presentFromRootViewController:nil]; } else { NSLog(@"InterstitialAd failed to show because the ad was not ready."); } @@ -817,19 +811,16 @@ - (void)interstitialAd:(nonnull GADInterstitialAd *)interstitialAd @implementation FLTRewardedAd { GADRewardedAd *_rewardedView; FLTAdRequest *_adRequest; - UIViewController *_rootViewController; NSString *_adUnitId; } - (instancetype)initWithAdUnitId:(NSString *_Nonnull)adUnitId request:(FLTAdRequest *_Nonnull)request - rootViewController:(UIViewController *_Nonnull)rootViewController adId:(NSNumber *_Nonnull)adId { self = [super init]; if (self) { self.adId = adId; _adRequest = request; - _rootViewController = rootViewController; _adUnitId = [adUnitId copy]; } return self; @@ -881,7 +872,7 @@ - (void)load { - (void)show { if (self.rewardedAd) { [self.rewardedAd - presentFromRootViewController:_rootViewController + presentFromRootViewController:nil userDidEarnRewardHandler:^{ GADAdReward *reward = self.rewardedAd.adReward; FLTRewardItem *fltReward = @@ -912,19 +903,16 @@ - (void)setServerSideVerificationOptions: @implementation FLTRewardedInterstitialAd { GADRewardedInterstitialAd *_rewardedInterstitialView; FLTAdRequest *_adRequest; - UIViewController *_rootViewController; NSString *_adUnitId; } - (instancetype)initWithAdUnitId:(NSString *_Nonnull)adUnitId request:(FLTAdRequest *_Nonnull)request - rootViewController:(UIViewController *_Nonnull)rootViewController adId:(NSNumber *_Nonnull)adId { self = [super init]; if (self) { self.adId = adId; _adRequest = request; - _rootViewController = rootViewController; _adUnitId = [adUnitId copy]; } return self; @@ -979,7 +967,7 @@ - (void)load { - (void)show { if (self.rewardedInterstitialAd) { [self.rewardedInterstitialAd - presentFromRootViewController:_rootViewController + presentFromRootViewController:nil userDidEarnRewardHandler:^{ GADAdReward *reward = self.rewardedInterstitialAd.adReward; FLTRewardItem *fltReward = @@ -1012,20 +1000,16 @@ - (void)setServerSideVerificationOptions: @implementation FLTAppOpenAd { GADAppOpenAd *_appOpenAd; FLTAdRequest *_adRequest; - UIViewController *_rootViewController; NSString *_adUnitId; } - (instancetype _Nonnull)initWithAdUnitId:(NSString *_Nonnull)adUnitId request:(FLTAdRequest *_Nonnull)request - rootViewController: - (UIViewController *_Nonnull)rootViewController adId:(NSNumber *_Nonnull)adId { self = [super init]; if (self) { self.adId = adId; _adRequest = request; - _rootViewController = rootViewController; _adUnitId = [adUnitId copy]; } return self; @@ -1077,7 +1061,7 @@ - (void)load { - (void)show { if (self.appOpenAd) { - [self.appOpenAd presentFromRootViewController:_rootViewController]; + [self.appOpenAd presentFromRootViewController:nil]; } else { NSLog(@"AppOpenAd failed to show because the ad was not ready."); } diff --git a/packages/google_mobile_ads/ios/Classes/FLTGoogleMobileAdsPlugin.m b/packages/google_mobile_ads/ios/Classes/FLTGoogleMobileAdsPlugin.m index 1defb9db4..9832c0c9b 100644 --- a/packages/google_mobile_ads/ios/Classes/FLTGoogleMobileAdsPlugin.m +++ b/packages/google_mobile_ads/ios/Classes/FLTGoogleMobileAdsPlugin.m @@ -215,8 +215,8 @@ - (UIViewController *)rootController { UIViewController *root = UIApplication.sharedApplication.delegate.window.rootViewController; if ([FLTAdUtil isNull:root]) { - // UIApplication.sharedApplication.delegate.window is not guaranteed to be - // set. Use the keyWindow in this case. +// UIApplication.sharedApplication.delegate.window is not guaranteed to be +// set. Use the keyWindow in this case. #pragma clang diagnostic push #pragma clang diagnostic ignored "-Wdeprecated-declarations" root = UIApplication.sharedApplication.keyWindow.rootViewController; @@ -226,8 +226,20 @@ - (UIViewController *)rootController { // Get the presented view controller. This fixes an issue in the add to app // case: https://github.com/googleads/googleads-mobile-flutter/issues/700 UIViewController *presentedViewController = root; - while (presentedViewController.presentedViewController) { - presentedViewController = presentedViewController.presentedViewController; + while (presentedViewController.presentedViewController && + ![presentedViewController.presentedViewController isBeingDismissed]) { + if ([presentedViewController isKindOfClass:[UITabBarController class]]) { + UITabBarController *tabBarController = + (UITabBarController *)presentedViewController; + presentedViewController = tabBarController.selectedViewController; + } else if ([presentedViewController + isKindOfClass:[UINavigationController class]]) { + UINavigationController *navigationController = + (UINavigationController *)presentedViewController; + presentedViewController = navigationController.visibleViewController; + } else { + presentedViewController = presentedViewController.presentedViewController; + } } return presentedViewController; } @@ -426,16 +438,14 @@ - (void)handleMethodCall:(FlutterMethodCall *)call FLTInterstitialAd *ad = [[FLTInterstitialAd alloc] initWithAdUnitId:call.arguments[@"adUnitId"] request:call.arguments[@"request"] - rootViewController:rootController adId:call.arguments[@"adId"]]; [_manager loadAd:ad]; result(nil); } else if ([call.method isEqualToString:@"loadAdManagerInterstitialAd"]) { FLTGAMInterstitialAd *ad = [[FLTGAMInterstitialAd alloc] - initWithAdUnitId:call.arguments[@"adUnitId"] - request:call.arguments[@"request"] - rootViewController:rootController - adId:call.arguments[@"adId"]]; + initWithAdUnitId:call.arguments[@"adUnitId"] + request:call.arguments[@"request"] + adId:call.arguments[@"adId"]]; [_manager loadAd:ad]; result(nil); } else if ([call.method isEqualToString:@"loadRewardedAd"]) { @@ -455,7 +465,6 @@ - (void)handleMethodCall:(FlutterMethodCall *)call FLTRewardedAd *ad = [[FLTRewardedAd alloc] initWithAdUnitId:call.arguments[@"adUnitId"] request:request - rootViewController:rootController adId:call.arguments[@"adId"]]; [_manager loadAd:ad]; result(nil); @@ -474,10 +483,9 @@ - (void)handleMethodCall:(FlutterMethodCall *)call } FLTRewardedInterstitialAd *ad = [[FLTRewardedInterstitialAd alloc] - initWithAdUnitId:call.arguments[@"adUnitId"] - request:request - rootViewController:rootController - adId:call.arguments[@"adId"]]; + initWithAdUnitId:call.arguments[@"adUnitId"] + request:request + adId:call.arguments[@"adId"]]; [_manager loadAd:ad]; result(nil); } else if ([call.method isEqualToString:@"loadAppOpenAd"]) { @@ -496,7 +504,6 @@ - (void)handleMethodCall:(FlutterMethodCall *)call FLTAppOpenAd *ad = [[FLTAppOpenAd alloc] initWithAdUnitId:call.arguments[@"adUnitId"] request:request - rootViewController:rootController adId:call.arguments[@"adId"]]; [_manager loadAd:ad]; result(nil);