From 4b0f24933a282d678fae33d52a2023cbc8335973 Mon Sep 17 00:00:00 2001 From: Fritz Huie Date: Fri, 8 Feb 2019 13:22:33 -0800 Subject: [PATCH 1/6] Add banner support --- .../Unity/UnityAdapter/GADMAdapterUnity.h | 2 +- .../Unity/UnityAdapter/GADMAdapterUnity.m | 104 +++++++++++++----- .../UnityAdapter/GADMAdapterUnityConstants.h | 2 +- .../UnityAdapter/GADMAdapterUnitySingleton.h | 4 + .../UnityAdapter/GADMAdapterUnitySingleton.m | 69 +++++++++++- 5 files changed, 149 insertions(+), 32 deletions(-) diff --git a/adapters/Unity/UnityAdapter/GADMAdapterUnity.h b/adapters/Unity/UnityAdapter/GADMAdapterUnity.h index b0a6a93a8..0301af6b7 100644 --- a/adapters/Unity/UnityAdapter/GADMAdapterUnity.h +++ b/adapters/Unity/UnityAdapter/GADMAdapterUnity.h @@ -21,6 +21,6 @@ /// Adapter for communicating with the Unity Ads Network to fetch reward-based video ads and /// interstitial ads. @interface GADMAdapterUnity : NSObject + GADMAdapterUnityDataProvider, UnityAdsExtendedDelegate, UnityAdsBannerDelegate> @end diff --git a/adapters/Unity/UnityAdapter/GADMAdapterUnity.m b/adapters/Unity/UnityAdapter/GADMAdapterUnity.m index 30a9cf9e8..5d11b29d7 100644 --- a/adapters/Unity/UnityAdapter/GADMAdapterUnity.m +++ b/adapters/Unity/UnityAdapter/GADMAdapterUnity.m @@ -23,7 +23,7 @@ @interface GADMAdapterUnity () { __weak id _rewardBasedVideoAdConnector; /// Connector from Google Mobile Ads SDK to receive ad configurations. - __weak id _interstitialConnector; + __weak id _networkConnector; /// Placement ID of Unity Ads network. NSString *_placementID; @@ -85,8 +85,6 @@ - (void)setUp { } - (void)requestRewardBasedVideoAd { - _placementID = - [[[_rewardBasedVideoAdConnector credentials] objectForKey:GADMAdapterUnityPlacementID] copy]; _isLoading = YES; [[GADMAdapterUnitySingleton sharedInstance] requestRewardBasedVideoAdWithDelegate:self]; } @@ -113,13 +111,13 @@ - (instancetype)initWithGADMAdNetworkConnector:(id)conne self = [super init]; if (self) { - _interstitialConnector = connector; + _networkConnector = connector; } return self; } - (void)getInterstitial { - id strongConnector = _interstitialConnector; + id strongConnector = _networkConnector; NSString *gameID = [[[strongConnector credentials] objectForKey:GADMAdapterUnityGameID] copy]; _placementID = @@ -137,7 +135,7 @@ - (void)getInterstitial { - (void)presentInterstitialFromRootViewController:(UIViewController *)rootViewController { // We will send adapterWillPresentInterstitial callback before presenting unity ad because the ad // has already loaded. - [_interstitialConnector adapterWillPresentInterstitial:self]; + [_networkConnector adapterWillPresentInterstitial:self]; [[GADMAdapterUnitySingleton sharedInstance] presentInterstitialAdForViewController:rootViewController delegate:self]; @@ -146,10 +144,17 @@ - (void)presentInterstitialFromRootViewController:(UIViewController *)rootViewCo #pragma mark Banner Methods - (void)getBannerWithSize:(GADAdSize)adSize { - // Unity Ads doesn't support banner ads. - id strongConnector = _interstitialConnector; - NSError *error = GADUnityErrorWithDescription(@"Unity Ads doesn't support banner ads."); - [strongConnector adapter:self didFailAd:error]; + id strongNetworkConnector = _networkConnector; + NSString *gameID = + [[[strongNetworkConnector credentials] objectForKey:GADMAdapterUnityGameID] copy]; + _placementID = + [[[strongNetworkConnector credentials] objectForKey:GADMAdapterUnityPlacementID] copy]; + if (!gameID || !_placementID) { + NSError *error = GADUnityErrorWithDescription(@"Game ID and Placement ID cannot be nil."); + [strongNetworkConnector adapter:self didFailAd:error]; + return; + } + [[GADMAdapterUnitySingleton sharedInstance] presentBannerAd:gameID delegate:self]; } - (BOOL)isBannerAnimationOK:(GADMBannerAnimationType)animType { @@ -173,11 +178,11 @@ - (void)unityAdsPlacementStateChanged:(NSString *)placementId } - (void)unityAdsDidFinish:(NSString *)placementID withFinishState:(UnityAdsFinishState)state { - id strongInterstitialConnector = _interstitialConnector; + id strongNetworkConnector = _networkConnector; id strongRewardedConnector = _rewardBasedVideoAdConnector; - if (strongInterstitialConnector) { - [strongInterstitialConnector adapterWillDismissInterstitial:self]; - [strongInterstitialConnector adapterDidDismissInterstitial:self]; + if (strongNetworkConnector) { + [strongNetworkConnector adapterWillDismissInterstitial:self]; + [strongNetworkConnector adapterDidDismissInterstitial:self]; } else if (strongRewardedConnector) { if (state == kUnityAdsFinishStateCompleted) { [strongRewardedConnector adapterDidCompletePlayingRewardBasedVideoAd:self]; @@ -200,28 +205,28 @@ - (void)unityAdsDidStart:(NSString *)placementID { } - (void)unityAdsReady:(NSString *)placementID { - id strongInterstitialConnector = _interstitialConnector; + id strongNetworkConnector = _networkConnector; if (!_isLoading) { return; } - - if (strongInterstitialConnector) { - [strongInterstitialConnector adapterDidReceiveInterstitial:self]; + + if (strongNetworkConnector) { + [strongNetworkConnector adapterDidReceiveInterstitial:self]; } else { - [_rewardBasedVideoAdConnector adapterDidReceiveRewardBasedVideoAd:self]; + [strongRewardedConnector adapterDidReceiveRewardBasedVideoAd:self]; } _isLoading = NO; } - (void)unityAdsDidClick:(NSString *)placementID { - id strongInterstitialConnector = _interstitialConnector; + id strongNetworkConnector = _networkConnector; id strongRewardedConnector = _rewardBasedVideoAdConnector; // The Unity Ads SDK doesn't provide an event for leaving the application, so the adapter assumes // that a click event indicates the user is leaving the application for a browser or deeplink, and // notifies the Google Mobile Ads SDK accordingly. - if (strongInterstitialConnector) { - [strongInterstitialConnector adapterDidGetAdClick:self]; - [strongInterstitialConnector adapterWillLeaveApplication:self]; + if (strongNetworkConnector) { + [strongNetworkConnector adapterDidGetAdClick:self]; + [strongNetworkConnector adapterWillLeaveApplication:self]; } else { [strongRewardedConnector adapterDidGetAdClick:self]; [strongRewardedConnector adapterWillLeaveApplication:self]; @@ -229,15 +234,15 @@ - (void)unityAdsDidClick:(NSString *)placementID { } - (void)unityAdsDidError:(UnityAdsError)error withMessage:(NSString *)message { - id strongInterstitialConnector = _interstitialConnector; + id strongNetworkConnector = _networkConnector; id strongRewardedConnector = _rewardBasedVideoAdConnector; if (!_isLoading) { // Unity Ads show error will only happen after the ad has been loaded. So, we will send // dismiss/close callbacks. if (error == kUnityAdsErrorShowError) { - if (strongInterstitialConnector) { - [strongInterstitialConnector adapterWillDismissInterstitial:self]; - [strongInterstitialConnector adapterDidDismissInterstitial:self]; + if (strongNetworkConnector) { + [strongNetworkConnector adapterWillDismissInterstitial:self]; + [strongNetworkConnector adapterDidDismissInterstitial:self]; } else { [strongRewardedConnector adapterDidCloseRewardBasedVideoAd:self]; } @@ -246,8 +251,8 @@ - (void)unityAdsDidError:(UnityAdsError)error withMessage:(NSString *)message { } NSError *errorWithDescription = GADUnityErrorWithDescription(message); - if (strongInterstitialConnector) { - [strongInterstitialConnector adapter:self didFailAd:errorWithDescription]; + if (strongNetworkConnector) { + [strongNetworkConnector adapter:self didFailAd:errorWithDescription]; } else if (strongRewardedConnector) { [strongRewardedConnector adapter:self didFailToLoadRewardBasedVideoAdwithError:errorWithDescription]; @@ -255,4 +260,45 @@ - (void)unityAdsDidError:(UnityAdsError)error withMessage:(NSString *)message { _isLoading = NO; } +#pragma mark - Unity Banner Delegate Methods + +- (void)unityAdsBannerDidClick:(nonnull NSString *)placementId { + id strongNetworkConnector = _networkConnector; + if (strongNetworkConnector) { + [strongNetworkConnector adapterDidGetAdClick:self]; + [strongNetworkConnector adapterWillLeaveApplication:self]; + } +} + +- (void)unityAdsBannerDidError:(nonnull NSString *)message { + id strongNetworkConnector = _networkConnector; + if (strongNetworkConnector){ + NSError *error = GADUnityErrorWithDescription(@"Unity Ads Banner internal error"); + [strongNetworkConnector adapter:self didFailAd:error]; + } +} + +- (void)unityAdsBannerDidHide:(nonnull NSString *)placementId { + NSLog(@"Unity Ads Banner did hide."); +} + +- (void)unityAdsBannerDidLoad:(nonnull NSString *)placementId view:(nonnull UIView *)view { + id strongNetworkConnector = _networkConnector; + if(strongNetworkConnector){ + [strongNetworkConnector adapter:self didReceiveAdView:view]; + }else{ + NSLog(@"ERROR: Network connector for UnityAds banner adapter not found."); + } +} + +- (void)unityAdsBannerDidShow:(nonnull NSString *)placementId { + NSLog(@"Unity Ads Banner is showing."); +} + +- (void)unityAdsBannerDidUnload:(nonnull NSString *)placementId { + NSLog(@"Unity Ads Banner has unloaded."); +} + @end + + diff --git a/adapters/Unity/UnityAdapter/GADMAdapterUnityConstants.h b/adapters/Unity/UnityAdapter/GADMAdapterUnityConstants.h index 71f55025c..99c1919e8 100644 --- a/adapters/Unity/UnityAdapter/GADMAdapterUnityConstants.h +++ b/adapters/Unity/UnityAdapter/GADMAdapterUnityConstants.h @@ -23,7 +23,7 @@ static NSString *const GADMAdapterUnityGameID = @"gameId"; static NSString *const GADMAdapterUnityPlacementID = @"zoneId"; /// Ad mediation network adapter version. -static NSString *const GADMAdapterUnityVersion = @"3.0.0.1"; +static NSString *const GADMAdapterUnityVersion = @"3.0.1.0"; /// Ad mediation network name. static NSString *const GADMAdapterUnityMediationNetworkName = @"AdMob"; diff --git a/adapters/Unity/UnityAdapter/GADMAdapterUnitySingleton.h b/adapters/Unity/UnityAdapter/GADMAdapterUnitySingleton.h index 432770ea1..b780cf899 100644 --- a/adapters/Unity/UnityAdapter/GADMAdapterUnitySingleton.h +++ b/adapters/Unity/UnityAdapter/GADMAdapterUnitySingleton.h @@ -52,6 +52,10 @@ (id) adapterDelegate; +/// Presents a banner ad for |gameID| with |adapterDelegate| +- (void)presentBannerAd:(NSString *)gameID + delegate: (id) adapterDelegate; + /// Tells the adapter to remove itself as a |adapterDelegate|. - (void)stopTrackingDelegate:(id) adapterDelegate; diff --git a/adapters/Unity/UnityAdapter/GADMAdapterUnitySingleton.m b/adapters/Unity/UnityAdapter/GADMAdapterUnitySingleton.m index 3e4a34304..375f736e6 100644 --- a/adapters/Unity/UnityAdapter/GADMAdapterUnitySingleton.m +++ b/adapters/Unity/UnityAdapter/GADMAdapterUnitySingleton.m @@ -17,18 +17,25 @@ #import "GADMAdapterUnityConstants.h" #import "GADMAdapterUnityWeakReference.h" -@interface GADMAdapterUnitySingleton () { +@interface GADMAdapterUnitySingleton () { /// Array to hold all adapter delegates. NSMutableArray *_adapterDelegates; /// Connector from unity adapter to send Unity callbacks. __weak id _currentShowingUnityDelegate; + + /// Connector from unity adapter to send Banner callbacks + __weak id _currentBannerDelegate; + } @end @implementation GADMAdapterUnitySingleton +NSString* _bannerPlacementID = nil; +bool _bannerRequested = false; + + (instancetype)sharedInstance { static GADMAdapterUnitySingleton *sharedManager = nil; static dispatch_once_t onceToken; @@ -158,6 +165,66 @@ - (void)presentInterstitialAdForViewController:(UIViewController *)viewControlle [UnityAds show:viewController placementId:[adapterDelegate getPlacementID]]; } +#pragma mark - Banner ad methods + +- (void)presentBannerAd:(NSString *)gameID + delegate: +(id) adapterDelegate { + _currentBannerDelegate = adapterDelegate; + + if ([UnityAds isSupported]) { + NSString *placementID = [_currentBannerDelegate getPlacementID]; + if(placementID == nil){ + NSString *description = + [[NSString alloc] initWithFormat:@"Tried to show banners with a nil placement ID"]; + [_currentBannerDelegate unityAdsBannerDidError:description]; + return; + }else{ + _bannerPlacementID = placementID; + } + + if (![UnityAds isInitialized]) { + [self initializeWithGameID:gameID]; + _bannerRequested = true; + }else{ + [UnityAdsBanner setDelegate:self]; + [UnityAdsBanner loadBanner:_bannerPlacementID]; + } + } else { + NSString *description = + [[NSString alloc] initWithFormat:@"Unity Ads is not supported for this device."]; + [_currentBannerDelegate unityAdsBannerDidError:description]; + } +} + +#pragma mark - Unity Banner Delegate Methods + +-(void)unityAdsBannerDidLoad:(NSString *)placementId view:(UIView *)view { + [_currentBannerDelegate unityAdsBannerDidLoad:_bannerPlacementID view:view]; +} + +-(void)unityAdsBannerDidUnload:(NSString *)placementId { + [_currentBannerDelegate unityAdsBannerDidUnload:_bannerPlacementID]; +} + +-(void)unityAdsBannerDidShow:(NSString *)placementId { + [_currentBannerDelegate unityAdsBannerDidShow:_bannerPlacementID]; +} + +-(void)unityAdsBannerDidHide:(NSString *)placementId { + [_currentBannerDelegate unityAdsBannerDidHide:_bannerPlacementID]; +} + +-(void)unityAdsBannerDidClick:(NSString *)placementId { + [_currentBannerDelegate unityAdsBannerDidClick:_bannerPlacementID]; +} + +-(void)unityAdsBannerDidError:(NSString *)message { + NSString *description = + [[NSString alloc] initWithFormat:@"Internal Unity Ads banner error"]; + [_currentBannerDelegate unityAdsBannerDidError:description]; +} + #pragma mark - Unity Delegate Methods - (void)unityAdsPlacementStateChanged:(NSString *)placementId From 3b356c2ebff7dc4409092cd2d9fe9d86d83f566a Mon Sep 17 00:00:00 2001 From: Ross Rothenstine Date: Mon, 22 Apr 2019 14:25:58 -0700 Subject: [PATCH 2/6] Implements new object-based API --- .../UnityAdapter.xcodeproj/project.pbxproj | 4 - .../Unity/UnityAdapter/GADMAdapterUnity.h | 5 +- .../Unity/UnityAdapter/GADMAdapterUnity.m | 236 +++++++--------- .../UnityAdapter/GADMAdapterUnityProtocol.h | 24 -- .../UnityAdapter/GADMAdapterUnitySingleton.h | 39 +-- .../UnityAdapter/GADMAdapterUnitySingleton.m | 258 +----------------- 6 files changed, 106 insertions(+), 460 deletions(-) delete mode 100644 adapters/Unity/UnityAdapter/GADMAdapterUnityProtocol.h diff --git a/adapters/Unity/UnityAdapter.xcodeproj/project.pbxproj b/adapters/Unity/UnityAdapter.xcodeproj/project.pbxproj index 1b1e9550e..c18eff0dc 100644 --- a/adapters/Unity/UnityAdapter.xcodeproj/project.pbxproj +++ b/adapters/Unity/UnityAdapter.xcodeproj/project.pbxproj @@ -36,7 +36,6 @@ 7DA7826A1DDA681E000C088E /* GADMAdapterUnity.h in Headers */ = {isa = PBXBuildFile; fileRef = 7DA7825E1DDA681E000C088E /* GADMAdapterUnity.h */; }; 7DA7826B1DDA681E000C088E /* GADMAdapterUnity.m in Sources */ = {isa = PBXBuildFile; fileRef = 7DA7825F1DDA681E000C088E /* GADMAdapterUnity.m */; }; 7DA7826C1DDA681E000C088E /* GADMAdapterUnityConstants.h in Headers */ = {isa = PBXBuildFile; fileRef = 7DA782601DDA681E000C088E /* GADMAdapterUnityConstants.h */; }; - 7DA7826D1DDA681E000C088E /* GADMAdapterUnityProtocol.h in Headers */ = {isa = PBXBuildFile; fileRef = 7DA782611DDA681E000C088E /* GADMAdapterUnityProtocol.h */; }; 7DA7826E1DDA681E000C088E /* GADMAdapterUnitySingleton.h in Headers */ = {isa = PBXBuildFile; fileRef = 7DA782621DDA681E000C088E /* GADMAdapterUnitySingleton.h */; }; 7DA7826F1DDA681E000C088E /* GADMAdapterUnitySingleton.m in Sources */ = {isa = PBXBuildFile; fileRef = 7DA782631DDA681E000C088E /* GADMAdapterUnitySingleton.m */; }; 7DA782701DDA681E000C088E /* GADMAdapterUnityWeakReference.h in Headers */ = {isa = PBXBuildFile; fileRef = 7DA782641DDA681E000C088E /* GADMAdapterUnityWeakReference.h */; }; @@ -70,7 +69,6 @@ 7DA7825E1DDA681E000C088E /* GADMAdapterUnity.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = GADMAdapterUnity.h; sourceTree = ""; }; 7DA7825F1DDA681E000C088E /* GADMAdapterUnity.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = GADMAdapterUnity.m; sourceTree = ""; }; 7DA782601DDA681E000C088E /* GADMAdapterUnityConstants.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = GADMAdapterUnityConstants.h; sourceTree = ""; }; - 7DA782611DDA681E000C088E /* GADMAdapterUnityProtocol.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = GADMAdapterUnityProtocol.h; sourceTree = ""; }; 7DA782621DDA681E000C088E /* GADMAdapterUnitySingleton.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = GADMAdapterUnitySingleton.h; sourceTree = ""; }; 7DA782631DDA681E000C088E /* GADMAdapterUnitySingleton.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = GADMAdapterUnitySingleton.m; sourceTree = ""; }; 7DA782641DDA681E000C088E /* GADMAdapterUnityWeakReference.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = GADMAdapterUnityWeakReference.h; sourceTree = ""; }; @@ -125,7 +123,6 @@ 7DA7825E1DDA681E000C088E /* GADMAdapterUnity.h */, 7DA7825F1DDA681E000C088E /* GADMAdapterUnity.m */, 7DA782601DDA681E000C088E /* GADMAdapterUnityConstants.h */, - 7DA782611DDA681E000C088E /* GADMAdapterUnityProtocol.h */, 7DA782621DDA681E000C088E /* GADMAdapterUnitySingleton.h */, 7DA782631DDA681E000C088E /* GADMAdapterUnitySingleton.m */, 7DA782641DDA681E000C088E /* GADMAdapterUnityWeakReference.h */, @@ -177,7 +174,6 @@ 7DA782681DDA681E000C088E /* UnityAdapter.h in Headers */, 7DA782701DDA681E000C088E /* GADMAdapterUnityWeakReference.h in Headers */, 7DA7826E1DDA681E000C088E /* GADMAdapterUnitySingleton.h in Headers */, - 7DA7826D1DDA681E000C088E /* GADMAdapterUnityProtocol.h in Headers */, 7DA7826C1DDA681E000C088E /* GADMAdapterUnityConstants.h in Headers */, 7DA782721DDA681E000C088E /* GADUnityError.h in Headers */, 7DA7826A1DDA681E000C088E /* GADMAdapterUnity.h in Headers */, diff --git a/adapters/Unity/UnityAdapter/GADMAdapterUnity.h b/adapters/Unity/UnityAdapter/GADMAdapterUnity.h index 0301af6b7..15667ef39 100644 --- a/adapters/Unity/UnityAdapter/GADMAdapterUnity.h +++ b/adapters/Unity/UnityAdapter/GADMAdapterUnity.h @@ -16,11 +16,8 @@ @import GoogleMobileAds; @import UnityAds; -#import "GADMAdapterUnityProtocol.h" - /// Adapter for communicating with the Unity Ads Network to fetch reward-based video ads and /// interstitial ads. -@interface GADMAdapterUnity : NSObject +@interface GADMAdapterUnity : NSObject @end diff --git a/adapters/Unity/UnityAdapter/GADMAdapterUnity.m b/adapters/Unity/UnityAdapter/GADMAdapterUnity.m index 5d11b29d7..690a940a5 100644 --- a/adapters/Unity/UnityAdapter/GADMAdapterUnity.m +++ b/adapters/Unity/UnityAdapter/GADMAdapterUnity.m @@ -18,7 +18,7 @@ #import "GADMAdapterUnitySingleton.h" #import "GADUnityError.h" -@interface GADMAdapterUnity () { +@interface GADMAdapterUnity () { /// Connector from Google Mobile Ads SDK to receive ad configurations. __weak id _rewardBasedVideoAdConnector; @@ -28,8 +28,9 @@ @interface GADMAdapterUnity () { /// Placement ID of Unity Ads network. NSString *_placementID; - /// YES if the adapter is loading. - BOOL _isLoading; + UADSInterstitialAd* _interstitialAd; + UADSBannerAd* _bannerAd; + UADSRewardedVideoAd* _rewardedVideoAd; } @end @@ -70,9 +71,7 @@ - (void)setUp { [strongConnector adapter:self didFailToSetUpRewardBasedVideoAdWithError:error]; return; } - BOOL isConfigured = - [[GADMAdapterUnitySingleton sharedInstance] configureRewardBasedVideoAdWithGameID:gameID - delegate:self]; + BOOL isConfigured = [[GADMAdapterUnitySingleton sharedInstance] configureWithGameID:gameID]; if (isConfigured) { [strongConnector adapterDidSetUpRewardBasedVideoAd:self]; } else { @@ -85,21 +84,22 @@ - (void)setUp { } - (void)requestRewardBasedVideoAd { - _isLoading = YES; - [[GADMAdapterUnitySingleton sharedInstance] requestRewardBasedVideoAdWithDelegate:self]; + if (_rewardedVideoAd) { + if ([_rewardedVideoAd canShow]) { + id strongRewardedConnector = _rewardBasedVideoAdConnector; + [strongRewardedConnector adapterDidReceiveRewardBasedVideoAd:self]; + } + } else { + _rewardedVideoAd = [[UADSRewardedVideoAd alloc] initWithPlacementId:_placementID]; + _rewardedVideoAd.delegate = self; + [_rewardedVideoAd load]; + } } - (void)presentRewardBasedVideoAdWithRootViewController:(UIViewController *)viewController { - // We will send adapterDidOpenRewardBasedVideoAd callback before presenting the unity ad because - // the ad has already loaded. - [_rewardBasedVideoAdConnector adapterDidOpenRewardBasedVideoAd:self]; - [[GADMAdapterUnitySingleton sharedInstance] - presentRewardBasedVideoAdForViewController:viewController - delegate:self]; -} - -- (void)stopBeingDelegate { - [[GADMAdapterUnitySingleton sharedInstance] stopTrackingDelegate:self]; + if ([_rewardedVideoAd canShow]) { + [_rewardedVideoAd showFromViewController:viewController]; + } } #pragma mark Interstitial Methods @@ -127,18 +127,20 @@ - (void)getInterstitial { [strongConnector adapter:self didFailAd:error]; return; } - _isLoading = YES; - [[GADMAdapterUnitySingleton sharedInstance] configureInterstitialAdWithGameID:gameID - delegate:self]; + [[GADMAdapterUnitySingleton sharedInstance] configureWithGameID:gameID]; + + if (!_interstitialAd) { + _interstitialAd = [[UADSInterstitialAd alloc] initWithPlacementId:_placementID]; + _interstitialAd.delegate = self; + [_interstitialAd load]; + } } - (void)presentInterstitialFromRootViewController:(UIViewController *)rootViewController { - // We will send adapterWillPresentInterstitial callback before presenting unity ad because the ad - // has already loaded. - [_networkConnector adapterWillPresentInterstitial:self]; - [[GADMAdapterUnitySingleton sharedInstance] - presentInterstitialAdForViewController:rootViewController - delegate:self]; + if ([_interstitialAd canShow]) { + [_networkConnector adapterWillPresentInterstitial:self]; + [_interstitialAd showFromViewController:rootViewController]; + } } #pragma mark Banner Methods @@ -154,151 +156,107 @@ - (void)getBannerWithSize:(GADAdSize)adSize { [strongNetworkConnector adapter:self didFailAd:error]; return; } - [[GADMAdapterUnitySingleton sharedInstance] presentBannerAd:gameID delegate:self]; + [[GADMAdapterUnitySingleton sharedInstance] configureWithGameID:gameID]; + + if (!_bannerAd) { + _bannerAd = [[UADSBannerAd alloc] initWithPlacementId:_placementID]; + _bannerAd.delegate = self; + [_bannerAd load]; + } } - (BOOL)isBannerAnimationOK:(GADMBannerAnimationType)animType { return YES; } -#pragma mark GADMAdapterUnityDataProvider Methods +#pragma mark - UADSInterstitialAdDelegate -- (NSString *)getPlacementID { - return _placementID; +-(void)interstitialAdDidLoad:(UADSInterstitialAd *)interstitialAd { + [_networkConnector adapterDidReceiveInterstitial:self]; } - -#pragma mark - Unity Delegate Methods - -- (void)unityAdsPlacementStateChanged:(NSString *)placementId - oldState:(UnityAdsPlacementState)oldState - newState:(UnityAdsPlacementState)newState { - // This callback is not forwarded to the adapter by the GADMAdapterUnitySingleton and the adapter - // should use the unityAdsReady: and unityAdsDidError: callbacks to forward Unity Ads SDK state to - // Google Mobile Ads SDK. +-(void)interstitialAdDidFailToLoad:(UADSInterstitialAd *)interstitialAd error:(NSError *)error { + [_networkConnector adapter:self didFailAd:GADUnityErrorWithDescription([error description])]; } - -- (void)unityAdsDidFinish:(NSString *)placementID withFinishState:(UnityAdsFinishState)state { - id strongNetworkConnector = _networkConnector; - id strongRewardedConnector = _rewardBasedVideoAdConnector; - if (strongNetworkConnector) { - [strongNetworkConnector adapterWillDismissInterstitial:self]; - [strongNetworkConnector adapterDidDismissInterstitial:self]; - } else if (strongRewardedConnector) { - if (state == kUnityAdsFinishStateCompleted) { - [strongRewardedConnector adapterDidCompletePlayingRewardBasedVideoAd:self]; - // Unity Ads doesn't provide a way to set the reward on their front-end. Default to a reward - // amount of 1. Publishers using this adapter should override the reward on the AdMob - // front-end. - GADAdReward *reward = - [[GADAdReward alloc] initWithRewardType:@"" rewardAmount:[NSDecimalNumber one]]; - [strongRewardedConnector adapter:self didRewardUserWithReward:reward]; - } - [strongRewardedConnector adapterDidCloseRewardBasedVideoAd:self]; - } +-(void)interstitialAdDidOpen:(UADSInterstitialAd *)interstitialAd { } -- (void)unityAdsDidStart:(NSString *)placementID { - id strongRewardedConnector = _rewardBasedVideoAdConnector; - if (strongRewardedConnector) { - [strongRewardedConnector adapterDidStartPlayingRewardBasedVideoAd:self]; - } +-(void)interstitialAdDidClick:(UADSInterstitialAd *)interstitialAd { + [_networkConnector adapterDidGetAdClick:self]; +} +-(void)interstitialAdDidLeaveApplication:(UADSInterstitialAd *)interstitialAd { + [_networkConnector adapterWillLeaveApplication:self]; +} +-(void)interstitialAdDidInvalidate:(UADSInterstitialAd *)interstitialAd { } -- (void)unityAdsReady:(NSString *)placementID { - id strongNetworkConnector = _networkConnector; - if (!_isLoading) { - return; - } - - if (strongNetworkConnector) { - [strongNetworkConnector adapterDidReceiveInterstitial:self]; - } else { - [strongRewardedConnector adapterDidReceiveRewardBasedVideoAd:self]; - } - _isLoading = NO; +-(void)interstitialAdDidClose:(UADSInterstitialAd *)interstitialAd finishState:(UnityAdsFinishState)finishState { + [_networkConnector adapterWillDismissInterstitial:self]; + [_networkConnector adapterDidDismissInterstitial:self]; + _interstitialAd = nil; } -- (void)unityAdsDidClick:(NSString *)placementID { - id strongNetworkConnector = _networkConnector; - id strongRewardedConnector = _rewardBasedVideoAdConnector; - // The Unity Ads SDK doesn't provide an event for leaving the application, so the adapter assumes - // that a click event indicates the user is leaving the application for a browser or deeplink, and - // notifies the Google Mobile Ads SDK accordingly. - if (strongNetworkConnector) { - [strongNetworkConnector adapterDidGetAdClick:self]; - [strongNetworkConnector adapterWillLeaveApplication:self]; - } else { - [strongRewardedConnector adapterDidGetAdClick:self]; - [strongRewardedConnector adapterWillLeaveApplication:self]; - } +#pragma mark - UADSRewardedVideoAdDelegate + +-(void)rewardedVideoAdDidLoad:(UADSRewardedVideoAd *)rewardedVideoAd { + [_rewardBasedVideoAdConnector adapterDidReceiveRewardBasedVideoAd:self]; +} +-(void)rewardedVideoAdDidFailToLoad:(UADSRewardedVideoAd *)rewardedVideoAd exception:(NSException *)exception { + [_rewardBasedVideoAdConnector adapter:self didFailToLoadRewardBasedVideoAdwithError:GADUnityErrorWithDescription([exception description])]; +} +-(void)rewardedVideoAdDidOpen:(UADSRewardedVideoAd *)rewardedVideoAd { + [_rewardBasedVideoAdConnector adapterDidOpenRewardBasedVideoAd:self]; +} +-(void)rewardedVideoAdDidStart:(UADSRewardedVideoAd *)rewardedVideoAd { + [_rewardBasedVideoAdConnector adapterDidStartPlayingRewardBasedVideoAd:self]; } -- (void)unityAdsDidError:(UnityAdsError)error withMessage:(NSString *)message { - id strongNetworkConnector = _networkConnector; - id strongRewardedConnector = _rewardBasedVideoAdConnector; - if (!_isLoading) { - // Unity Ads show error will only happen after the ad has been loaded. So, we will send - // dismiss/close callbacks. - if (error == kUnityAdsErrorShowError) { - if (strongNetworkConnector) { - [strongNetworkConnector adapterWillDismissInterstitial:self]; - [strongNetworkConnector adapterDidDismissInterstitial:self]; - } else { - [strongRewardedConnector adapterDidCloseRewardBasedVideoAd:self]; - } - } - return; - } +-(void)rewardedVideoAdDidClick:(UADSRewardedVideoAd *)rewardedVideoAd { + [_rewardBasedVideoAdConnector adapterDidGetAdClick:self]; +} +-(void)rewardedVideoAdDidLeaveApplication:(UADSRewardedVideoAd *)rewardedVideoAd { + [_rewardBasedVideoAdConnector adapterWillLeaveApplication:self]; +} +-(void)rewardedVideoAdDidReward:(UADSRewardedVideoAd *)rewardedVideoAd { + GADAdReward *reward = [[GADAdReward alloc] initWithRewardType:@"" rewardAmount:[NSDecimalNumber one]]; + [_rewardBasedVideoAdConnector adapter:self didRewardUserWithReward:reward]; +} +-(void)rewardedVideoAdDidClose:(UADSRewardedVideoAd *)rewardedVideoAd finishState:(UnityAdsFinishState)finishState { + [_rewardBasedVideoAdConnector adapterDidCloseRewardBasedVideoAd:self]; +} +-(void)rewardedVideoAdDidInvalidate:(UADSRewardedVideoAd *)rewardedVideoAd { - NSError *errorWithDescription = GADUnityErrorWithDescription(message); - if (strongNetworkConnector) { - [strongNetworkConnector adapter:self didFailAd:errorWithDescription]; - } else if (strongRewardedConnector) { - [strongRewardedConnector adapter:self - didFailToLoadRewardBasedVideoAdwithError:errorWithDescription]; - } - _isLoading = NO; +} +-(void)rewardedVideoAdDidFinish:(UADSRewardedVideoAd *)rewardedVideoAd { + [_rewardBasedVideoAdConnector adapterDidCompletePlayingRewardBasedVideoAd:self]; } -#pragma mark - Unity Banner Delegate Methods +#pragma mark - UADSBannerAdDelegate -- (void)unityAdsBannerDidClick:(nonnull NSString *)placementId { - id strongNetworkConnector = _networkConnector; - if (strongNetworkConnector) { - [strongNetworkConnector adapterDidGetAdClick:self]; - [strongNetworkConnector adapterWillLeaveApplication:self]; - } +-(void)bannerAdDidLoad:(UADSBannerAd *)bannerAd { + [_networkConnector adapter:self didReceiveAdView:[bannerAd getView]]; } -- (void)unityAdsBannerDidError:(nonnull NSString *)message { - id strongNetworkConnector = _networkConnector; - if (strongNetworkConnector){ - NSError *error = GADUnityErrorWithDescription(@"Unity Ads Banner internal error"); - [strongNetworkConnector adapter:self didFailAd:error]; - } +-(void)bannerAdDidFailToLoad:(UADSBannerAd *)bannerAd error:(NSError *)error { + [_networkConnector adapter:self didFailAd:GADUnityErrorWithDescription([error description])]; } -- (void)unityAdsBannerDidHide:(nonnull NSString *)placementId { - NSLog(@"Unity Ads Banner did hide."); +-(void)bannerAdDidOpen:(UADSBannerAd *)bannerAd { } -- (void)unityAdsBannerDidLoad:(nonnull NSString *)placementId view:(nonnull UIView *)view { - id strongNetworkConnector = _networkConnector; - if(strongNetworkConnector){ - [strongNetworkConnector adapter:self didReceiveAdView:view]; - }else{ - NSLog(@"ERROR: Network connector for UnityAds banner adapter not found."); - } +-(void)bannerAdDidClose:(UADSBannerAd *)bannerAd { } -- (void)unityAdsBannerDidShow:(nonnull NSString *)placementId { - NSLog(@"Unity Ads Banner is showing."); +-(void)bannerAdDidClick:(UADSBannerAd *)bannerAd { + [_networkConnector adapterDidGetAdClick:self]; +} +-(void)bannerAdDidLeaveApplication:(UADSBannerAd *)bannerAd { + [_networkConnector adapterWillLeaveApplication:self]; } -- (void)unityAdsBannerDidUnload:(nonnull NSString *)placementId { - NSLog(@"Unity Ads Banner has unloaded."); +-(void)bannerAdDidInvalidate:(UADSBannerAd *)bannerAd { } + @end diff --git a/adapters/Unity/UnityAdapter/GADMAdapterUnityProtocol.h b/adapters/Unity/UnityAdapter/GADMAdapterUnityProtocol.h deleted file mode 100644 index 31540311f..000000000 --- a/adapters/Unity/UnityAdapter/GADMAdapterUnityProtocol.h +++ /dev/null @@ -1,24 +0,0 @@ -// Copyright 2016 Google Inc. -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. - -@import Foundation; - -/// The purpose of the GADMAdapterUnityDataProvider protocol is to allow the singleton to interact -/// with the adapter. -@protocol GADMAdapterUnityDataProvider - -/// Returns placement ID for either reward-based video ad or interstitial ad of Unity Ads network. -- (NSString *)getPlacementID; - -@end diff --git a/adapters/Unity/UnityAdapter/GADMAdapterUnitySingleton.h b/adapters/Unity/UnityAdapter/GADMAdapterUnitySingleton.h index b780cf899..2354409f7 100644 --- a/adapters/Unity/UnityAdapter/GADMAdapterUnitySingleton.h +++ b/adapters/Unity/UnityAdapter/GADMAdapterUnitySingleton.h @@ -16,48 +16,13 @@ @import GoogleMobileAds; @import UnityAds; -#import "GADMAdapterUnityProtocol.h" - @interface GADMAdapterUnitySingleton : NSObject /// Shared instance. + (instancetype)sharedInstance; -/// Configures a reward-based video ad with provided |gameID| and |adapterDelegate| and returns +/// Configures a reward-based video ad with provided |gameID| and returns /// YES if successful; otherwise returns NO. -- (BOOL)configureRewardBasedVideoAdWithGameID:(NSString *)gameID - delegate: - (id) - adapterDelegate; - -/// Requests a reward-based video ad with |adapterDelegate|. -- (void)requestRewardBasedVideoAdWithDelegate: - (id)adapterDelegate; - -/// Presents a reward-based video ad for |viewController| with |adapterDelegate|. -- (void)presentRewardBasedVideoAdForViewController:(UIViewController *)viewController - delegate: - (id) - adapterDelegate; - -/// Configures an interstitial ad with provided |gameID| and |adapterDelegate|. -- (void)configureInterstitialAdWithGameID:(NSString *)gameID - delegate: - (id) - adapterDelegate; - -/// Presents an interstitial ad for |viewController| with |adapterDelegate|. -- (void)presentInterstitialAdForViewController:(UIViewController *)viewController - delegate: - (id) - adapterDelegate; - -/// Presents a banner ad for |gameID| with |adapterDelegate| -- (void)presentBannerAd:(NSString *)gameID - delegate: (id) adapterDelegate; - -/// Tells the adapter to remove itself as a |adapterDelegate|. -- (void)stopTrackingDelegate:(id) - adapterDelegate; +- (BOOL)configureWithGameID:(NSString *)gameID; @end diff --git a/adapters/Unity/UnityAdapter/GADMAdapterUnitySingleton.m b/adapters/Unity/UnityAdapter/GADMAdapterUnitySingleton.m index 375f736e6..375a026e2 100644 --- a/adapters/Unity/UnityAdapter/GADMAdapterUnitySingleton.m +++ b/adapters/Unity/UnityAdapter/GADMAdapterUnitySingleton.m @@ -17,25 +17,8 @@ #import "GADMAdapterUnityConstants.h" #import "GADMAdapterUnityWeakReference.h" -@interface GADMAdapterUnitySingleton () { - /// Array to hold all adapter delegates. - NSMutableArray *_adapterDelegates; - - /// Connector from unity adapter to send Unity callbacks. - __weak id _currentShowingUnityDelegate; - - /// Connector from unity adapter to send Banner callbacks - __weak id _currentBannerDelegate; - -} - -@end - @implementation GADMAdapterUnitySingleton -NSString* _bannerPlacementID = nil; -bool _bannerRequested = false; - + (instancetype)sharedInstance { static GADMAdapterUnitySingleton *sharedManager = nil; static dispatch_once_t onceToken; @@ -45,244 +28,15 @@ + (instancetype)sharedInstance { return sharedManager; } -- (id)init { - self = [super init]; - if (self) { - _adapterDelegates = [[NSMutableArray alloc] init]; - } - return self; -} - -- (void)initializeWithGameID:(NSString *)gameID { - // Metadata needed by Unity Ads SDK before initialization. - UADSMediationMetaData *mediationMetaData = [[UADSMediationMetaData alloc] init]; - [mediationMetaData setName:GADMAdapterUnityMediationNetworkName]; - [mediationMetaData setVersion:GADMAdapterUnityVersion]; - [mediationMetaData commit]; - // Initializing Unity Ads with |gameID|. - [UnityAds initialize:gameID delegate:self]; -} - -- (void)addAdapterDelegate: - (id)adapterDelegate { - GADMAdapterUnityWeakReference *delegateReference = - [[GADMAdapterUnityWeakReference alloc] initWithObject:adapterDelegate]; - // Removes duplicate delegate references. - [self removeAdapterDelegate:delegateReference]; - [_adapterDelegates addObject:delegateReference]; -} - -- (void)removeAdapterDelegate:(GADMAdapterUnityWeakReference *)adapterDelegate { - // Removes duplicate mediation adapter delegate references. - NSMutableArray *delegatesToRemove = [NSMutableArray array]; - [_adapterDelegates - enumerateObjectsUsingBlock:^(id _Nonnull obj, NSUInteger idx, BOOL *_Nonnull stop) { - GADMAdapterUnityWeakReference *weakReference = obj; - if ([weakReference isEqual:adapterDelegate]) { - [delegatesToRemove addObject:obj]; - } - }]; - [_adapterDelegates removeObjectsInArray:delegatesToRemove]; -} - -#pragma mark - Rewardbased video ad methods - -- (BOOL)configureRewardBasedVideoAdWithGameID:(NSString *)gameID - delegate: - (id) - adapterDelegate { - if ([UnityAds isSupported]) { - if (![UnityAds isInitialized]) { - // Add delegate reference in adapterDelegate list only if Unity Ads is not initialized. - [self addAdapterDelegate:adapterDelegate]; - [self initializeWithGameID:gameID]; - } - return YES; - } - return NO; -} - -- (void)requestRewardBasedVideoAdWithDelegate: - (id)adapterDelegate { - if ([UnityAds isInitialized]) { - NSString *placementID = [adapterDelegate getPlacementID]; - if ([UnityAds isReady:placementID]) { - [adapterDelegate unityAdsReady:placementID]; - } else { - NSString *description = - [[NSString alloc] initWithFormat:@"%@ failed to receive reward based video ad.", - NSStringFromClass([UnityAds class])]; - [adapterDelegate unityAdsDidError:kUnityAdsErrorShowError withMessage:description]; - } - } -} - -- (void)presentRewardBasedVideoAdForViewController:(UIViewController *)viewController - delegate: - (id) - adapterDelegate { - _currentShowingUnityDelegate = adapterDelegate; - // The Unity Ads show method checks whether an ad is available. - [UnityAds show:viewController placementId:[adapterDelegate getPlacementID]]; -} - -#pragma mark - Interstitial ad methods - -- (void)configureInterstitialAdWithGameID:(NSString *)gameID - delegate: - (id) - adapterDelegate { - if ([UnityAds isSupported]) { - if ([UnityAds isInitialized]) { - NSString *placementID = [adapterDelegate getPlacementID]; - if ([UnityAds isReady:placementID]) { - [adapterDelegate unityAdsReady:placementID]; - } else { - NSString *description = - [[NSString alloc] initWithFormat:@"%@ failed to receive interstitial ad.", - NSStringFromClass([UnityAds class])]; - [adapterDelegate unityAdsDidError:kUnityAdsErrorShowError withMessage:description]; - } - } else { - // Add delegate reference in adapterDelegate list only if Unity Ads is not initialized. - [self addAdapterDelegate:adapterDelegate]; - [self initializeWithGameID:gameID]; - } - } else { - NSString *description = - [[NSString alloc] initWithFormat:@"%@ is not supported for this device.", - NSStringFromClass([UnityAds class])]; - [adapterDelegate unityAdsDidError:kUnityAdsErrorNotInitialized withMessage:description]; - } -} - -- (void)presentInterstitialAdForViewController:(UIViewController *)viewController - delegate: - (id) - adapterDelegate { - _currentShowingUnityDelegate = adapterDelegate; - // The Unity Ads show method checks whether an ad is available. - [UnityAds show:viewController placementId:[adapterDelegate getPlacementID]]; -} - -#pragma mark - Banner ad methods - -- (void)presentBannerAd:(NSString *)gameID - delegate: -(id) adapterDelegate { - _currentBannerDelegate = adapterDelegate; - +-(BOOL)configureWithGameID:(NSString *)gameID { if ([UnityAds isSupported]) { - NSString *placementID = [_currentBannerDelegate getPlacementID]; - if(placementID == nil){ - NSString *description = - [[NSString alloc] initWithFormat:@"Tried to show banners with a nil placement ID"]; - [_currentBannerDelegate unityAdsBannerDidError:description]; - return; - }else{ - _bannerPlacementID = placementID; - } - - if (![UnityAds isInitialized]) { - [self initializeWithGameID:gameID]; - _bannerRequested = true; - }else{ - [UnityAdsBanner setDelegate:self]; - [UnityAdsBanner loadBanner:_bannerPlacementID]; - } - } else { - NSString *description = - [[NSString alloc] initWithFormat:@"Unity Ads is not supported for this device."]; - [_currentBannerDelegate unityAdsBannerDidError:description]; + if (![UnityAds isInitialized]) { + [UnityAds initialize:gameID]; + return YES; + } } + return NO; } -#pragma mark - Unity Banner Delegate Methods - --(void)unityAdsBannerDidLoad:(NSString *)placementId view:(UIView *)view { - [_currentBannerDelegate unityAdsBannerDidLoad:_bannerPlacementID view:view]; -} - --(void)unityAdsBannerDidUnload:(NSString *)placementId { - [_currentBannerDelegate unityAdsBannerDidUnload:_bannerPlacementID]; -} - --(void)unityAdsBannerDidShow:(NSString *)placementId { - [_currentBannerDelegate unityAdsBannerDidShow:_bannerPlacementID]; -} - --(void)unityAdsBannerDidHide:(NSString *)placementId { - [_currentBannerDelegate unityAdsBannerDidHide:_bannerPlacementID]; -} - --(void)unityAdsBannerDidClick:(NSString *)placementId { - [_currentBannerDelegate unityAdsBannerDidClick:_bannerPlacementID]; -} - --(void)unityAdsBannerDidError:(NSString *)message { - NSString *description = - [[NSString alloc] initWithFormat:@"Internal Unity Ads banner error"]; - [_currentBannerDelegate unityAdsBannerDidError:description]; -} - -#pragma mark - Unity Delegate Methods - -- (void)unityAdsPlacementStateChanged:(NSString *)placementId - oldState:(UnityAdsPlacementState)oldState - newState:(UnityAdsPlacementState)newState { - // The unityAdsReady: and unityAdsDidError: callback methods are used to forward Unity Ads SDK - // states to the adapters. No need to forward this callback to the adapters. -} - -- (void)unityAdsDidFinish:(NSString *)placementID withFinishState:(UnityAdsFinishState)state { - [_currentShowingUnityDelegate unityAdsDidFinish:placementID withFinishState:state]; -} - -- (void)unityAdsDidStart:(NSString *)placementID { - [_currentShowingUnityDelegate unityAdsDidStart:placementID]; -} - -- (void)unityAdsReady:(NSString *)placementID { - NSMutableArray *delegatesToRemove = [NSMutableArray array]; - [_adapterDelegates - enumerateObjectsUsingBlock:^(id _Nonnull obj, NSUInteger idx, BOOL *_Nonnull stop) { - GADMAdapterUnityWeakReference *weakReference = obj; - if ([[(id)weakReference.weakObject getPlacementID] - isEqualToString:placementID]) { - [(id)weakReference.weakObject unityAdsReady:placementID]; - [delegatesToRemove addObject:obj]; - } - }]; - [_adapterDelegates removeObjectsInArray:delegatesToRemove]; -} - -- (void)unityAdsDidClick:(NSString *)placementID { - [_currentShowingUnityDelegate unityAdsDidClick:placementID]; -} - -- (void)unityAdsDidError:(UnityAdsError)error withMessage:(NSString *)message { - // If the error is of type show, we will not have it's delegate reference in our adapterDelegate - // list. Delegate instances are being removed when we get unityAdsReady callback. - if (error == kUnityAdsErrorShowError) { - [_currentShowingUnityDelegate unityAdsDidError:error withMessage:message]; - return; - } - NSMutableArray *delegatesToRemove = [NSMutableArray array]; - [_adapterDelegates - enumerateObjectsUsingBlock:^(id _Nonnull obj, NSUInteger idx, BOOL *_Nonnull stop) { - GADMAdapterUnityWeakReference *weakReference = obj; - [(id)weakReference.weakObject unityAdsDidError:error - withMessage:message]; - [delegatesToRemove addObject:obj]; - }]; - [_adapterDelegates removeObjectsInArray:delegatesToRemove]; -} - -- (void)stopTrackingDelegate:(id) - adapterDelegate { - GADMAdapterUnityWeakReference *delegateReference = - [[GADMAdapterUnityWeakReference alloc] initWithObject:adapterDelegate]; - [self removeAdapterDelegate:delegateReference]; -} @end From 18418674306a79a40c33a4f32ee05b9d95f1d3b5 Mon Sep 17 00:00:00 2001 From: Tina Han Date: Thu, 25 Apr 2019 23:21:12 -0700 Subject: [PATCH 3/6] Added stopBeingDelegate in Adapter --- adapters/Unity/UnityAdapter/GADMAdapterUnity.m | 3 +++ 1 file changed, 3 insertions(+) diff --git a/adapters/Unity/UnityAdapter/GADMAdapterUnity.m b/adapters/Unity/UnityAdapter/GADMAdapterUnity.m index 690a940a5..2c015dce7 100644 --- a/adapters/Unity/UnityAdapter/GADMAdapterUnity.m +++ b/adapters/Unity/UnityAdapter/GADMAdapterUnity.m @@ -143,6 +143,9 @@ - (void)presentInterstitialFromRootViewController:(UIViewController *)rootViewCo } } +- (void)stopBeingDelegate { +} + #pragma mark Banner Methods - (void)getBannerWithSize:(GADAdSize)adSize { From 535acdbfb7737529184cafea70d1864db672bd97 Mon Sep 17 00:00:00 2001 From: Tina Han Date: Thu, 25 Apr 2019 23:48:55 -0700 Subject: [PATCH 4/6] Fix configure check bug and add more config checks --- .../Unity/UnityAdapter/GADMAdapterUnity.m | 38 +++++++++++++------ .../UnityAdapter/GADMAdapterUnitySingleton.m | 2 +- 2 files changed, 27 insertions(+), 13 deletions(-) diff --git a/adapters/Unity/UnityAdapter/GADMAdapterUnity.m b/adapters/Unity/UnityAdapter/GADMAdapterUnity.m index 690a940a5..d9f9658a6 100644 --- a/adapters/Unity/UnityAdapter/GADMAdapterUnity.m +++ b/adapters/Unity/UnityAdapter/GADMAdapterUnity.m @@ -127,12 +127,19 @@ - (void)getInterstitial { [strongConnector adapter:self didFailAd:error]; return; } - [[GADMAdapterUnitySingleton sharedInstance] configureWithGameID:gameID]; - - if (!_interstitialAd) { - _interstitialAd = [[UADSInterstitialAd alloc] initWithPlacementId:_placementID]; - _interstitialAd.delegate = self; - [_interstitialAd load]; + BOOL isConfigured = [[GADMAdapterUnitySingleton sharedInstance] configureWithGameID:gameID]; + if (!isConfigured) { + NSString *description = + [[NSString alloc] initWithFormat:@"%@ is not supported for this device.", + NSStringFromClass([UnityAds class])]; + NSError *error = GADUnityErrorWithDescription(description); + [strongConnector adapter:self didFailAd:error]; + } else { + if (!_interstitialAd) { + _interstitialAd = [[UADSInterstitialAd alloc] initWithPlacementId:_placementID]; + _interstitialAd.delegate = self; + [_interstitialAd load]; + } } } @@ -156,12 +163,19 @@ - (void)getBannerWithSize:(GADAdSize)adSize { [strongNetworkConnector adapter:self didFailAd:error]; return; } - [[GADMAdapterUnitySingleton sharedInstance] configureWithGameID:gameID]; - - if (!_bannerAd) { - _bannerAd = [[UADSBannerAd alloc] initWithPlacementId:_placementID]; - _bannerAd.delegate = self; - [_bannerAd load]; + BOOL isConfigured = [[GADMAdapterUnitySingleton sharedInstance] configureWithGameID:gameID]; + if (!isConfigured) { + NSString *description = + [[NSString alloc] initWithFormat:@"%@ is not supported for this device.", + NSStringFromClass([UnityAds class])]; + NSError *error = GADUnityErrorWithDescription(description); + [strongNetworkConnector adapter:self didFailAd:error]; + } else { + if (!_bannerAd) { + _bannerAd = [[UADSBannerAd alloc] initWithPlacementId:_placementID]; + _bannerAd.delegate = self; + [_bannerAd load]; + } } } diff --git a/adapters/Unity/UnityAdapter/GADMAdapterUnitySingleton.m b/adapters/Unity/UnityAdapter/GADMAdapterUnitySingleton.m index 375a026e2..85e81a161 100644 --- a/adapters/Unity/UnityAdapter/GADMAdapterUnitySingleton.m +++ b/adapters/Unity/UnityAdapter/GADMAdapterUnitySingleton.m @@ -32,8 +32,8 @@ -(BOOL)configureWithGameID:(NSString *)gameID { if ([UnityAds isSupported]) { if (![UnityAds isInitialized]) { [UnityAds initialize:gameID]; - return YES; } + return YES; } return NO; } From ed2b59f21f6c4b35ba9ff1e45fb024a189ebb147 Mon Sep 17 00:00:00 2001 From: Tina Han Date: Fri, 26 Apr 2019 11:22:01 -0700 Subject: [PATCH 5/6] Remove canShow check before every show --- adapters/Unity/UnityAdapter/GADMAdapterUnity.m | 10 +++------- 1 file changed, 3 insertions(+), 7 deletions(-) diff --git a/adapters/Unity/UnityAdapter/GADMAdapterUnity.m b/adapters/Unity/UnityAdapter/GADMAdapterUnity.m index 690a940a5..4029ecded 100644 --- a/adapters/Unity/UnityAdapter/GADMAdapterUnity.m +++ b/adapters/Unity/UnityAdapter/GADMAdapterUnity.m @@ -97,9 +97,7 @@ - (void)requestRewardBasedVideoAd { } - (void)presentRewardBasedVideoAdWithRootViewController:(UIViewController *)viewController { - if ([_rewardedVideoAd canShow]) { - [_rewardedVideoAd showFromViewController:viewController]; - } + [_rewardedVideoAd showFromViewController:viewController]; } #pragma mark Interstitial Methods @@ -137,10 +135,8 @@ - (void)getInterstitial { } - (void)presentInterstitialFromRootViewController:(UIViewController *)rootViewController { - if ([_interstitialAd canShow]) { - [_networkConnector adapterWillPresentInterstitial:self]; - [_interstitialAd showFromViewController:rootViewController]; - } + [_networkConnector adapterWillPresentInterstitial:self]; + [_interstitialAd showFromViewController:rootViewController]; } #pragma mark Banner Methods From bed07005535c0a08b8e1fd90683760b1352c27c4 Mon Sep 17 00:00:00 2001 From: Tina Han Date: Fri, 26 Apr 2019 12:47:35 -0700 Subject: [PATCH 6/6] Deallocate rewardedAd object when it gets closed --- adapters/Unity/UnityAdapter/GADMAdapterUnity.m | 10 +++------- 1 file changed, 3 insertions(+), 7 deletions(-) diff --git a/adapters/Unity/UnityAdapter/GADMAdapterUnity.m b/adapters/Unity/UnityAdapter/GADMAdapterUnity.m index 690a940a5..bad66281b 100644 --- a/adapters/Unity/UnityAdapter/GADMAdapterUnity.m +++ b/adapters/Unity/UnityAdapter/GADMAdapterUnity.m @@ -84,12 +84,7 @@ - (void)setUp { } - (void)requestRewardBasedVideoAd { - if (_rewardedVideoAd) { - if ([_rewardedVideoAd canShow]) { - id strongRewardedConnector = _rewardBasedVideoAdConnector; - [strongRewardedConnector adapterDidReceiveRewardBasedVideoAd:self]; - } - } else { + if (_rewardedVideoAd == nil) { _rewardedVideoAd = [[UADSRewardedVideoAd alloc] initWithPlacementId:_placementID]; _rewardedVideoAd.delegate = self; [_rewardedVideoAd load]; @@ -191,8 +186,8 @@ -(void)interstitialAdDidInvalidate:(UADSInterstitialAd *)interstitialAd { -(void)interstitialAdDidClose:(UADSInterstitialAd *)interstitialAd finishState:(UnityAdsFinishState)finishState { [_networkConnector adapterWillDismissInterstitial:self]; - [_networkConnector adapterDidDismissInterstitial:self]; _interstitialAd = nil; + [_networkConnector adapterDidDismissInterstitial:self]; } #pragma mark - UADSRewardedVideoAdDelegate @@ -222,6 +217,7 @@ -(void)rewardedVideoAdDidReward:(UADSRewardedVideoAd *)rewardedVideoAd { } -(void)rewardedVideoAdDidClose:(UADSRewardedVideoAd *)rewardedVideoAd finishState:(UnityAdsFinishState)finishState { [_rewardBasedVideoAdConnector adapterDidCloseRewardBasedVideoAd:self]; + _rewardedVideoAd = nil; } -(void)rewardedVideoAdDidInvalidate:(UADSRewardedVideoAd *)rewardedVideoAd {