Updated as per of feedback - added a fix to support multiple interstitial instances
MODIFY
adapters/Vungle/VungleAdapter/GADMAdapterVungleInterstitial.m
adapters/Vungle/VungleAdapter/GADMAdapterVungleInterstitial.m
@@ -22,14 +22,14 @@ static NSString *const kGADMAdapterVungleInterstitialKeyApplicationID = @"applic
self = [super init];
if (self) {
self.connector = connector;
- [vungleHelper sharedInstance].interstitialDelegate = self;
+ [[vungleHelper sharedInstance] addDelegate:self];
}
return self;
}
- (void)dealloc {
- [vungleHelper sharedInstance].interstitialDelegate = nil;
+ [self stopBeingDelegate];
}
- (void)getBannerWithSize:(GADAdSize)adSize{
@@ -42,11 +42,7 @@ static NSString *const kGADMAdapterVungleInterstitialKeyApplicationID = @"applic
}
- (void) loadAd {
- if ([[vungleHelper sharedInstance] isAdPlayableFor:desiredPlacement]) {
- [_connector adapterDidReceiveInterstitial:self];
- } else {
- [[vungleHelper sharedInstance] loadAd:InterstitialAdapter placement:desiredPlacement];
- }
+ [[vungleHelper sharedInstance] loadAd:desiredPlacement];
}
@@ -58,19 +54,15 @@ static NSString *const kGADMAdapterVungleInterstitialKeyApplicationID = @"applic
[_connector adapter:self didFailAd:[NSError errorWithDomain:@"GADMAdapterVungleInterstitial" code:0 userInfo:@{NSLocalizedDescriptionKey: @"Placements should be specified!"}]];
return;
}
-
- if ([[vungleHelper sharedInstance] isInitialised]) {
- [self loadAd];
- } else {
+ waitingInit = YES;
NSDictionary *serverParameters = [_connector credentials];
NSString *applicationID = [serverParameters objectForKey:kGADMAdapterVungleInterstitialKeyApplicationID];
- [[vungleHelper sharedInstance] initWithAppId:applicationID placements:extras.allPlacements adapter:InterstitialAdapter];
- }
+ [[vungleHelper sharedInstance] initWithAppId:applicationID placements:extras.allPlacements];
}
- (void)stopBeingDelegate {
_connector = nil;
- [vungleHelper sharedInstance].interstitialDelegate = nil;
+ [[vungleHelper sharedInstance] removeDelegate:self];
}
- (BOOL)isBannerAnimationOK:(GADMBannerAnimationType)animType{
@@ -78,7 +70,7 @@ static NSString *const kGADMAdapterVungleInterstitialKeyApplicationID = @"applic
}
- (void)presentInterstitialFromRootViewController:(UIViewController *)rootViewController {
- if (![[vungleHelper sharedInstance] playAd:rootViewController adapter:InterstitialAdapter placement:desiredPlacement extras:[_connector networkExtras]]) {
+ if (![[vungleHelper sharedInstance] playAd:rootViewController delegate:self extras:[_connector networkExtras]]) {
[_connector adapterDidDismissInterstitial:self];
}
}
@@ -88,8 +80,11 @@ static NSString *const kGADMAdapterVungleInterstitialKeyApplicationID = @"applic
@synthesize desiredPlacement;
+@synthesize waitingInit;
+
-(void)initialized:(BOOL)isSuccess error:(NSError *)error {
- if (isSuccess) {
+ waitingInit = NO;
+ if (isSuccess && desiredPlacement) {
if (desiredPlacement) {
[self loadAd];
}
@@ -113,6 +108,7 @@ static NSString *const kGADMAdapterVungleInterstitialKeyApplicationID = @"applic
-(void)willCloseAd:(bool)completedView{
[_connector adapterWillDismissInterstitial:self];
[_connector adapterDidDismissInterstitial:self];
+ desiredPlacement = nil;
}
@end

MODIFY
adapters/Vungle/VungleAdapter/GADMAdapterVungleRewardBasedVideoAd.m
adapters/Vungle/VungleAdapter/GADMAdapterVungleRewardBasedVideoAd.m
@@ -23,13 +23,13 @@ static NSString *const kGADMAdapterVungleRewardBasedVideoAdKeyApplicationID = @"
self = [super init];
if (self) {
self.connector = connector;
- [vungleHelper sharedInstance].rewardDelegate = self;
+ [[vungleHelper sharedInstance] addDelegate:self];
}
return self;
}
- (void)dealloc {
- [vungleHelper sharedInstance].rewardDelegate = nil;
+ [self stopBeingDelegate];
}
- (void)setUp {
@@ -41,40 +41,34 @@ static NSString *const kGADMAdapterVungleRewardBasedVideoAdKeyApplicationID = @"
[_connector adapter:self didFailToSetUpRewardBasedVideoAdWithError:[NSError errorWithDomain:@"GADMAdapterVungleRewardBasedVideoAd" code:0 userInfo:@{NSLocalizedDescriptionKey: @"Placements should be specified!"}]];
return;
}
- vungleHelper* sdk = [vungleHelper sharedInstance];
- if ([sdk isInitialised]) {
- [_connector adapterDidSetUpRewardBasedVideoAd:self];
- } else {
- [sdk initWithAppId:applicationID placements:extras.allPlacements adapter:RewardBasedAdapter];
- }
+ waitingInit = YES;
+ [[vungleHelper sharedInstance] initWithAppId:applicationID placements:extras.allPlacements];
}
- (void)requestRewardBasedVideoAd {
- vungleHelper* sdk = [vungleHelper sharedInstance];
desiredPlacement = ((VungleAdNetworkExtras *)[_connector networkExtras]).playingPlacement;
- if ([sdk isAdPlayableFor:desiredPlacement]) {
- [_connector adapterDidReceiveRewardBasedVideoAd:self];
- } else {
- [sdk loadAd:RewardBasedAdapter placement:desiredPlacement];
- }
+ [[vungleHelper sharedInstance] loadAd:desiredPlacement];
}
- (void)presentRewardBasedVideoAdWithRootViewController:(UIViewController *)viewController {
- if (![[vungleHelper sharedInstance] playAd:viewController adapter:RewardBasedAdapter placement:desiredPlacement extras:[_connector networkExtras]]) {
+ if (![[vungleHelper sharedInstance] playAd:viewController delegate:self extras:[_connector networkExtras]]) {
[_connector adapterDidCloseRewardBasedVideoAd:self];
}
}
- (void)stopBeingDelegate {
_connector = nil;
- [vungleHelper sharedInstance].rewardDelegate = nil;
+ [[vungleHelper sharedInstance] removeDelegate:self];
}
#pragma mark - vungleHelper delegates
@synthesize desiredPlacement;
+@synthesize waitingInit;
+
-(void)initialized:(BOOL)isSuccess error:(NSError *)error{
+ waitingInit = NO;
if (isSuccess){
[_connector adapterDidSetUpRewardBasedVideoAd:self];
} else {
@@ -101,6 +95,7 @@ static NSString *const kGADMAdapterVungleRewardBasedVideoAdKeyApplicationID = @"
[_connector adapter:self didRewardUserWithReward:reward];
}
[_connector adapterDidCloseRewardBasedVideoAd:self];
+ desiredPlacement = nil;
}
@end

MODIFY
adapters/Vungle/VungleAdapter/vungleHelper.h
adapters/Vungle/VungleAdapter/vungleHelper.h
#import "VungleAdNetworkExtras.h"
-typedef enum {
- InterstitialAdapter = 1 << 1,
- RewardBasedAdapter = 1 << 2
-} Adapter;
-
@protocol VungleDelegate <NSObject>
-(void)initialized:(BOOL)isSuccess error:(NSError *)error;
-(void)adAvailable;
-(void)willShowAd;
-(void)willLeaveApplication;
-(void)willCloseAd:(bool)completedView;
-@property (strong) NSString * desiredPlacement;
+@property (readonly, strong) NSString * desiredPlacement;
+@property (readonly, assign) BOOL waitingInit;
@end
@interface vungleHelper : NSObject<VungleSDKDelegate>
-@property (strong) id<VungleDelegate> interstitialDelegate;
-@property (strong) id<VungleDelegate> rewardDelegate;
@property (readonly) BOOL isInitialising;
-@property (readonly) BOOL isInitialised;
+ (NSString *)adapterVersion;
+ (vungleHelper*) sharedInstance;
--(void)initWithAppId:(NSString *)appId placements:(NSArray<NSString *>*)placements adapter:(Adapter)adapter;
--(BOOL)isAdPlayableFor:(NSString *)placement;
--(BOOL)playAd:(UIViewController *)viewController adapter:(Adapter)adapter placement:(NSString *)placement extras:(VungleAdNetworkExtras *)extras;
--(void)loadAd:(Adapter)adapter placement:(NSString *)placement;
+-(void)initWithAppId:(NSString *)appId placements:(NSArray<NSString *>*)placements;
+-(BOOL)playAd:(UIViewController *)viewController delegate:(id<VungleDelegate>)delegate extras:(VungleAdNetworkExtras *)extras;
+-(void)loadAd:(NSString *)placement;
+-(void)addDelegate:(id<VungleDelegate>)delegate;
+-(void)removeDelegate:(id<VungleDelegate>)delegate;
@end

MODIFY
adapters/Vungle/VungleAdapter/vungleHelper.m
adapters/Vungle/VungleAdapter/vungleHelper.m
#import "vungleHelper.h"
+@interface vungleHelper()
+@property (strong) NSMutableArray<id<VungleDelegate>> * delegates;
+@property (strong) id<VungleDelegate> playingDelegate;
+@end
+
@implementation vungleHelper
-static Adapter initializing = 0;
-static Adapter waiting = 0;
-static Adapter playing = 0;
+ (NSString *)adapterVersion {
- return @"2.0.0";
+ return @"5.1.0";
}
+ (vungleHelper*) sharedInstance{
@@ -23,11 +25,12 @@ static Adapter playing = 0;
self = [super init];
if (self){
[VungleSDK sharedSDK].delegate = self;
+ _delegates = [NSMutableArray array];
}
return self;
}
--(void)initWithAppId:(NSString *)appId placements:(NSArray<NSString *>*)placements adapter:(Adapter)adapter{
+-(void)initWithAppId:(NSString *)appId placements:(NSArray<NSString *>*)placements{
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
NSString *version = [[vungleHelper adapterVersion] stringByReplacingOccurrencesOfString:@"." withString:@"_"];
@@ -38,10 +41,9 @@ static Adapter playing = 0;
});
VungleSDK* sdk = [VungleSDK sharedSDK];
if ([sdk isInitialized]) {
- _isInitialised = true;
+ [self initialized:true error:nil];
return;
}
- initializing |= adapter;
if ([self isInitialising]) {
return;
}
@@ -55,111 +57,86 @@ static Adapter playing = 0;
}
}
--(BOOL)isAdPlayableFor:(NSString *)placement{
- return [[VungleSDK sharedSDK] isAdCachedForPlacementID:placement];
+-(void)addDelegate:(id<VungleDelegate>)delegate {
+ if (delegate && ![_delegates containsObject:delegate]) {
+ [_delegates addObject:delegate];
+ }
+}
+
+-(void)removeDelegate:(id<VungleDelegate>)delegate {
+ if (delegate && [_delegates containsObject:delegate]) {
+ [_delegates removeObject:delegate];
+ }
+}
+
+-(void)notifyAdIsReady:(NSString *)placement {
+ for (id<VungleDelegate> item in _delegates) {
+ if ([placement isEqualToString:item.desiredPlacement]) {
+ [item adAvailable];
+ }
+ }
}
--(void)loadAd:(Adapter)adapter placement:(NSString *)placement {
- waiting |= adapter;
+-(void)loadAd:(NSString *)placement {
+ if (placement) {
VungleSDK* sdk = [VungleSDK sharedSDK];
if ([sdk isAdCachedForPlacementID:placement]) {
- [self vungleAdPlayabilityUpdate:true placementID:placement];
+ [self notifyAdIsReady:placement];
return;
}
[sdk loadPlacementWithID:placement error:nil];
}
+}
-- (BOOL)playAd:(UIViewController *)viewController adapter:(Adapter)adapter placement:(NSString *)placement extras:(VungleAdNetworkExtras *)extras {
- if (![[VungleSDK sharedSDK] isAdCachedForPlacementID:placement]) {
+- (BOOL)playAd:(UIViewController *)viewController delegate:(id<VungleDelegate>)delegate extras:(VungleAdNetworkExtras *)extras {
+ if (!delegate || !delegate.desiredPlacement || ![[VungleSDK sharedSDK] isAdCachedForPlacementID:delegate.desiredPlacement]) {
return false;
}
NSMutableDictionary *options = [[NSMutableDictionary alloc] init];
NSError *error = nil;
bool startPlaying = true;
[VungleSDK sharedSDK].muted = extras.muted;
- playing |= adapter;
- if (![[VungleSDK sharedSDK] playAd:viewController options:options placementID:placement error:&error]){
- playing &= ~adapter;
+ if (extras.userId)
+ [options setObject:extras.userId forKey:VunglePlayAdOptionKeyUser];
+ if (![[VungleSDK sharedSDK] playAd:viewController options:options placementID:delegate.desiredPlacement error:&error]){
startPlaying = false;
}
if (error) {
NSLog(@"Adapter failed to present reward based video ad, error %@", [error localizedDescription]);
- playing &= ~adapter;
startPlaying = false;
};
+ if (startPlaying) {
+ _playingDelegate = delegate;
+ }
return startPlaying;
}
-(void)initialized:(BOOL)isSuccess error:(NSError *)error {
_isInitialising = false;
- if (isSuccess) {
- _isInitialised = true;
+ for (id<VungleDelegate> item in _delegates) {
+ if ([item waitingInit]) {
+ [item initialized:isSuccess error:error];
}
- if ((initializing & InterstitialAdapter) && _interstitialDelegate && [_interstitialDelegate respondsToSelector:@selector(initialized:error:)]) {
- initializing &= ~InterstitialAdapter;
- [_interstitialDelegate initialized:isSuccess error:error];
- }
-
- if ((initializing & RewardBasedAdapter) && _rewardDelegate && [_rewardDelegate respondsToSelector:@selector(initialized:error:)]) {
- initializing &= ~RewardBasedAdapter;
- [_rewardDelegate initialized:isSuccess error:error];
}
}
#pragma mark - VungleSDKDelegate methods
- (void)vungleWillShowAdForPlacementID:(nullable NSString *)placementID {
- if ((playing & InterstitialAdapter) && _interstitialDelegate && [_interstitialDelegate respondsToSelector:@selector(willShowAd)]){
- [_interstitialDelegate willShowAd];
- }
- if ((playing & RewardBasedAdapter) && _rewardDelegate && [_rewardDelegate respondsToSelector:@selector(willShowAd)]){
- [_rewardDelegate willShowAd];
+ if (_playingDelegate) {
+ [_playingDelegate willShowAd];
}
}
- (void)vungleWillCloseAdWithViewInfo:(nonnull VungleViewInfo *)info placementID:(nonnull NSString *)placementID {
- if (playing & InterstitialAdapter){
- playing &= ~InterstitialAdapter;
- if (_interstitialDelegate && [_interstitialDelegate respondsToSelector:@selector(willCloseAd:)]){
- [_interstitialDelegate willCloseAd:info.completedView];
- }
- }
- if (playing & RewardBasedAdapter){
- playing &= ~RewardBasedAdapter;
- if (_rewardDelegate && [_rewardDelegate respondsToSelector:@selector(willCloseAd:)]){
- [_rewardDelegate willCloseAd:info.completedView];
- }
+ if (_playingDelegate) {
+ [_playingDelegate willCloseAd:info.completedView];
}
}
- (void)vungleAdPlayabilityUpdate:(BOOL)isAdPlayable placementID:(nullable NSString *)placementID {
if (isAdPlayable) {
- if (waiting & InterstitialAdapter) {
- if (_interstitialDelegate && [_interstitialDelegate respondsToSelector:@selector(desiredPlacement)]){
- NSString* desired = [_interstitialDelegate desiredPlacement];
- if (!desired || [desired isEqualToString:placementID]) {
- waiting &= ~InterstitialAdapter;
- if ([_interstitialDelegate respondsToSelector:@selector(adAvailable)]) {
- [_interstitialDelegate adAvailable];
- }
- }
- } else {
- waiting &= ~InterstitialAdapter;
- }
- }
- if (waiting & RewardBasedAdapter) {
- if (_rewardDelegate && [_rewardDelegate respondsToSelector:@selector(desiredPlacement)]){
- NSString* desired = [_rewardDelegate desiredPlacement];
- if (!desired || [desired isEqualToString:placementID]) {
- waiting &= ~RewardBasedAdapter;
- if ([_rewardDelegate respondsToSelector:@selector(adAvailable)]) {
- [_rewardDelegate adAvailable];
- }
- }
- } else {
- waiting &= ~RewardBasedAdapter;
- }
- }
+ [self notifyAdIsReady:placementID];
}
}

Updated as per of feedback - added a fix to support multiple interstitial instances