From 3606154ea527295f162fa36893c35a5dd9cb428c Mon Sep 17 00:00:00 2001 From: ManojBudumuru-vungle <63756359+ManojBudumuru-vungle@users.noreply.github.com> Date: Wed, 6 Mar 2024 14:36:25 +0530 Subject: [PATCH] Re-init SDK on load api if SDK is not initialized and error code mapping (#13) * Re-init SDK on load api if SDK is not initialized. This commit will include the changes to update the loadAd implementation in adapter to re-init SDK if the SDK has not yet initialized. IOS-6620 IOS-6619 --- .../VungleAdapter/ALVungleMediationAdapter.m | 302 ++++++++++-------- 1 file changed, 175 insertions(+), 127 deletions(-) diff --git a/Vungle/VungleAdapter/ALVungleMediationAdapter.m b/Vungle/VungleAdapter/ALVungleMediationAdapter.m index 7873aaadde6..eb5400cad77 100644 --- a/Vungle/VungleAdapter/ALVungleMediationAdapter.m +++ b/Vungle/VungleAdapter/ALVungleMediationAdapter.m @@ -9,7 +9,7 @@ #import "ALVungleMediationAdapter.h" #import -#define ADAPTER_VERSION @"7.2.2.0" +#define ADAPTER_VERSION @"7.3.0.0" @interface ALVungleMediationAdapterInterstitialAdDelegate : NSObject @property (nonatomic, weak) ALVungleMediationAdapter *parentAdapter; @@ -99,6 +99,7 @@ @implementation ALVungleMediationAdapter static ALAtomicBoolean *ALVungleInitialized; static MAAdapterInitializationStatus ALVungleIntializationStatus = NSIntegerMin; +static NSMutableSet *completionHandlers; + (void)initialize { @@ -115,9 +116,32 @@ - (void)initializeWithParameters:(id)paramete if ( [ALVungleInitialized compareAndSet: NO update: YES] ) { + [self initVungleSDK:parameters completionHandler:completionHandler]; + } + else + { + completionHandler(ALVungleIntializationStatus, nil); + } +} + +- (void)initVungleSDK:(id)parameters completionHandler:(void (^)(MAAdapterInitializationStatus, NSString *_Nullable))completionHandler { + if ( ![VungleAds isInitialized] ) + { + @synchronized(completionHandlers) + { + if (completionHandlers == nil) { + completionHandlers = [NSMutableSet set]; + } + [completionHandlers addObject: completionHandler]; + } + + if ( ALVungleIntializationStatus == MAAdapterInitializationStatusInitializing ) + { + return; + } ALVungleIntializationStatus = MAAdapterInitializationStatusInitializing; - NSString *appID = [parameters.serverParameters al_stringForKey: @"app_id"]; + NSString *appID = [parameters.serverParameters al_stringForKey: @"app_id"] ? :@""; [self log: @"Initializing Vungle SDK with app id: %@...", appID]; [VungleAds setIntegrationName: @"max" version: ADAPTER_VERSION]; @@ -129,14 +153,28 @@ - (void)initializeWithParameters:(id)paramete ALVungleIntializationStatus = MAAdapterInitializationStatusInitializedFailure; NSString *errorString = [NSString stringWithFormat: @"%ld:%@", (long) error.code, error.localizedDescription]; - completionHandler(ALVungleIntializationStatus, errorString); + @synchronized(completionHandlers) + { + for ( void (^handler)(MAAdapterInitializationStatus, NSString *_Nullable) in completionHandlers ) + { + handler(ALVungleIntializationStatus, errorString); + } + [completionHandlers removeAllObjects]; + } } else { [self log: @"Vungle SDK initialized"]; ALVungleIntializationStatus = MAAdapterInitializationStatusInitializedSuccess; - completionHandler(ALVungleIntializationStatus, nil); + @synchronized(completionHandlers) + { + for ( void (^handler)(MAAdapterInitializationStatus, NSString *_Nullable) in completionHandlers ) + { + handler(ALVungleIntializationStatus, nil); + } + [completionHandlers removeAllObjects]; + } } }]; } @@ -205,21 +243,23 @@ - (void)loadInterstitialAdForParameters:(id)paramet NSString *placementIdentifier = parameters.thirdPartyAdPlacementIdentifier; [self log: @"Loading %@interstitial ad for placement: %@...", ( isBiddingAd ? @"bidding " : @"" ), placementIdentifier]; - if ( ![VungleAds isInitialized] ) - { - [self log: @"Vungle SDK not successfully initialized: failing interstitial ad load..."]; - [delegate didFailToLoadInterstitialAdWithError: MAAdapterError.notInitialized]; - - return; - } - - [self updateUserPrivacySettingsForParameters: parameters]; - - self.interstitialAd = [[VungleInterstitial alloc] initWithPlacementId: placementIdentifier]; - self.interstitialAdDelegate = [[ALVungleMediationAdapterInterstitialAdDelegate alloc] initWithParentAdapter: self andNotify: delegate]; - self.interstitialAd.delegate = self.interstitialAdDelegate; - - [self.interstitialAd load: bidResponse]; + [self initVungleSDK:parameters completionHandler:^(MAAdapterInitializationStatus initializationStatus, NSString * _Nullable errorMessage) { + if ( initializationStatus == MAAdapterInitializationStatusInitializedFailure ) + { + [self log: @"Vungle SDK not successfully initialized: failing interstitial ad load with error %@", errorMessage]; + [delegate didFailToLoadInterstitialAdWithError: MAAdapterError.notInitialized]; + } + else + { + [self updateUserPrivacySettingsForParameters: parameters]; + + self.interstitialAd = [[VungleInterstitial alloc] initWithPlacementId: placementIdentifier]; + self.interstitialAdDelegate = [[ALVungleMediationAdapterInterstitialAdDelegate alloc] initWithParentAdapter: self andNotify: delegate]; + self.interstitialAd.delegate = self.interstitialAdDelegate; + + [self.interstitialAd load: bidResponse]; + } + }]; } - (void)showInterstitialAdForParameters:(id)parameters andNotify:(id)delegate @@ -263,21 +303,23 @@ - (void)loadAppOpenAdForParameters:(id)parameters a NSString *placementIdentifier = parameters.thirdPartyAdPlacementIdentifier; [self log: @"Loading %@app open ad for placement: %@...", ( isBiddingAd ? @"bidding " : @"" ), placementIdentifier]; - if ( ![VungleAds isInitialized] ) - { - [self log: @"Vungle SDK not successfully initialized: failing app open ad load..."]; - [delegate didFailToLoadAppOpenAdWithError: MAAdapterError.notInitialized]; - - return; - } - - [self updateUserPrivacySettingsForParameters: parameters]; - - self.appOpenAdDelegate = [[ALVungleMediationAdapterAppOpenAdDelegate alloc] initWithParentAdapter: self andNotify: delegate]; - self.appOpenAd = [[VungleInterstitial alloc] initWithPlacementId: placementIdentifier]; - self.appOpenAd.delegate = self.appOpenAdDelegate; - - [self.appOpenAd load: bidResponse]; + [self initVungleSDK:parameters completionHandler:^(MAAdapterInitializationStatus initializationStatus, NSString * _Nullable errorMessage) { + if ( initializationStatus == MAAdapterInitializationStatusInitializedFailure ) + { + [self log: @"Vungle SDK not successfully initialized: failing app open ad load with error %@", errorMessage]; + [delegate didFailToLoadAppOpenAdWithError: MAAdapterError.notInitialized]; + } + else + { + [self updateUserPrivacySettingsForParameters: parameters]; + + self.appOpenAdDelegate = [[ALVungleMediationAdapterAppOpenAdDelegate alloc] initWithParentAdapter: self andNotify: delegate]; + self.appOpenAd = [[VungleInterstitial alloc] initWithPlacementId: placementIdentifier]; + self.appOpenAd.delegate = self.appOpenAdDelegate; + + [self.appOpenAd load: bidResponse]; + } + }]; } - (void)showAppOpenAdForParameters:(id)parameters andNotify:(id)delegate @@ -317,21 +359,23 @@ - (void)loadRewardedAdForParameters:(id)parameters NSString *placementIdentifier = parameters.thirdPartyAdPlacementIdentifier; [self log: @"Loading %@rewarded ad for placement: %@...", ( isBiddingAd ? @"bidding " : @"" ), placementIdentifier]; - if ( ![VungleAds isInitialized] ) - { - [self log: @"Vungle SDK not successfully initialized: failing rewarded ad load..."]; - [delegate didFailToLoadRewardedAdWithError: MAAdapterError.notInitialized]; - - return; - } - - [self updateUserPrivacySettingsForParameters: parameters]; - - self.rewardedAd = [[VungleRewarded alloc] initWithPlacementId: placementIdentifier]; - self.rewardedAdDelegate = [[ALVungleMediationAdapterRewardedAdDelegate alloc] initWithParentAdapter: self andNotify: delegate]; - self.rewardedAd.delegate = self.rewardedAdDelegate; - - [self.rewardedAd load: bidResponse]; + [self initVungleSDK:parameters completionHandler:^(MAAdapterInitializationStatus initializationStatus, NSString * _Nullable errorMessage) { + if ( initializationStatus == MAAdapterInitializationStatusInitializedFailure ) + { + [self log: @"Vungle SDK not successfully initialized: failing rewarded ad load with error %@", errorMessage]; + [delegate didFailToLoadRewardedAdWithError: MAAdapterError.notInitialized]; + } + else + { + [self updateUserPrivacySettingsForParameters: parameters]; + + self.rewardedAd = [[VungleRewarded alloc] initWithPlacementId: placementIdentifier]; + self.rewardedAdDelegate = [[ALVungleMediationAdapterRewardedAdDelegate alloc] initWithParentAdapter: self andNotify: delegate]; + self.rewardedAd.delegate = self.rewardedAdDelegate; + + [self.rewardedAd load: bidResponse]; + } + }]; } - (void)showRewardedAdForParameters:(id)parameters andNotify:(id)delegate @@ -382,39 +426,41 @@ - (void)loadAdViewAdForParameters:(id)parameters ad [self log: @"Loading %@%@%@ ad for placement: %@...", ( isBiddingAd ? @"bidding " : @"" ), ( isNative ? @"native " : @"" ), adFormatLabel, placementIdentifier]; - if ( ![VungleAds isInitialized] ) - { - [self log: @"Vungle SDK not successfully initialized: failing %@ ad load...", adFormatLabel]; - [delegate didFailToLoadAdViewAdWithError: MAAdapterError.notInitialized]; - - return; - } - - [self updateUserPrivacySettingsForParameters: parameters]; - - if ( isNative ) - { - self.nativeAdViewDelegate = [[ALVungleMediationAdapterNativeAdViewDelegate alloc] initWithParentAdapter: self - format: adFormat - parameters: parameters - andNotify: delegate]; - [self loadVungleNativeAdForParameters: parameters andNotify: self.nativeAdViewDelegate]; - } - else - { - BannerSize adSize = [self adSizeFromAdFormat: adFormat]; - - self.adView = [[VungleBanner alloc] initWithPlacementId: placementIdentifier size: adSize]; - self.adViewDelegate = [[ALVungleMediationAdapterAdViewDelegate alloc] initWithParentAdapter: self - format: adFormat - parameters: parameters - andNotify: delegate]; - self.adView.delegate = self.adViewDelegate; + [self initVungleSDK:parameters completionHandler:^(MAAdapterInitializationStatus initializationStatus, NSString * _Nullable errorMessage) { + if ( initializationStatus == MAAdapterInitializationStatusInitializedFailure ) + { + [self log: @"Vungle SDK not successfully initialized: failing %@ ad load with error %@", adFormatLabel, errorMessage]; + [delegate didFailToLoadAdViewAdWithError: MAAdapterError.notInitialized]; + } + else + { + [self updateUserPrivacySettingsForParameters: parameters]; + + if ( isNative ) + { + self.nativeAdViewDelegate = [[ALVungleMediationAdapterNativeAdViewDelegate alloc] initWithParentAdapter: self + format: adFormat + parameters: parameters + andNotify: delegate]; + [self loadVungleNativeAdForParameters: parameters andNotify: self.nativeAdViewDelegate]; + } + else + { + BannerSize adSize = [self adSizeFromAdFormat: adFormat]; - self.adViewContainer = [[UIView alloc] initWithFrame: (CGRect) { CGPointZero, adFormat.size }]; - - [self.adView load: bidResponse]; - } + self.adView = [[VungleBanner alloc] initWithPlacementId: placementIdentifier size: adSize]; + self.adViewDelegate = [[ALVungleMediationAdapterAdViewDelegate alloc] initWithParentAdapter: self + format: adFormat + parameters: parameters + andNotify: delegate]; + self.adView.delegate = self.adViewDelegate; + + self.adViewContainer = [[UIView alloc] initWithFrame: (CGRect) { CGPointZero, adFormat.size }]; + + [self.adView load: bidResponse]; + } + } + }]; } #pragma mark - MANativeAdAdapter Methods @@ -427,20 +473,22 @@ - (void)loadNativeAdForParameters:(id)parameters an [self log: @"Loading %@native ad for placement: %@...", ( isBiddingAd ? @"bidding " : @"" ), placementIdentifier]; - if ( ![VungleAds isInitialized] ) - { - [self log: @"Vungle SDK not successfully initialized: failing native ad load..."]; - [delegate didFailToLoadNativeAdWithError: MAAdapterError.notInitialized]; - - return; - } - - [self updateUserPrivacySettingsForParameters: parameters]; - - self.nativeAdDelegate = [[ALVungleMediationAdapterNativeAdDelegate alloc] initWithParentAdapter: self - parameters: parameters - andNotify: delegate]; - [self loadVungleNativeAdForParameters: parameters andNotify: self.nativeAdDelegate]; + [self initVungleSDK:parameters completionHandler:^(MAAdapterInitializationStatus initializationStatus, NSString * _Nullable errorMessage) { + if ( initializationStatus == MAAdapterInitializationStatusInitializedFailure ) + { + [self log: @"Vungle SDK not successfully initialized: failing native ad load with error %@", errorMessage]; + [delegate didFailToLoadNativeAdWithError: MAAdapterError.notInitialized]; + } + else + { + [self updateUserPrivacySettingsForParameters: parameters]; + + self.nativeAdDelegate = [[ALVungleMediationAdapterNativeAdDelegate alloc] initWithParentAdapter: self + parameters: parameters + andNotify: delegate]; + [self loadVungleNativeAdForParameters: parameters andNotify: self.nativeAdDelegate]; + } + }]; } #pragma mark - Shared Methods @@ -543,64 +591,64 @@ + (MAAdapterError *)toMaxError:(nullable NSError *)vungleError isAdPresentError: { if ( !vungleError ) return MAAdapterError.unspecified; - int vungleErrorCode = (int) vungleError.code; + VungleError vungleErrorCode = vungleError.code; MAAdapterError *adapterError = MAAdapterError.unspecified; switch ( vungleErrorCode ) { - case 6: // SDK Not Initialized + case VungleErrorSdkNotInitialized: adapterError = MAAdapterError.notInitialized; break; - case 2: // Invalid AppID - case 201: // Invalid PlacementID - case 207: // Invalid Placement Type - case 222: // Invalid Placement load type - case 500: // BannerView: Invalid Size - case 30001: // Ad Publisher Mismatch + case VungleErrorInvalidAppID: + case VungleErrorInvalidPlacementID: + case VungleErrorPlacementAdTypeMismatch: + case VungleErrorInvalidWaterfallPlacementID: + case VungleErrorBannerViewInvalidSize: + case VungleErrorAdPublisherMismatch: adapterError = MAAdapterError.invalidConfiguration; break; - case 119: // Json Encode Error - case 30002: // Ad Internal Integration Error + case VungleErrorJsonEncodeError: + case VungleErrorAdInternalIntegrationError: adapterError = MAAdapterError.internalError; break; - case 202: // Ad already Consumed - case 203: // Ad is already loading - case 204: // Ad already loaded - case 205: // Ad is playing - case 206: // Ad already failed loading + case VungleErrorAdConsumed: + case VungleErrorAdIsLoading: + case VungleErrorAdAlreadyLoaded: + case VungleErrorAdIsPlaying: + case VungleErrorAdAlreadyFailed: adapterError = MAAdapterError.invalidLoadState; break; - case 210: // Ad Not Loaded + case VungleErrorAdNotLoaded: adapterError = adPresentError ? MAAdapterError.adNotReady : MAAdapterError.invalidLoadState; break; - case 115: // Invalid IndexURL - case 302: // Invalid Ifa Status - case 305: // Mraid Bridge Error - case 400: // Concurrent Playback Unsupported + case VungleErrorInvalidIndexURL: + case VungleErrorInvalidIfaStatus: + case VungleErrorMraidBridgeError: + case VungleErrorConcurrentPlaybackUnsupported: adapterError = MAAdapterError.adDisplayFailedError; break; - case 212: // Placement Sleep - case 10001: // Ad No Fill - case 10002: // AdLoad Too Frequently + case VungleErrorPlacementSleep: + case VungleErrorAdNoFill: + case VungleErrorAdLoadTooFrequently: adapterError = MAAdapterError.noFill; break; - case 217: // Ad response timeOut + case VungleErrorAdResponseTimedOut: adapterError = MAAdapterError.timeout; break; - case 220: // Server busy with retry after timer. - case 221: // Load ad during Server busy with retry after timer. - case 20001: // Ad Server Error + case VungleErrorAdResponseRetryAfter: + case VungleErrorAdLoadFailRetryAfter: + case VungleErrorAdServerError: adapterError = MAAdapterError.serverError; break; - case 304: // Ad Expired - case 307: // Ad Expired on play call. + case VungleErrorAdExpired: + case VungleErrorAdExpiredOnPlay: adapterError = MAAdapterError.adExpiredError; break; - case 600: // Native Asset Error + case VungleErrorNativeAssetError: adapterError = MAAdapterError.missingRequiredNativeAdAssets; break; - case 2000: // webView WebContent Process Did Terminate - case 2001: // webView Failed Navigation + case VungleErrorWebViewWebContentProcessDidTerminate: + case VungleErrorWebViewFailedNavigation: adapterError = MAAdapterError.webViewError; break; }