Update singleton to use complete block
Update singleton to use complete block
MODIFY
adapters/Unity/UnityAdapter/GADMAdapterUnity.m
adapters/Unity/UnityAdapter/GADMAdapterUnity.m
return;
}
- [[GADMAdapterUnitySingleton sharedInstance] initializeWithGameID:_gameID];
+ UnitySingletonCompletion completeBlock = ^(UnityAdsError *error, NSString *message) {
+ if(!error) {
[UnityAds addDelegate:self];
- [UnityAds load:_placementID];
+ [UnityAds load:[self getPlacementID]];
+ } else {
+ id<GADMAdNetworkConnector> strongNetworkConnector = self->_networkConnector;
+ if (strongNetworkConnector) {
+ NSError *errorWithDescription = GADUnityErrorWithDescription(message);
+ [strongNetworkConnector adapter:self didFailAd:errorWithDescription];
+ }
+ }
+ };
+
+ [[GADMAdapterUnitySingleton sharedInstance] initializeWithGameID:_gameID
+ completeBlock:completeBlock];
}
- (void)presentInterstitialFromRootViewController:(UIViewController *)rootViewController {
- (void)unityAdsDidFinish:(NSString *)placementID withFinishState:(UnityAdsFinishState)state {
id<GADMAdNetworkConnector> strongNetworkConnector = _networkConnector;
if (strongNetworkConnector && [placementID isEqualToString:_placementID]) {
+ if (state == kUnityAdsFinishStateCompleted) {
+
+
+ } else if (state == kUnityAdsFinishStateError) {
+ [strongNetworkConnector adapterWillDismissInterstitial:self];
+ [strongNetworkConnector adapterDidDismissInterstitial:self];
+
+ } else if (state == kUnityAdsFinishStateSkipped) {
[strongNetworkConnector adapterWillDismissInterstitial:self];
[strongNetworkConnector adapterDidDismissInterstitial:self];
+
+ }
[UnityAds removeDelegate:self];
}
}
}
- (void)unityAdsDidError:(UnityAdsError)error withMessage:(NSString *)message {
- id<GADMAdNetworkConnector> strongNetworkConnector = _networkConnector;
- if (strongNetworkConnector) {
- NSError *errorWithDescription = GADUnityErrorWithDescription(message);
- [strongNetworkConnector adapter:self didFailAd:errorWithDescription];
- if (error == kUnityAdsErrorShowError) {
- [strongNetworkConnector adapterWillDismissInterstitial:self];
- [strongNetworkConnector adapterDidDismissInterstitial:self];
- }
- [UnityAds removeDelegate:self];
- }
- return;
+// id<GADMAdNetworkConnector> strongNetworkConnector = _networkConnector;
+// if (strongNetworkConnector) {
+// NSError *errorWithDescription = GADUnityErrorWithDescription(message);
+// [strongNetworkConnector adapter:self didFailAd:errorWithDescription];
+// if (error == kUnityAdsErrorShowError) {
+// [strongNetworkConnector adapterWillDismissInterstitial:self];
+// [strongNetworkConnector adapterDidDismissInterstitial:self];
+// }
+// [UnityAds removeDelegate:self];
+// }
+// return;
}
- (void)unityAdsDidStart:(nonnull NSString *)placementID {

MODIFY
adapters/Unity/UnityAdapter/GADMAdapterUnityBannerAd.m
adapters/Unity/UnityAdapter/GADMAdapterUnityBannerAd.m
if (![UnityAds isInitialized]) {
NSString *gameID = [strongConnector.credentials[kGADMAdapterUnityGameID] copy];
- [GADMAdapterUnitySingleton.sharedInstance initializeWithGameID:gameID];
+
+ UnitySingletonCompletion completeBlock = ^(UnityAdsError *error, NSString *message) {
+ if(!error) {
+ [UnityAds addDelegate:self];
+ [UnityAds load:gameID];
+ } else {
+ id<GADMAdNetworkConnector> strongConnector = self->_connector;
+ id<GADMAdNetworkAdapter> strongAdapter = self->_adapter;
+ if (strongConnector && strongAdapter) {
+ [strongConnector adapter:strongAdapter didFailAd:GADUnityErrorWithDescription(message)];
+ }
+ }
+ };
+
+ [[GADMAdapterUnitySingleton sharedInstance] initializeWithGameID:gameID
+ completeBlock:completeBlock];
}
_placementID = [strongConnector.credentials[kGADMAdapterUnityPlacementID] copy];

MODIFY
adapters/Unity/UnityAdapter/GADMAdapterUnityRewardedAd.m
adapters/Unity/UnityAdapter/GADMAdapterUnityRewardedAd.m
return;
}
- [[GADMAdapterUnitySingleton sharedInstance] initializeWithGameID:_gameID];
+ UnitySingletonCompletion completeBlock = ^(UnityAdsError *error, NSString *message) {
+ if(!error) {
[UnityAds addDelegate:self];
- [UnityAds load:_placementID];
+ [UnityAds load:[self getPlacementID]];
+ } else {
+ NSError *errorWithDescription = GADUnityErrorWithDescription(message);
+ self->_adLoadCompletionHandler(nil, errorWithDescription);
+ self->_adLoadCompletionHandler = nil;
+ }
+ };
+
+ [[GADMAdapterUnitySingleton sharedInstance] initializeWithGameID:_gameID
+ completeBlock:completeBlock];
+
}
- (void)presentFromViewController:(nonnull UIViewController *)viewController {
#pragma mark - Unity Delegate Methods
- (void)unityAdsDidError:(UnityAdsError)error withMessage:(nonnull NSString *)message {
- id<GADMediationRewardedAdEventDelegate> strongDelegate = _adEventDelegate;
- if (strongDelegate && error == kUnityAdsErrorShowError) {
- NSError *presentError = GADUnityErrorWithDescription(message);
- [_adEventDelegate didFailToPresentWithError:presentError];
- }
-
- if (_adLoadCompletionHandler) {
- NSError *errorWithDescription = GADUnityErrorWithDescription(message);
- _adLoadCompletionHandler(nil, errorWithDescription);
- _adLoadCompletionHandler = nil;
- }
+// id<GADMediationRewardedAdEventDelegate> strongDelegate = _adEventDelegate;
+// if (strongDelegate && error == kUnityAdsErrorShowError) {
+// NSError *presentError = GADUnityErrorWithDescription(message);
+// [_adEventDelegate didFailToPresentWithError:presentError];
+// }
+//
+// if (_adLoadCompletionHandler) {
+// NSError *errorWithDescription = GADUnityErrorWithDescription(message);
+// _adLoadCompletionHandler(nil, errorWithDescription);
+// _adLoadCompletionHandler = nil;
+// }
}
- (void)unityAdsDidFinish:(nonnull NSString *)placementID
GADAdReward *reward = [[GADAdReward alloc] initWithRewardType:@""
rewardAmount:[NSDecimalNumber one]];
[strongDelegate didRewardUserWithReward:reward];
+ } else if (state == kUnityAdsFinishStateError) {
+ NSError *presentError = GADUnityErrorWithDescription(@"Finish State Error");
+ [strongDelegate didFailToPresentWithError:presentError];
}
[strongDelegate willDismissFullScreenView];
[strongDelegate didDismissFullScreenView];
+ [strongDelegate didEndVideo];
[UnityAds removeDelegate:self];
}
}

MODIFY
adapters/Unity/UnityAdapter/GADMAdapterUnitySingleton.h
adapters/Unity/UnityAdapter/GADMAdapterUnitySingleton.h
#import "GADMAdapterUnityProtocol.h"
+typedef void (^UnitySingletonCompletion)(UnityAdsError *error, NSString *message);
+
@interface GADMAdapterUnitySingleton : NSObject
+@property (nonatomic, strong) UnitySingletonCompletion completeBlock;
+
/// Shared instance.
+ (instancetype)sharedInstance;
/// Configures a reward-based video ad with provided |gameID| and |adapterDelegate| and returns
/// YES if successful; otherwise returns NO.
-- (void)initializeWithGameID:(NSString *)gameID;
+- (void)initializeWithGameID:(NSString *)gameID
+ completeBlock:(UnitySingletonCompletion)completeBlock;
@end

MODIFY
adapters/Unity/UnityAdapter/GADMAdapterUnitySingleton.m
adapters/Unity/UnityAdapter/GADMAdapterUnitySingleton.m
#import "GADMAdapterUnityConstants.h"
#import "GADMAdapterUnityUtils.h"
-@interface GADMAdapterUnitySingleton () {
- /// Array to hold all adapter delegates.
- NSMapTable *_adapterDelegates;
-
- int impressionOrdinal;
- int missedImpressionOrdinal;
-
- /// Connector from unity adapter to send Unity callbacks.
- __weak id<GADMAdapterUnityDataProvider, UnityAdsExtendedDelegate> _currentShowingUnityDelegate;
-}
+@interface GADMAdapterUnitySingleton () <UnityAdsExtendedDelegate>
@end
- (id)init {
self = [super init];
- if (self) {
- _adapterDelegates = [NSMapTable mapTableWithKeyOptions:NSMapTableStrongMemory
- valueOptions:NSMapTableWeakMemory];
- }
return self;
}
-- (void)initializeWithGameID:(NSString *)gameID {
+- (void)initializeWithGameID:(NSString *)gameID
+ completeBlock:(UnitySingletonCompletion)completeBlock{
if ([UnityAds isInitialized]) {
+ completeBlock(NULL, @"UnityAds Initialization Succeeded");
return;
}
- // Metadata needed by Unity Ads SDK before initialization.
+ [self setCompleteBlock:completeBlock];
+
UADSMediationMetaData *mediationMetaData = [[UADSMediationMetaData alloc] init];
[mediationMetaData setName:kGADMAdapterUnityMediationNetworkName];
[mediationMetaData setVersion:kGADMAdapterUnityVersion];
[mediationMetaData set:@"adapter_version" value:[UnityAds getVersion]];
[mediationMetaData commit];
- // Initializing Unity Ads with |gameID|.
- [UnityAds initialize:gameID testMode:NO enablePerPlacementLoad:YES];
+ [UnityAds initialize:gameID testMode:false enablePerPlacementLoad:true];
+ [UnityAds addDelegate:self];
+}
+
+#pragma mark - Unity Delegate Methods
+
+- (void)unityAdsPlacementStateChanged:(NSString *)placementId
+ oldState:(UnityAdsPlacementState)oldState
+ newState:(UnityAdsPlacementState)newState {
+ if (newState == kUnityAdsPlacementStateWaiting || newState == kUnityAdsPlacementStateReady) {
+ if (self.completeBlock) {
+ self.completeBlock(NULL, @"UnityAds Initialization Succeeded");
+ }
+ }
+ return;
+}
+
+- (void)unityAdsDidError:(UnityAdsError)error withMessage:(NSString *)message {
+ if (error == kUnityAdsErrorNotInitialized || error == kUnityAdsErrorInvalidArgument || error == kUnityAdsErrorInitializedFailed || error == kUnityAdsErrorInitSanityCheckFail) {
+ if (self.completeBlock) {
+ self.completeBlock(&error, @"Unity Ads Initialization Failed");
+ }
+ }
+ return;
+}
+
+- (void)unityAdsDidFinish:(NSString *)placementID withFinishState:(UnityAdsFinishState)state {
+}
+
+- (void)unityAdsDidStart:(NSString *)placementID {
+}
+
+- (void)unityAdsReady:(NSString *)placementID {
+}
+
+- (void)unityAdsDidClick:(NSString *)placementID {
}
@end

MODIFY
adapters/Unity/UnityAdapter/GADMediationAdapterUnity.m
adapters/Unity/UnityAdapter/GADMediationAdapterUnity.m
#import "GADUnityError.h"
@import UnityAds;
-@interface GADMediationAdapterUnity ()
+@interface GADMediationAdapterUnity () <GADMediationAdapter>
@property(nonatomic, strong) GADMAdapterUnityRewardedAd *rewardedAd;
NSLog(@"Initializing Unity Ads SDK with the game ID %@.", gameID);
}
- [[GADMAdapterUnitySingleton sharedInstance] initializeWithGameID:gameID];
+ UnitySingletonCompletion completeBlock = ^(UnityAdsError *error, NSString *message) {
+ if(!error && completionHandler) {
completionHandler(nil);
+ } else {
+ completionHandler(GADUnityErrorWithDescription(message));
+ }
+ };
+
+ [[GADMAdapterUnitySingleton sharedInstance] initializeWithGameID:gameID
+ completeBlock:completeBlock];
}
+ (GADVersionNumber)adSDKVersion {

Update singleton to use complete block

Update singleton to use complete block