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 b0a6a93a8..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 30a9cf9e8..450861bc6 100644 --- a/adapters/Unity/UnityAdapter/GADMAdapterUnity.m +++ b/adapters/Unity/UnityAdapter/GADMAdapterUnity.m @@ -18,18 +18,19 @@ #import "GADMAdapterUnitySingleton.h" #import "GADUnityError.h" -@interface GADMAdapterUnity () { +@interface GADMAdapterUnity () { /// Connector from Google Mobile Ads SDK to receive ad configurations. __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; - /// 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,23 +84,15 @@ - (void)setUp { } - (void)requestRewardBasedVideoAd { - _placementID = - [[[_rewardBasedVideoAdConnector credentials] objectForKey:GADMAdapterUnityPlacementID] copy]; - _isLoading = YES; - [[GADMAdapterUnitySingleton sharedInstance] requestRewardBasedVideoAdWithDelegate:self]; + if (_rewardedVideoAd == nil) { + _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]; + [_rewardedVideoAd showFromViewController:viewController]; } #pragma mark Interstitial Methods @@ -113,13 +104,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 = @@ -129,130 +120,152 @@ - (void)getInterstitial { [strongConnector adapter:self didFailAd:error]; return; } - _isLoading = YES; - [[GADMAdapterUnitySingleton sharedInstance] configureInterstitialAdWithGameID:gameID - delegate:self]; + 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]; + } + } } - (void)presentInterstitialFromRootViewController:(UIViewController *)rootViewController { - // We will send adapterWillPresentInterstitial callback before presenting unity ad because the ad - // has already loaded. - [_interstitialConnector adapterWillPresentInterstitial:self]; - [[GADMAdapterUnitySingleton sharedInstance] - presentInterstitialAdForViewController:rootViewController - delegate:self]; + [_networkConnector adapterWillPresentInterstitial:self]; + [_interstitialAd showFromViewController:rootViewController]; +} + +- (void)stopBeingDelegate { } #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; + } + 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]; + } + } } - (BOOL)isBannerAnimationOK:(GADMBannerAnimationType)animType { return YES; } -#pragma mark GADMAdapterUnityDataProvider Methods +#pragma mark - UADSInterstitialAdDelegate -- (NSString *)getPlacementID { - return _placementID; +-(void)interstitialAdDidLoad:(UADSInterstitialAd *)interstitialAd { + [_networkConnector adapterDidReceiveInterstitial:self]; +} +-(void)interstitialAdDidFailToLoad:(UADSInterstitialAd *)interstitialAd error:(NSError *)error { + [_networkConnector adapter:self didFailAd:GADUnityErrorWithDescription([error description])]; +} +-(void)interstitialAdDidOpen:(UADSInterstitialAd *)interstitialAd { } -#pragma mark - Unity Delegate Methods +-(void)interstitialAdDidClick:(UADSInterstitialAd *)interstitialAd { + [_networkConnector adapterDidGetAdClick:self]; +} +-(void)interstitialAdDidLeaveApplication:(UADSInterstitialAd *)interstitialAd { + [_networkConnector adapterWillLeaveApplication:self]; +} +-(void)interstitialAdDidInvalidate:(UADSInterstitialAd *)interstitialAd { +} -- (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)interstitialAdDidClose:(UADSInterstitialAd *)interstitialAd finishState:(UnityAdsFinishState)finishState { + [_networkConnector adapterWillDismissInterstitial:self]; + _interstitialAd = nil; + [_networkConnector adapterDidDismissInterstitial:self]; } -- (void)unityAdsDidFinish:(NSString *)placementID withFinishState:(UnityAdsFinishState)state { - id strongInterstitialConnector = _interstitialConnector; - id strongRewardedConnector = _rewardBasedVideoAdConnector; - if (strongInterstitialConnector) { - [strongInterstitialConnector adapterWillDismissInterstitial:self]; - [strongInterstitialConnector 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]; - } +#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)unityAdsDidStart:(NSString *)placementID { - id strongRewardedConnector = _rewardBasedVideoAdConnector; - if (strongRewardedConnector) { - [strongRewardedConnector adapterDidStartPlayingRewardBasedVideoAd:self]; - } +-(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]; + _rewardedVideoAd = nil; } +-(void)rewardedVideoAdDidInvalidate:(UADSRewardedVideoAd *)rewardedVideoAd { -- (void)unityAdsReady:(NSString *)placementID { - id strongInterstitialConnector = _interstitialConnector; - if (!_isLoading) { - return; - } +} +-(void)rewardedVideoAdDidFinish:(UADSRewardedVideoAd *)rewardedVideoAd { + [_rewardBasedVideoAdConnector adapterDidCompletePlayingRewardBasedVideoAd:self]; +} - if (strongInterstitialConnector) { - [strongInterstitialConnector adapterDidReceiveInterstitial:self]; - } else { - [_rewardBasedVideoAdConnector adapterDidReceiveRewardBasedVideoAd:self]; - } - _isLoading = NO; -} - -- (void)unityAdsDidClick:(NSString *)placementID { - id strongInterstitialConnector = _interstitialConnector; - 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]; - } else { - [strongRewardedConnector adapterDidGetAdClick:self]; - [strongRewardedConnector adapterWillLeaveApplication:self]; - } +#pragma mark - UADSBannerAdDelegate + +-(void)bannerAdDidLoad:(UADSBannerAd *)bannerAd { + [_networkConnector adapter:self didReceiveAdView:[bannerAd getView]]; } -- (void)unityAdsDidError:(UnityAdsError)error withMessage:(NSString *)message { - id strongInterstitialConnector = _interstitialConnector; - 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]; - } else { - [strongRewardedConnector adapterDidCloseRewardBasedVideoAd:self]; - } - } - return; - } +-(void)bannerAdDidFailToLoad:(UADSBannerAd *)bannerAd error:(NSError *)error { + [_networkConnector adapter:self didFailAd:GADUnityErrorWithDescription([error description])]; +} - NSError *errorWithDescription = GADUnityErrorWithDescription(message); - if (strongInterstitialConnector) { - [strongInterstitialConnector adapter:self didFailAd:errorWithDescription]; - } else if (strongRewardedConnector) { - [strongRewardedConnector adapter:self - didFailToLoadRewardBasedVideoAdwithError:errorWithDescription]; - } - _isLoading = NO; +-(void)bannerAdDidOpen:(UADSBannerAd *)bannerAd { +} + +-(void)bannerAdDidClose:(UADSBannerAd *)bannerAd { } +-(void)bannerAdDidClick:(UADSBannerAd *)bannerAd { + [_networkConnector adapterDidGetAdClick:self]; +} +-(void)bannerAdDidLeaveApplication:(UADSBannerAd *)bannerAd { + [_networkConnector adapterWillLeaveApplication:self]; +} + +-(void)bannerAdDidInvalidate:(UADSBannerAd *)bannerAd { +} + + @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/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 432770ea1..2354409f7 100644 --- a/adapters/Unity/UnityAdapter/GADMAdapterUnitySingleton.h +++ b/adapters/Unity/UnityAdapter/GADMAdapterUnitySingleton.h @@ -16,44 +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; - -/// 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 3e4a34304..85e81a161 100644 --- a/adapters/Unity/UnityAdapter/GADMAdapterUnitySingleton.m +++ b/adapters/Unity/UnityAdapter/GADMAdapterUnitySingleton.m @@ -17,16 +17,6 @@ #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; -} - -@end - @implementation GADMAdapterUnitySingleton + (instancetype)sharedInstance { @@ -38,184 +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]; +-(BOOL)configureWithGameID:(NSString *)gameID { + if ([UnityAds isSupported]) { + if (![UnityAds isInitialized]) { + [UnityAds initialize:gameID]; } - } else { - // Add delegate reference in adapterDelegate list only if Unity Ads is not initialized. - [self addAdapterDelegate:adapterDelegate]; - [self initializeWithGameID:gameID]; + return YES; } - } 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 - 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]; + return NO; } -- (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