diff --git a/AdNetworkSupport/Unity/MPUnityRouter.h b/AdNetworkSupport/Unity/MPUnityRouter.h index afab6ac84..025e20d81 100644 --- a/AdNetworkSupport/Unity/MPUnityRouter.h +++ b/AdNetworkSupport/Unity/MPUnityRouter.h @@ -15,6 +15,8 @@ @interface MPUnityRouter : NSObject @property (nonatomic, weak) id delegate; +@property NSMutableDictionary* delegateMap; +@property NSString* currentPlacementId; + (MPUnityRouter *)sharedRouter; @@ -32,7 +34,9 @@ - (void)unityAdsDidStart:(NSString *)placementId; - (void)unityAdsDidFinish:(NSString *)placementId withFinishState:(UnityAdsFinishState)state; - (void)unityAdsDidClick:(NSString *)placementId; - - (void)unityAdsDidFailWithError:(NSError *)error; +@optional +- (void)unityAdsPlacementStateChanged:(NSString*)placementId oldState:(UnityAdsPlacementState)oldState newState:(UnityAdsPlacementState)newState; + @end diff --git a/AdNetworkSupport/Unity/MPUnityRouter.m b/AdNetworkSupport/Unity/MPUnityRouter.m index b2b07f042..ba3eece3c 100644 --- a/AdNetworkSupport/Unity/MPUnityRouter.m +++ b/AdNetworkSupport/Unity/MPUnityRouter.m @@ -20,6 +20,13 @@ @interface MPUnityRouter () @implementation MPUnityRouter +- (id) init { + self = [super init]; + self.delegateMap = [[NSMutableDictionary alloc] init]; + + return self; +} + + (MPUnityRouter *)sharedRouter { return [[MPInstanceProvider sharedProvider] sharedMPUnityRouter]; @@ -28,8 +35,8 @@ + (MPUnityRouter *)sharedRouter - (void)requestVideoAdWithGameId:(NSString *)gameId placementId:(NSString *)placementId delegate:(id)delegate; { if (!self.isAdPlaying) { - self.delegate = delegate; - + [self.delegateMap setObject:delegate forKey:placementId]; + static dispatch_once_t unityInitToken; dispatch_once(&unityInitToken, ^{ UADSMediationMetaData *mediationMetaData = [[UADSMediationMetaData alloc] init]; @@ -41,7 +48,7 @@ - (void)requestVideoAdWithGameId:(NSString *)gameId placementId:(NSString *)plac // Need to check immediately as an ad may be cached. if ([self isAdAvailableForPlacementId:placementId]) { - [self.delegate unityAdsReady:placementId]; + [self unityAdsReady:placementId]; } // MoPub timeout will handle the case for an ad failing to load. } else { @@ -59,9 +66,7 @@ - (void)presentVideoAdFromViewController:(UIViewController *)viewController cust { if (!self.isAdPlaying && [self isAdAvailableForPlacementId:placementId]) { self.isAdPlaying = YES; - - self.delegate = delegate; - + self.currentPlacementId = placementId; [UnityAds show:viewController placementId:placementId]; } else { NSError *error = [NSError errorWithDomain:MoPubRewardedVideoAdsSDKDomain code:MPRewardedVideoAdErrorUnknown userInfo:nil]; @@ -69,6 +74,10 @@ - (void)presentVideoAdFromViewController:(UIViewController *)viewController cust } } +- (id)getDelegate:(NSString*) placementId { + return [self.delegateMap valueForKey:placementId]; +} + - (void)clearDelegate:(id)delegate { if (self.delegate == delegate) @@ -81,25 +90,49 @@ - (void)clearDelegate:(id)delegate - (void)unityAdsReady:(NSString *)placementId { - [self.delegate unityAdsReady:placementId]; + if (!self.isAdPlaying) { + id delegate = [self getDelegate:placementId]; + if (delegate != nil) { + [delegate unityAdsReady:placementId]; + } + } } - (void)unityAdsDidError:(UnityAdsError)error withMessage:(NSString *)message { - [self.delegate unityAdsDidError:error withMessage:message]; + id delegate = [self getDelegate:self.currentPlacementId]; + if (delegate != nil) { + [delegate unityAdsDidError:error withMessage:message]; + } } - (void)unityAdsDidStart:(NSString *)placementId { - [self.delegate unityAdsDidStart:placementId]; + id delegate = [self getDelegate:placementId]; + if (delegate != nil) { + [delegate unityAdsDidStart:placementId]; + } } - (void)unityAdsDidFinish:(NSString *)placementId withFinishState:(UnityAdsFinishState)state { - [self.delegate unityAdsDidFinish:placementId withFinishState:state]; + id delegate = [self getDelegate:placementId]; + if (delegate != nil) { + [delegate unityAdsDidFinish:placementId withFinishState:state]; + } + [self.delegateMap removeObjectForKey:placementId]; self.isAdPlaying = NO; } -- (void)unityAdsDidClick:(NSString *)placementId -{ - [self.delegate unityAdsDidClick:placementId]; +- (void)unityAdsDidClick:(NSString *)placementId { + id delegate = [self getDelegate:placementId]; + if (delegate != nil) { + [delegate unityAdsDidClick:placementId]; + } +} + +- (void)unityAdsPlacementStateChanged:(NSString *)placementId oldState:(UnityAdsPlacementState)oldState newState:(UnityAdsPlacementState)newState { + id delegate = [self getDelegate:placementId]; + if (delegate != nil && [delegate respondsToSelector:@selector(unityAdsPlacementStateChanged:oldState:newState:)]) { + [delegate unityAdsPlacementStateChanged:placementId oldState:oldState newState:newState]; + } } @end diff --git a/AdNetworkSupport/Unity/UnityAdsInterstitialCustomEvent.m b/AdNetworkSupport/Unity/UnityAdsInterstitialCustomEvent.m index 2bc6e9438..6dd98d640 100644 --- a/AdNetworkSupport/Unity/UnityAdsInterstitialCustomEvent.m +++ b/AdNetworkSupport/Unity/UnityAdsInterstitialCustomEvent.m @@ -16,6 +16,7 @@ @interface UnityAdsInterstitialCustomEvent () +@property BOOL loadRequested; @property (nonatomic, copy) NSString *placementId; @end @@ -27,8 +28,8 @@ - (void)dealloc [[MPUnityRouter sharedRouter] clearDelegate:self]; } -- (void)requestInterstitialWithCustomEventInfo:(NSDictionary *)info -{ +- (void)requestInterstitialWithCustomEventInfo:(NSDictionary *)info { + self.loadRequested = YES; NSString *gameId = [info objectForKey:kMPUnityRewardedVideoGameId]; self.placementId = [info objectForKey:kUnityAdsOptionPlacementIdKey]; if (self.placementId == nil) { @@ -74,7 +75,10 @@ - (void)handleAdPlayedForCustomEventNetwork - (void)unityAdsReady:(NSString *)placementId { - [self.delegate interstitialCustomEvent:self didLoadAd:placementId]; + if (self.loadRequested) { + [self.delegate interstitialCustomEvent:self didLoadAd:placementId]; + self.loadRequested = NO; + } } - (void)unityAdsDidError:(UnityAdsError)error withMessage:(NSString *)message