Skip to content

Commit

Permalink
Unity SDK 2.1.0 support and backwards compatibility.
Browse files Browse the repository at this point in the history
 - Ads delegation pattern
    This alleviates any race condition issues we may run into in the future.
    If the user loads multiple ads, it's unclear which delegate gets called,
    and the previous delegates are lost.

 - Retains the last placement ID for the unityAdsDidError callback

    In the future, unityAdsDidError should pass in the placement ID so that
    delegation can happen per the usual flow.

 - Guard against sending the UnityAdsReady message if an ad is playing

    This fixes the issue MoPub is seeing with an infinite loop cycle when an
    ad is dismissed.

 - Etc.
    Remove unecessary comments
    Prevents ready being dispatched more than once
  • Loading branch information
RossRothenstine committed Jun 21, 2017
1 parent 90684a2 commit cf02e62
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 17 deletions.
6 changes: 5 additions & 1 deletion AdNetworkSupport/Unity/MPUnityRouter.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
@interface MPUnityRouter : NSObject <UnityAdsExtendedDelegate>

@property (nonatomic, weak) id<MPUnityRouterDelegate> delegate;
@property NSMutableDictionary* delegateMap;
@property NSString* currentPlacementId;

+ (MPUnityRouter *)sharedRouter;

Expand All @@ -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
59 changes: 46 additions & 13 deletions AdNetworkSupport/Unity/MPUnityRouter.m
Original file line number Diff line number Diff line change
Expand Up @@ -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];
Expand All @@ -28,8 +35,8 @@ + (MPUnityRouter *)sharedRouter
- (void)requestVideoAdWithGameId:(NSString *)gameId placementId:(NSString *)placementId delegate:(id<MPUnityRouterDelegate>)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];
Expand All @@ -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 {
Expand All @@ -59,16 +66,18 @@ - (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];
[delegate unityAdsDidFailWithError:error];
}
}

- (id<MPUnityRouterDelegate>)getDelegate:(NSString*) placementId {
return [self.delegateMap valueForKey:placementId];
}

- (void)clearDelegate:(id<MPUnityRouterDelegate>)delegate
{
if (self.delegate == delegate)
Expand All @@ -81,25 +90,49 @@ - (void)clearDelegate:(id<MPUnityRouterDelegate>)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
10 changes: 7 additions & 3 deletions AdNetworkSupport/Unity/UnityAdsInterstitialCustomEvent.m
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

@interface UnityAdsInterstitialCustomEvent () <MPUnityRouterDelegate>

@property BOOL loadRequested;
@property (nonatomic, copy) NSString *placementId;

@end
Expand All @@ -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) {
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit cf02e62

Please sign in to comment.