12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051 |
- //
- // MAUnityAdManager.m
- // AppLovin MAX Unity Plugin
- //
- #import "MAUnityAdManager.h"
- #define KEY_WINDOW [UIApplication sharedApplication].keyWindow
- #define DEVICE_SPECIFIC_ADVIEW_AD_FORMAT ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPad) ? MAAdFormat.leader : MAAdFormat.banner
- #define IS_VERTICAL_BANNER_POSITION(_POS) ( [@"center_left" isEqual: adViewPosition] || [@"center_right" isEqual: adViewPosition] )
- #define DEGREES_TO_RADIANS(angle) ((angle) / 180.0 * M_PI)
- #ifdef __cplusplus
- extern "C" {
- #endif
-
- extern bool max_unity_should_disable_all_logs(void); // Forward declaration
- // UnityAppController.mm
- UIViewController* UnityGetGLViewController(void);
- UIWindow* UnityGetMainWindow(void);
-
- // life cycle management
- int UnityIsPaused(void);
- void UnityPause(int pause);
-
- void max_unity_dispatch_on_main_thread(dispatch_block_t block)
- {
- if ( block )
- {
- if ( [NSThread isMainThread] )
- {
- block();
- }
- else
- {
- dispatch_async(dispatch_get_main_queue(), block);
- }
- }
- }
- #ifdef __cplusplus
- }
- #endif
- @interface MAUnityAdManager()<MAAdDelegate, MAAdViewAdDelegate, MARewardedAdDelegate, MAAdRevenueDelegate, MAAdReviewDelegate, MAAdExpirationDelegate>
- // Parent Fields
- @property (nonatomic, weak) ALSdk *sdk;
- // Fullscreen Ad Fields
- @property (nonatomic, strong) NSMutableDictionary<NSString *, MAInterstitialAd *> *interstitials;
- @property (nonatomic, strong) NSMutableDictionary<NSString *, MAAppOpenAd *> *appOpenAds;
- @property (nonatomic, strong) NSMutableDictionary<NSString *, MARewardedAd *> *rewardedAds;
- // AdView Fields
- @property (nonatomic, strong) NSMutableDictionary<NSString *, MAAdView *> *adViews;
- @property (nonatomic, strong) NSMutableDictionary<NSString *, MAAdFormat *> *adViewAdFormats;
- @property (nonatomic, strong) NSMutableDictionary<NSString *, NSString *> *adViewPositions;
- @property (nonatomic, strong) NSMutableDictionary<NSString *, NSValue *> *adViewOffsets;
- @property (nonatomic, strong) NSMutableDictionary<NSString *, NSNumber *> *adViewWidths;
- @property (nonatomic, strong) NSMutableDictionary<NSString *, MAAdFormat *> *verticalAdViewFormats;
- @property (nonatomic, strong) NSMutableDictionary<NSString *, NSArray<NSLayoutConstraint *> *> *adViewConstraints;
- @property (nonatomic, strong) NSMutableDictionary<NSString *, NSMutableDictionary<NSString *, NSString *> *> *adViewExtraParametersToSetAfterCreate;
- @property (nonatomic, strong) NSMutableDictionary<NSString *, NSMutableDictionary<NSString *, id> *> *adViewLocalExtraParametersToSetAfterCreate;
- @property (nonatomic, strong) NSMutableDictionary<NSString *, NSString *> *adViewCustomDataToSetAfterCreate;
- @property (nonatomic, strong) NSMutableArray<NSString *> *adUnitIdentifiersToShowAfterCreate;
- @property (nonatomic, strong) NSMutableSet<NSString *> *disabledAdaptiveBannerAdUnitIdentifiers;
- @property (nonatomic, strong) NSMutableSet<NSString *> *disabledAutoRefreshAdViewAdUnitIdentifiers;
- @property (nonatomic, strong) NSMutableSet<NSString *> *ignoreSafeAreaLandscapeAdUnitIdentifiers;
- @property (nonatomic, strong) UIView *safeAreaBackground;
- @property (nonatomic, strong, nullable) UIColor *publisherBannerBackgroundColor;
- @property (nonatomic, strong) NSMutableDictionary<NSString *, MAAd *> *adInfoDict;
- @property (nonatomic, strong) NSObject *adInfoDictLock;
- @property (nonatomic, strong) NSOperationQueue *backgroundCallbackEventsQueue;
- @property (nonatomic, assign) BOOL resumeUnityAfterApplicationBecomesActive;
- @end
- // Internal
- @interface UIColor (ALUtils)
- + (nullable UIColor *)al_colorWithHexString:(NSString *)hexString;
- @end
- @interface NSNumber (ALUtils)
- + (NSNumber *)al_numberWithString:(NSString *)string;
- @end
- @interface NSString (ALUtils)
- @property (assign, readonly, getter=al_isValidString) BOOL al_validString;
- @end
- @interface MAAdFormat (ALUtils)
- @property (nonatomic, assign, readonly, getter=isFullscreenAd) BOOL fullscreenAd;
- @property (nonatomic, assign, readonly, getter=isAdViewAd) BOOL adViewAd;
- @end
- @implementation MAUnityAdManager
- static NSString *const SDK_TAG = @"AppLovinSdk";
- static NSString *const TAG = @"MAUnityAdManager";
- static NSString *const DEFAULT_AD_VIEW_POSITION = @"top_left";
- static ALUnityBackgroundCallback backgroundCallback;
- #pragma mark - Initialization
- - (instancetype)init
- {
- self = [super init];
- if ( self )
- {
- self.interstitials = [NSMutableDictionary dictionaryWithCapacity: 2];
- self.appOpenAds = [NSMutableDictionary dictionaryWithCapacity: 2];
- self.rewardedAds = [NSMutableDictionary dictionaryWithCapacity: 2];
- self.adViews = [NSMutableDictionary dictionaryWithCapacity: 2];
- self.adViewAdFormats = [NSMutableDictionary dictionaryWithCapacity: 2];
- self.adViewPositions = [NSMutableDictionary dictionaryWithCapacity: 2];
- self.adViewOffsets = [NSMutableDictionary dictionaryWithCapacity: 2];
- self.adViewWidths = [NSMutableDictionary dictionaryWithCapacity: 2];
- self.verticalAdViewFormats = [NSMutableDictionary dictionaryWithCapacity: 2];
- self.adViewConstraints = [NSMutableDictionary dictionaryWithCapacity: 2];
- self.adViewExtraParametersToSetAfterCreate = [NSMutableDictionary dictionaryWithCapacity: 1];
- self.adViewLocalExtraParametersToSetAfterCreate = [NSMutableDictionary dictionaryWithCapacity: 1];
- self.adViewCustomDataToSetAfterCreate = [NSMutableDictionary dictionaryWithCapacity: 1];
- self.adUnitIdentifiersToShowAfterCreate = [NSMutableArray arrayWithCapacity: 2];
- self.disabledAdaptiveBannerAdUnitIdentifiers = [NSMutableSet setWithCapacity: 2];
- self.disabledAutoRefreshAdViewAdUnitIdentifiers = [NSMutableSet setWithCapacity: 2];
- self.ignoreSafeAreaLandscapeAdUnitIdentifiers = [NSMutableSet setWithCapacity: 2];
- self.adInfoDict = [NSMutableDictionary dictionary];
- self.adInfoDictLock = [[NSObject alloc] init];
-
- self.backgroundCallbackEventsQueue = [[NSOperationQueue alloc] init];
- self.backgroundCallbackEventsQueue.maxConcurrentOperationCount = 1;
-
- max_unity_dispatch_on_main_thread(^{
- self.safeAreaBackground = [[UIView alloc] init];
- self.safeAreaBackground.hidden = YES;
- self.safeAreaBackground.backgroundColor = UIColor.clearColor;
- self.safeAreaBackground.translatesAutoresizingMaskIntoConstraints = NO;
- self.safeAreaBackground.userInteractionEnabled = NO;
-
- UIViewController *rootViewController = [self unityViewController];
- [rootViewController.view addSubview: self.safeAreaBackground];
- });
-
- // Enable orientation change listener, so that the position can be updated for vertical banners.
- [[NSNotificationCenter defaultCenter] addObserverForName: UIDeviceOrientationDidChangeNotification
- object: nil
- queue: [NSOperationQueue mainQueue]
- usingBlock:^(NSNotification *notification) {
-
- for ( NSString *adUnitIdentifier in self.verticalAdViewFormats )
- {
- [self positionAdViewForAdUnitIdentifier: adUnitIdentifier adFormat: self.verticalAdViewFormats[adUnitIdentifier]];
- }
- }];
-
- [[NSNotificationCenter defaultCenter] addObserver: self
- selector: @selector(applicationPaused:)
- name: UIApplicationDidEnterBackgroundNotification
- object: nil];
-
- [[NSNotificationCenter defaultCenter] addObserver: self
- selector: @selector(applicationResumed:)
- name: UIApplicationDidBecomeActiveNotification
- object: nil];
-
- [[NSNotificationCenter defaultCenter] addObserverForName: UIApplicationDidBecomeActiveNotification
- object: nil
- queue: [NSOperationQueue mainQueue]
- usingBlock:^(NSNotification *notification) {
-
- #if !IS_TEST_APP
- if ( self.resumeUnityAfterApplicationBecomesActive && UnityIsPaused() )
- {
- UnityPause(NO);
- }
- #endif
-
- self.backgroundCallbackEventsQueue.suspended = NO;
- }];
- }
- return self;
- }
- + (MAUnityAdManager *)shared
- {
- static dispatch_once_t token;
- static MAUnityAdManager *shared;
- dispatch_once(&token, ^{
- shared = [[MAUnityAdManager alloc] init];
- });
- return shared;
- }
- + (void)setUnityBackgroundCallback:(ALUnityBackgroundCallback)unityBackgroundCallback
- {
- backgroundCallback = unityBackgroundCallback;
- }
- #pragma mark - Plugin Initialization
- - (void)initializeSdkWithConfiguration:(ALSdkInitializationConfiguration *)initConfig andCompletionHandler:(ALSdkInitializationCompletionHandler)completionHandler;
- {
- self.sdk = [ALSdk shared];
- [self.sdk initializeWithConfiguration: initConfig completionHandler:^(ALSdkConfiguration *configuration) {
- dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
-
- // Note: internal state should be updated first
- completionHandler( configuration );
-
- NSString *consentFlowUserGeographyStr = @(configuration.consentFlowUserGeography).stringValue;
- NSString *consentDialogStateStr = @(configuration.consentDialogState).stringValue;
- NSString *appTrackingStatus = @(configuration.appTrackingTransparencyStatus).stringValue; // Deliberately name it `appTrackingStatus` to be a bit more generic (in case Android introduces a similar concept)
- [self forwardUnityEventWithArgs: @{@"name" : @"OnSdkInitializedEvent",
- @"consentFlowUserGeography" : consentFlowUserGeographyStr,
- @"consentDialogState" : consentDialogStateStr,
- @"countryCode" : configuration.countryCode,
- @"appTrackingStatus" : appTrackingStatus,
- @"isSuccessfullyInitialized" : @([self.sdk isInitialized]),
- @"isTestModeEnabled" : @([configuration isTestModeEnabled])}];
- });
- }];
- }
- #pragma mark - Banners
- - (void)createBannerWithAdUnitIdentifier:(nullable NSString *)adUnitIdentifier atPosition:(nullable NSString *)bannerPosition
- {
- [self createAdViewWithAdUnitIdentifier: adUnitIdentifier adFormat: [self adViewAdFormatForAdUnitIdentifier: adUnitIdentifier] atPosition: bannerPosition withOffset: CGPointZero];
- }
- - (void)createBannerWithAdUnitIdentifier:(nullable NSString *)adUnitIdentifier x:(CGFloat)xOffset y:(CGFloat)yOffset
- {
- [self createAdViewWithAdUnitIdentifier: adUnitIdentifier adFormat: [self adViewAdFormatForAdUnitIdentifier: adUnitIdentifier] atPosition: DEFAULT_AD_VIEW_POSITION withOffset: CGPointMake(xOffset, yOffset)];
- }
- - (void)loadBannerWithAdUnitIdentifier:(nullable NSString *)adUnitIdentifier
- {
- [self loadAdViewWithAdUnitIdentifier: adUnitIdentifier adFormat: [self adViewAdFormatForAdUnitIdentifier: adUnitIdentifier]];
- }
- - (void)setBannerBackgroundColorForAdUnitIdentifier:(nullable NSString *)adUnitIdentifier hexColorCode:(nullable NSString *)hexColorCode
- {
- [self setAdViewBackgroundColorForAdUnitIdentifier: adUnitIdentifier adFormat: [self adViewAdFormatForAdUnitIdentifier: adUnitIdentifier] hexColorCode: hexColorCode];
- }
- - (void)setBannerPlacement:(nullable NSString *)placement forAdUnitIdentifier:(nullable NSString *)adUnitIdentifier
- {
- [self setAdViewPlacement: placement forAdUnitIdentifier: adUnitIdentifier adFormat: [self adViewAdFormatForAdUnitIdentifier: adUnitIdentifier]];
- }
- - (void)startBannerAutoRefreshForAdUnitIdentifier:(nullable NSString *)adUnitIdentifier
- {
- [self startAdViewAutoRefreshForAdUnitIdentifier: adUnitIdentifier adFormat: [self adViewAdFormatForAdUnitIdentifier: adUnitIdentifier]];
- }
- - (void)stopBannerAutoRefreshForAdUnitIdentifier:(nullable NSString *)adUnitIdentifier
- {
- [self stopAdViewAutoRefreshForAdUnitIdentifier: adUnitIdentifier adFormat: [self adViewAdFormatForAdUnitIdentifier: adUnitIdentifier]];
- }
- - (void)setBannerWidth:(CGFloat)width forAdUnitIdentifier:(nullable NSString *)adUnitIdentifier
- {
- [self setAdViewWidth: width forAdUnitIdentifier: adUnitIdentifier adFormat: [self adViewAdFormatForAdUnitIdentifier: adUnitIdentifier]];
- }
- - (void)updateBannerPosition:(nullable NSString *)bannerPosition forAdUnitIdentifier:(nullable NSString *)adUnitIdentifier
- {
- [self updateAdViewPosition: bannerPosition withOffset: CGPointZero forAdUnitIdentifier: adUnitIdentifier adFormat: [self adViewAdFormatForAdUnitIdentifier: adUnitIdentifier]];
- }
- - (void)updateBannerPosition:(CGFloat)xOffset y:(CGFloat)yOffset forAdUnitIdentifier:(nullable NSString *)adUnitIdentifier
- {
- [self updateAdViewPosition: DEFAULT_AD_VIEW_POSITION withOffset: CGPointMake(xOffset, yOffset) forAdUnitIdentifier: adUnitIdentifier adFormat: [self adViewAdFormatForAdUnitIdentifier: adUnitIdentifier]];
- }
- - (void)setBannerExtraParameterForAdUnitIdentifier:(nullable NSString *)adUnitIdentifier key:(nullable NSString *)key value:(nullable NSString *)value
- {
- [self setAdViewExtraParameterForAdUnitIdentifier: adUnitIdentifier adFormat: [self adViewAdFormatForAdUnitIdentifier: adUnitIdentifier] key: key value: value];
- }
- - (void)setBannerLocalExtraParameterForAdUnitIdentifier:(nullable NSString *)adUnitIdentifier key:(nullable NSString *)key value:(nullable id)value
- {
- if ( !key )
- {
- [self log: @"Failed to set local extra parameter: No key specified"];
- return;
- }
-
- [self setAdViewLocalExtraParameterForAdUnitIdentifier: adUnitIdentifier adFormat: [self adViewAdFormatForAdUnitIdentifier: adUnitIdentifier] key: key value: value];
- }
- - (void)setBannerCustomData:(nullable NSString *)customData forAdUnitIdentifier:(nullable NSString *)adUnitIdentifier
- {
- [self setAdViewCustomData: customData forAdUnitIdentifier: adUnitIdentifier adFormat: [self adViewAdFormatForAdUnitIdentifier: adUnitIdentifier]];
- }
- - (void)showBannerWithAdUnitIdentifier:(nullable NSString *)adUnitIdentifier
- {
- [self showAdViewWithAdUnitIdentifier: adUnitIdentifier adFormat: [self adViewAdFormatForAdUnitIdentifier: adUnitIdentifier]];
- }
- - (void)hideBannerWithAdUnitIdentifier:(nullable NSString *)adUnitIdentifier
- {
- [self hideAdViewWithAdUnitIdentifier: adUnitIdentifier adFormat: [self adViewAdFormatForAdUnitIdentifier: adUnitIdentifier]];
- }
- - (NSString *)bannerLayoutForAdUnitIdentifier:(nullable NSString *)adUnitIdentifier
- {
- return [self adViewLayoutForAdUnitIdentifier: adUnitIdentifier adFormat: [self adViewAdFormatForAdUnitIdentifier: adUnitIdentifier]];
- }
- - (void)destroyBannerWithAdUnitIdentifier:(nullable NSString *)adUnitIdentifier
- {
- [self destroyAdViewWithAdUnitIdentifier: adUnitIdentifier adFormat: [self adViewAdFormatForAdUnitIdentifier: adUnitIdentifier]];
- }
- + (CGFloat)adaptiveBannerHeightForWidth:(CGFloat)width
- {
- return [DEVICE_SPECIFIC_ADVIEW_AD_FORMAT adaptiveSizeForWidth: width].height;
- }
- #pragma mark - MRECs
- - (void)createMRecWithAdUnitIdentifier:(nullable NSString *)adUnitIdentifier atPosition:(nullable NSString *)mrecPosition
- {
- [self createAdViewWithAdUnitIdentifier: adUnitIdentifier adFormat: MAAdFormat.mrec atPosition: mrecPosition withOffset: CGPointZero];
- }
- - (void)createMRecWithAdUnitIdentifier:(nullable NSString *)adUnitIdentifier x:(CGFloat)xOffset y:(CGFloat)yOffset
- {
- [self createAdViewWithAdUnitIdentifier: adUnitIdentifier adFormat: MAAdFormat.mrec atPosition: DEFAULT_AD_VIEW_POSITION withOffset: CGPointMake(xOffset, yOffset)];
- }
- - (void)loadMRecWithAdUnitIdentifier:(nullable NSString *)adUnitIdentifier
- {
- [self loadAdViewWithAdUnitIdentifier: adUnitIdentifier adFormat: MAAdFormat.mrec];
- }
- - (void)setMRecPlacement:(nullable NSString *)placement forAdUnitIdentifier:(nullable NSString *)adUnitIdentifier
- {
- [self setAdViewPlacement: placement forAdUnitIdentifier: adUnitIdentifier adFormat: MAAdFormat.mrec];
- }
- - (void)startMRecAutoRefreshForAdUnitIdentifier:(nullable NSString *)adUnitIdentifier
- {
- [self startAdViewAutoRefreshForAdUnitIdentifier: adUnitIdentifier adFormat: MAAdFormat.mrec];
- }
- - (void)stopMRecAutoRefreshForAdUnitIdentifier:(nullable NSString *)adUnitIdentifier
- {
- [self stopAdViewAutoRefreshForAdUnitIdentifier: adUnitIdentifier adFormat: MAAdFormat.mrec];
- }
- - (void)updateMRecPosition:(nullable NSString *)mrecPosition forAdUnitIdentifier:(nullable NSString *)adUnitIdentifier
- {
- [self updateAdViewPosition: mrecPosition withOffset: CGPointZero forAdUnitIdentifier: adUnitIdentifier adFormat: MAAdFormat.mrec];
- }
- - (void)updateMRecPosition:(CGFloat)xOffset y:(CGFloat)yOffset forAdUnitIdentifier:(nullable NSString *)adUnitIdentifier
- {
- [self updateAdViewPosition: DEFAULT_AD_VIEW_POSITION withOffset: CGPointMake(xOffset, yOffset) forAdUnitIdentifier: adUnitIdentifier adFormat: MAAdFormat.mrec];
- }
- - (void)setMRecExtraParameterForAdUnitIdentifier:(nullable NSString *)adUnitIdentifier key:(nullable NSString *)key value:(nullable NSString *)value
- {
- [self setAdViewExtraParameterForAdUnitIdentifier: adUnitIdentifier adFormat: MAAdFormat.mrec key: key value: value];
- }
- - (void)setMRecLocalExtraParameterForAdUnitIdentifier:(nullable NSString *)adUnitIdentifier key:(nullable NSString *)key value:(nullable id)value
- {
- if ( !key )
- {
- [self log: @"Failed to set local extra parameter: No key specified"];
- return;
- }
-
- [self setAdViewLocalExtraParameterForAdUnitIdentifier: adUnitIdentifier adFormat: MAAdFormat.mrec key: key value: value];
- }
- - (void)setMRecCustomData:(nullable NSString *)customData forAdUnitIdentifier:(nullable NSString *)adUnitIdentifier;
- {
- [self setAdViewCustomData: customData forAdUnitIdentifier: adUnitIdentifier adFormat: MAAdFormat.mrec];
- }
- - (void)showMRecWithAdUnitIdentifier:(nullable NSString *)adUnitIdentifier
- {
- [self showAdViewWithAdUnitIdentifier: adUnitIdentifier adFormat: MAAdFormat.mrec];
- }
- - (void)destroyMRecWithAdUnitIdentifier:(nullable NSString *)adUnitIdentifier
- {
- [self destroyAdViewWithAdUnitIdentifier: adUnitIdentifier adFormat: MAAdFormat.mrec];
- }
- - (void)hideMRecWithAdUnitIdentifier:(nullable NSString *)adUnitIdentifier
- {
- [self hideAdViewWithAdUnitIdentifier: adUnitIdentifier adFormat: MAAdFormat.mrec];
- }
- - (NSString *)mrecLayoutForAdUnitIdentifier:(nullable NSString *)adUnitIdentifier
- {
- return [self adViewLayoutForAdUnitIdentifier: adUnitIdentifier adFormat: MAAdFormat.mrec];
- }
- #pragma mark - Interstitials
- - (void)loadInterstitialWithAdUnitIdentifier:(nullable NSString *)adUnitIdentifier
- {
- MAInterstitialAd *interstitial = [self retrieveInterstitialForAdUnitIdentifier: adUnitIdentifier];
- [interstitial loadAd];
- }
- - (BOOL)isInterstitialReadyWithAdUnitIdentifier:(nullable NSString *)adUnitIdentifier
- {
- MAInterstitialAd *interstitial = [self retrieveInterstitialForAdUnitIdentifier: adUnitIdentifier];
- return [interstitial isReady];
- }
- - (void)showInterstitialWithAdUnitIdentifier:(nullable NSString *)adUnitIdentifier placement:(nullable NSString *)placement customData:(nullable NSString *)customData
- {
- MAInterstitialAd *interstitial = [self retrieveInterstitialForAdUnitIdentifier: adUnitIdentifier];
- [interstitial showAdForPlacement: placement customData: customData];
- }
- - (void)setInterstitialExtraParameterForAdUnitIdentifier:(nullable NSString *)adUnitIdentifier key:(nullable NSString *)key value:(nullable NSString *)value
- {
- MAInterstitialAd *interstitial = [self retrieveInterstitialForAdUnitIdentifier: adUnitIdentifier];
- [interstitial setExtraParameterForKey: key value: value];
- }
- - (void)setInterstitialLocalExtraParameterForAdUnitIdentifier:(nullable NSString *)adUnitIdentifier key:(nullable NSString *)key value:(nullable id)value
- {
- if ( !key )
- {
- [self log: @"Failed to set local extra parameter: No key specified"];
- return;
- }
-
- MAInterstitialAd *interstitial = [self retrieveInterstitialForAdUnitIdentifier: adUnitIdentifier];
- [interstitial setLocalExtraParameterForKey: key value: value];
- }
- #pragma mark - App Open Ads
- - (void)loadAppOpenAdWithAdUnitIdentifier:(nullable NSString *)adUnitIdentifier
- {
- MAAppOpenAd *appOpenAd = [self retrieveAppOpenAdForAdUnitIdentifier: adUnitIdentifier];
- [appOpenAd loadAd];
- }
- - (BOOL)isAppOpenAdReadyWithAdUnitIdentifier:(nullable NSString *)adUnitIdentifier
- {
- MAAppOpenAd *appOpenAd = [self retrieveAppOpenAdForAdUnitIdentifier: adUnitIdentifier];
- return [appOpenAd isReady];
- }
- - (void)showAppOpenAdWithAdUnitIdentifier:(nullable NSString *)adUnitIdentifier placement:(nullable NSString *)placement customData:(nullable NSString *)customData
- {
- MAAppOpenAd *appOpenAd = [self retrieveAppOpenAdForAdUnitIdentifier: adUnitIdentifier];
- [appOpenAd showAdForPlacement: placement customData: customData];
- }
- - (void)setAppOpenAdExtraParameterForAdUnitIdentifier:(nullable NSString *)adUnitIdentifier key:(nullable NSString *)key value:(nullable NSString *)value
- {
- MAAppOpenAd *appOpenAd = [self retrieveAppOpenAdForAdUnitIdentifier: adUnitIdentifier];
- [appOpenAd setExtraParameterForKey: key value: value];
- }
- - (void)setAppOpenAdLocalExtraParameterForAdUnitIdentifier:(nullable NSString *)adUnitIdentifier key:(nullable NSString *)key value:(nullable id)value
- {
- if ( !key )
- {
- [self log: @"Failed to set local extra parameter: No key specified"];
- return;
- }
-
- MAAppOpenAd *appOpenAd = [self retrieveAppOpenAdForAdUnitIdentifier: adUnitIdentifier];
- [appOpenAd setLocalExtraParameterForKey: key value: value];
- }
- #pragma mark - Rewarded
- - (void)loadRewardedAdWithAdUnitIdentifier:(nullable NSString *)adUnitIdentifier
- {
- MARewardedAd *rewardedAd = [self retrieveRewardedAdForAdUnitIdentifier: adUnitIdentifier];
- [rewardedAd loadAd];
- }
- - (BOOL)isRewardedAdReadyWithAdUnitIdentifier:(nullable NSString *)adUnitIdentifier
- {
- MARewardedAd *rewardedAd = [self retrieveRewardedAdForAdUnitIdentifier: adUnitIdentifier];
- return [rewardedAd isReady];
- }
- - (void)showRewardedAdWithAdUnitIdentifier:(nullable NSString *)adUnitIdentifier placement:(nullable NSString *)placement customData:(nullable NSString *)customData
- {
- MARewardedAd *rewardedAd = [self retrieveRewardedAdForAdUnitIdentifier: adUnitIdentifier];
- [rewardedAd showAdForPlacement: placement customData: customData];
- }
- - (void)setRewardedAdExtraParameterForAdUnitIdentifier:(nullable NSString *)adUnitIdentifier key:(nullable NSString *)key value:(nullable NSString *)value
- {
- MARewardedAd *rewardedAd = [self retrieveRewardedAdForAdUnitIdentifier: adUnitIdentifier];
- [rewardedAd setExtraParameterForKey: key value: value];
- }
- - (void)setRewardedAdLocalExtraParameterForAdUnitIdentifier:(nullable NSString *)adUnitIdentifier key:(nullable NSString *)key value:(nullable id)value;
- {
- if ( !key )
- {
- [self log: @"Failed to set local extra parameter: No key specified"];
- return;
- }
-
- MARewardedAd *rewardedAd = [self retrieveRewardedAdForAdUnitIdentifier: adUnitIdentifier];
- [rewardedAd setLocalExtraParameterForKey: key value: value];
- }
- #pragma mark - Event Tracking
- - (void)trackEvent:(nullable NSString *)event parameters:(nullable NSString *)parameters
- {
- NSDictionary<NSString *, id> *deserializedParameters = [MAUnityAdManager deserializeParameters: parameters];
- [self.sdk.eventService trackEvent: event parameters: deserializedParameters];
- }
- #pragma mark - Ad Info
- - (NSDictionary<NSString *, id> *)adInfoForAd:(MAAd *)ad
- {
- return @{@"adUnitId" : ad.adUnitIdentifier,
- @"adFormat" : ad.format.label,
- @"networkName" : ad.networkName,
- @"networkPlacement" : ad.networkPlacement,
- @"creativeId" : ad.creativeIdentifier ?: @"",
- @"placement" : ad.placement ?: @"",
- @"revenue" : [@(ad.revenue) stringValue],
- @"revenuePrecision" : ad.revenuePrecision,
- @"waterfallInfo" : [self createAdWaterfallInfo: ad.waterfall],
- @"latencyMillis" : [self requestLatencyMillisFromRequestLatency: ad.requestLatency],
- @"dspName" : ad.DSPName ?: @""};
- }
- #pragma mark - Waterfall Information
- - (NSDictionary<NSString *, id> *)createAdWaterfallInfo:(MAAdWaterfallInfo *)waterfallInfo
- {
- NSMutableDictionary<NSString *, NSObject *> *waterfallInfoDict = [NSMutableDictionary dictionary];
- if ( !waterfallInfo ) return waterfallInfoDict;
-
- waterfallInfoDict[@"name"] = waterfallInfo.name;
- waterfallInfoDict[@"testName"] = waterfallInfo.testName;
-
- NSMutableArray<NSDictionary<NSString *, NSObject *> *> *networkResponsesArray = [NSMutableArray arrayWithCapacity: waterfallInfo.networkResponses.count];
- for ( MANetworkResponseInfo *response in waterfallInfo.networkResponses )
- {
- [networkResponsesArray addObject: [self createNetworkResponseInfo: response]];
- }
-
- waterfallInfoDict[@"networkResponses"] = networkResponsesArray;
- waterfallInfoDict[@"latencyMillis"] = [self requestLatencyMillisFromRequestLatency: waterfallInfo.latency];
-
- return waterfallInfoDict;
- }
- - (NSDictionary<NSString *, id> *)createNetworkResponseInfo:(MANetworkResponseInfo *)response
- {
- NSMutableDictionary<NSString *, NSObject *> *networkResponseDict = [NSMutableDictionary dictionary];
-
- networkResponseDict[@"adLoadState"] = @(response.adLoadState).stringValue;
-
- MAMediatedNetworkInfo *mediatedNetworkInfo = response.mediatedNetwork;
- if ( mediatedNetworkInfo )
- {
- NSMutableDictionary <NSString *, NSObject *> *networkInfoObject = [NSMutableDictionary dictionary];
- networkInfoObject[@"name"] = response.mediatedNetwork.name;
- networkInfoObject[@"adapterClassName"] = response.mediatedNetwork.adapterClassName;
- networkInfoObject[@"adapterVersion"] = response.mediatedNetwork.adapterVersion;
- networkInfoObject[@"sdkVersion"] = response.mediatedNetwork.sdkVersion;
- networkInfoObject[@"initializationStatus"] = @(response.mediatedNetwork.initializationStatus);
-
- networkResponseDict[@"mediatedNetwork"] = networkInfoObject;
- }
-
- networkResponseDict[@"credentials"] = response.credentials;
- networkResponseDict[@"isBidding"] = @([response isBidding]);
-
- MAError *error = response.error;
- if ( error )
- {
- NSMutableDictionary<NSString *, NSObject *> *errorObject = [NSMutableDictionary dictionary];
- errorObject[@"errorMessage"] = error.message;
- errorObject[@"adLoadFailure"] = error.adLoadFailureInfo;
- errorObject[@"errorCode"] = @(error.code).stringValue;
- errorObject[@"latencyMillis"] = [self requestLatencyMillisFromRequestLatency: error.requestLatency];
-
- networkResponseDict[@"error"] = errorObject;
- }
-
- networkResponseDict[@"latencyMillis"] = [self requestLatencyMillisFromRequestLatency: response.latency];
-
- return networkResponseDict;
- }
- #pragma mark - Ad Value
- - (NSString *)adValueForAdUnitIdentifier:(nullable NSString *)adUnitIdentifier withKey:(nullable NSString *)key
- {
- if ( adUnitIdentifier == nil || adUnitIdentifier.length == 0 ) return @"";
- if ( key == nil || key.length == 0 ) return @"";
-
- MAAd *ad = [self adWithAdUnitIdentifier: adUnitIdentifier];
- if ( !ad ) return @"";
-
- return [ad adValueForKey: key];
- }
- #pragma mark - Ad Callbacks
- - (void)didLoadAd:(MAAd *)ad
- {
- NSString *name;
- MAAdFormat *adFormat = ad.format;
- if ( [adFormat isAdViewAd] )
- {
- MAAdView *adView = [self retrieveAdViewForAdUnitIdentifier: ad.adUnitIdentifier adFormat: adFormat];
- // An ad is now being shown, enable user interaction.
- adView.userInteractionEnabled = YES;
-
- if ( MAAdFormat.mrec == adFormat )
- {
- name = @"OnMRecAdLoadedEvent";
- }
- else
- {
- name = @"OnBannerAdLoadedEvent";
- }
- [self positionAdViewForAd: ad];
-
- // Do not auto-refresh by default if the ad view is not showing yet (e.g. first load during app launch and publisher does not automatically show banner upon load success)
- // We will resume auto-refresh in -[MAUnityAdManager showBannerWithAdUnitIdentifier:].
- if ( adView && [adView isHidden] )
- {
- [adView stopAutoRefresh];
- }
- }
- else if ( MAAdFormat.interstitial == adFormat )
- {
- name = @"OnInterstitialLoadedEvent";
- }
- else if ( MAAdFormat.appOpen == adFormat )
- {
- name = @"OnAppOpenAdLoadedEvent";
- }
- else if ( MAAdFormat.rewarded == adFormat )
- {
- name = @"OnRewardedAdLoadedEvent";
- }
- else
- {
- [self logInvalidAdFormat: adFormat];
- return;
- }
-
- dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
-
- @synchronized ( self.adInfoDictLock )
- {
- self.adInfoDict[ad.adUnitIdentifier] = ad;
- }
-
- NSDictionary<NSString *, id> *args = [self defaultAdEventParametersForName: name withAd: ad];
- [self forwardUnityEventWithArgs: args];
- });
- }
- - (void)didFailToLoadAdForAdUnitIdentifier:(NSString *)adUnitIdentifier withError:(MAError *)error
- {
- dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
-
- if ( !adUnitIdentifier )
- {
- [self log: @"adUnitIdentifier cannot be nil from %@", [NSThread callStackSymbols]];
- return;
- }
-
- NSString *name;
- if ( self.adViews[adUnitIdentifier] )
- {
- MAAdFormat *adFormat = self.adViewAdFormats[adUnitIdentifier];
- if ( MAAdFormat.mrec == adFormat )
- {
- name = @"OnMRecAdLoadFailedEvent";
- }
- else
- {
- name = @"OnBannerAdLoadFailedEvent";
- }
- }
- else if ( self.interstitials[adUnitIdentifier] )
- {
- name = @"OnInterstitialLoadFailedEvent";
- }
- else if ( self.appOpenAds[adUnitIdentifier] )
- {
- name = @"OnAppOpenAdLoadFailedEvent";
- }
- else if ( self.rewardedAds[adUnitIdentifier] )
- {
- name = @"OnRewardedAdLoadFailedEvent";
- }
- else
- {
- [self log: @"invalid adUnitId from %@", [NSThread callStackSymbols]];
- return;
- }
-
- @synchronized ( self.adInfoDictLock )
- {
- [self.adInfoDict removeObjectForKey: adUnitIdentifier];
- }
-
- [self forwardUnityEventWithArgs: @{@"name" : name,
- @"adUnitId" : adUnitIdentifier,
- @"errorCode" : [@(error.code) stringValue],
- @"errorMessage" : error.message,
- @"waterfallInfo" : [self createAdWaterfallInfo: error.waterfall],
- @"adLoadFailureInfo" : error.adLoadFailureInfo ?: @"",
- @"latencyMillis" : [self requestLatencyMillisFromRequestLatency: error.requestLatency]}];
- });
- }
- - (void)didClickAd:(MAAd *)ad
- {
- dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
-
- NSString *name;
- MAAdFormat *adFormat = ad.format;
- if ( MAAdFormat.banner == adFormat || MAAdFormat.leader == adFormat )
- {
- name = @"OnBannerAdClickedEvent";
- }
- else if ( MAAdFormat.mrec == adFormat )
- {
- name = @"OnMRecAdClickedEvent";
- }
- else if ( MAAdFormat.interstitial == adFormat )
- {
- name = @"OnInterstitialClickedEvent";
- }
- else if ( MAAdFormat.appOpen == adFormat )
- {
- name = @"OnAppOpenAdClickedEvent";
- }
- else if ( MAAdFormat.rewarded == adFormat )
- {
- name = @"OnRewardedAdClickedEvent";
- }
- else
- {
- [self logInvalidAdFormat: adFormat];
- return;
- }
-
- NSDictionary<NSString *, id> *args = [self defaultAdEventParametersForName: name withAd: ad];
- [self forwardUnityEventWithArgs: args];
- });
- }
- - (void)didDisplayAd:(MAAd *)ad
- {
- // BMLs do not support [DISPLAY] events in Unity
- MAAdFormat *adFormat = ad.format;
- if ( ![adFormat isFullscreenAd] ) return;
-
- #if !IS_TEST_APP
- // UnityPause needs to be called on the main thread.
- UnityPause(YES);
- #endif
-
- dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
-
- NSString *name;
- if ( MAAdFormat.interstitial == adFormat )
- {
- name = @"OnInterstitialDisplayedEvent";
- }
- else if ( MAAdFormat.appOpen == adFormat )
- {
- name = @"OnAppOpenAdDisplayedEvent";
- }
- else // rewarded
- {
- name = @"OnRewardedAdDisplayedEvent";
- }
-
- NSDictionary<NSString *, id> *args = [self defaultAdEventParametersForName: name withAd: ad];
- [self forwardUnityEventWithArgs: args];
- });
- }
- - (void)didFailToDisplayAd:(MAAd *)ad withError:(MAError *)error
- {
- dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
-
- // BMLs do not support [DISPLAY] events in Unity
- MAAdFormat *adFormat = ad.format;
- if ( ![adFormat isFullscreenAd] ) return;
-
- NSString *name;
- if ( MAAdFormat.interstitial == adFormat )
- {
- name = @"OnInterstitialAdFailedToDisplayEvent";
- }
- else if ( MAAdFormat.appOpen == adFormat )
- {
- name = @"OnAppOpenAdFailedToDisplayEvent";
- }
- else // rewarded
- {
- name = @"OnRewardedAdFailedToDisplayEvent";
- }
-
- NSMutableDictionary<NSString *, id> *args = [self defaultAdEventParametersForName: name withAd: ad];
- args[@"errorCode"] = [@(error.code) stringValue];
- args[@"errorMessage"] = error.message;
- args[@"mediatedNetworkErrorCode"] = [@(error.mediatedNetworkErrorCode) stringValue];
- args[@"mediatedNetworkErrorMessage"] = error.mediatedNetworkErrorMessage;
- args[@"waterfallInfo"] = [self createAdWaterfallInfo: error.waterfall];
- args[@"latencyMillis"] = [self requestLatencyMillisFromRequestLatency: error.requestLatency];
- [self forwardUnityEventWithArgs: args];
- });
- }
- - (void)didHideAd:(MAAd *)ad
- {
- // BMLs do not support [HIDDEN] events in Unity
- MAAdFormat *adFormat = ad.format;
- if ( ![adFormat isFullscreenAd] ) return;
-
- #if !IS_TEST_APP
- extern bool _didResignActive;
- if ( _didResignActive )
- {
- // If the application is not active, we should wait until application becomes active to resume unity.
- self.resumeUnityAfterApplicationBecomesActive = YES;
- }
- else
- {
- // UnityPause needs to be called on the main thread.
- UnityPause(NO);
- }
- #endif
-
- dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
-
- NSString *name;
- if ( MAAdFormat.interstitial == adFormat )
- {
- name = @"OnInterstitialHiddenEvent";
- }
- else if ( MAAdFormat.appOpen == adFormat )
- {
- name = @"OnAppOpenAdHiddenEvent";
- }
- else // rewarded
- {
- name = @"OnRewardedAdHiddenEvent";
- }
-
- NSDictionary<NSString *, id> *args = [self defaultAdEventParametersForName: name withAd: ad];
- [self forwardUnityEventWithArgs: args];
- });
- }
- - (void)didExpandAd:(MAAd *)ad
- {
- MAAdFormat *adFormat = ad.format;
- if ( ![adFormat isAdViewAd] )
- {
- [self logInvalidAdFormat: adFormat];
- return;
- }
-
- #if !IS_TEST_APP
- // UnityPause needs to be called on the main thread.
- UnityPause(YES);
- #endif
-
- dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
-
- NSString *name;
- if ( MAAdFormat.mrec == adFormat )
- {
- name = @"OnMRecAdExpandedEvent";
- }
- else
- {
- name = @"OnBannerAdExpandedEvent";
- }
-
- NSDictionary<NSString *, id> *args = [self defaultAdEventParametersForName: name withAd: ad];
- [self forwardUnityEventWithArgs: args];
- });
- }
- - (void)didCollapseAd:(MAAd *)ad
- {
- MAAdFormat *adFormat = ad.format;
- if ( ![adFormat isAdViewAd] )
- {
- [self logInvalidAdFormat: adFormat];
- return;
- }
-
- #if !IS_TEST_APP
- extern bool _didResignActive;
- if ( _didResignActive )
- {
- // If the application is not active, we should wait until application becomes active to resume unity.
- self.resumeUnityAfterApplicationBecomesActive = YES;
- }
- else
- {
- // UnityPause needs to be called on the main thread.
- UnityPause(NO);
- }
- #endif
-
- dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
-
- NSString *name;
- if ( MAAdFormat.mrec == adFormat )
- {
- name = @"OnMRecAdCollapsedEvent";
- }
- else
- {
- name = @"OnBannerAdCollapsedEvent";
- }
-
- NSDictionary<NSString *, id> *args = [self defaultAdEventParametersForName: name withAd: ad];
- [self forwardUnityEventWithArgs: args];
- });
- }
- - (void)didRewardUserForAd:(MAAd *)ad withReward:(MAReward *)reward
- {
- dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
-
- MAAdFormat *adFormat = ad.format;
- if ( adFormat != MAAdFormat.rewarded )
- {
- [self logInvalidAdFormat: adFormat];
- return;
- }
-
- NSString *rewardLabel = reward ? reward.label : @"";
- NSInteger rewardAmountInt = reward ? reward.amount : 0;
- NSString *rewardAmount = [@(rewardAmountInt) stringValue];
- NSString *name = @"OnRewardedAdReceivedRewardEvent";
-
- NSMutableDictionary<NSString *, id> *args = [self defaultAdEventParametersForName: name withAd: ad];
- args[@"rewardLabel"] = rewardLabel;
- args[@"rewardAmount"] = rewardAmount;
- [self forwardUnityEventWithArgs: args];
- });
- }
- - (void)didPayRevenueForAd:(MAAd *)ad
- {
- dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
-
- NSString *name;
- MAAdFormat *adFormat = ad.format;
- if ( MAAdFormat.banner == adFormat || MAAdFormat.leader == adFormat )
- {
- name = @"OnBannerAdRevenuePaidEvent";
- }
- else if ( MAAdFormat.mrec == adFormat )
- {
- name = @"OnMRecAdRevenuePaidEvent";
- }
- else if ( MAAdFormat.interstitial == adFormat )
- {
- name = @"OnInterstitialAdRevenuePaidEvent";
- }
- else if ( MAAdFormat.appOpen == adFormat )
- {
- name = @"OnAppOpenAdRevenuePaidEvent";
- }
- else if ( MAAdFormat.rewarded == adFormat )
- {
- name = @"OnRewardedAdRevenuePaidEvent";
- }
- else
- {
- [self logInvalidAdFormat: adFormat];
- return;
- }
-
- NSMutableDictionary<NSString *, id> *args = [self defaultAdEventParametersForName: name withAd: ad];
- args[@"keepInBackground"] = @([adFormat isFullscreenAd]);
- [self forwardUnityEventWithArgs: args];
- });
- }
- - (void)didReloadExpiredAd:(MAAd *)expiredAd withNewAd:(MAAd *)newAd;
- {
- dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
-
- NSString *name;
- MAAdFormat *adFormat = newAd.format;
- if ( MAAdFormat.interstitial == adFormat )
- {
- name = @"OnExpiredInterstitialAdReloadedEvent";
- }
- else if ( MAAdFormat.appOpen == adFormat )
- {
- name = @"OnExpiredAppOpenAdReloadedEvent";
- }
- else if ( MAAdFormat.rewarded == adFormat )
- {
- name = @"OnExpiredRewardedAdReloadedEvent ";
- }
- else
- {
- [self logInvalidAdFormat: adFormat];
- return;
- }
-
- @synchronized ( self.adInfoDictLock )
- {
- self.adInfoDict[newAd.adUnitIdentifier] = newAd;
- }
-
- NSMutableDictionary<NSString *, NSObject *> *args = [NSMutableDictionary dictionary];
- args[@"expiredAdInfo"] = [self adInfoForAd: expiredAd];
- args[@"newAdInfo"] = [self adInfoForAd: newAd];
- args[@"name"] = name;
- [self forwardUnityEventWithArgs: args];
- });
- }
- - (void)didGenerateCreativeIdentifier:(NSString *)creativeIdentifier forAd:(MAAd *)ad
- {
- dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
-
- NSString *name;
- MAAdFormat *adFormat = ad.format;
- if ( MAAdFormat.banner == adFormat || MAAdFormat.leader == adFormat )
- {
- name = @"OnBannerAdReviewCreativeIdGeneratedEvent";
- }
- else if ( MAAdFormat.mrec == adFormat )
- {
- name = @"OnMRecAdReviewCreativeIdGeneratedEvent";
- }
- else if ( MAAdFormat.interstitial == adFormat )
- {
- name = @"OnInterstitialAdReviewCreativeIdGeneratedEvent";
- }
- else if ( MAAdFormat.rewarded == adFormat )
- {
- name = @"OnRewardedAdReviewCreativeIdGeneratedEvent";
- }
- else
- {
- [self logInvalidAdFormat: adFormat];
- return;
- }
-
- NSMutableDictionary<NSString *, id> *args = [self defaultAdEventParametersForName: name withAd: ad];
- args[@"adReviewCreativeId"] = creativeIdentifier;
- args[@"keepInBackground"] = @([adFormat isFullscreenAd]);
-
- // Forward the event in background for fullscreen ads so that the user gets the callback even while the ad is playing.
- [self forwardUnityEventWithArgs: args];
- });
- }
- - (NSMutableDictionary<NSString *, id> *)defaultAdEventParametersForName:(NSString *)name withAd:(MAAd *)ad
- {
- NSMutableDictionary<NSString *, id> *args = [[self adInfoForAd: ad] mutableCopy];
- args[@"name"] = name;
-
- return args;
- }
- #pragma mark - Internal Methods
- - (void)createAdViewWithAdUnitIdentifier:(NSString *)adUnitIdentifier adFormat:(MAAdFormat *)adFormat atPosition:(NSString *)adViewPosition withOffset:(CGPoint)offset
- {
- max_unity_dispatch_on_main_thread(^{
- [self log: @"Creating %@ with ad unit identifier \"%@\" and position: \"%@\"", adFormat, adUnitIdentifier, adViewPosition];
-
- if ( self.adViews[adUnitIdentifier] )
- {
- [self log: @"Trying to create a %@ that was already created. This will cause the current ad to be hidden.", adFormat.label];
- }
-
- // Retrieve ad view from the map
- MAAdView *adView = [self retrieveAdViewForAdUnitIdentifier: adUnitIdentifier adFormat: adFormat atPosition: adViewPosition withOffset: offset];
- adView.hidden = YES;
- self.safeAreaBackground.hidden = YES;
-
- // Position ad view immediately so if publisher sets color before ad loads, it will not be the size of the screen
- self.adViewAdFormats[adUnitIdentifier] = adFormat;
- [self positionAdViewForAdUnitIdentifier: adUnitIdentifier adFormat: adFormat];
-
- NSDictionary<NSString *, NSString *> *extraParameters = self.adViewExtraParametersToSetAfterCreate[adUnitIdentifier];
-
- // Enable adaptive banners by default for banners and leaders.
- if ( [adFormat isBannerOrLeaderAd] )
- {
- // Check if there is already a pending setting for adaptive banners. If not, enable them.
- if ( !extraParameters[@"adaptive_banner"] )
- {
- [adView setExtraParameterForKey: @"adaptive_banner" value: @"true"];
- }
- }
-
- // Handle initial extra parameters if publisher sets it before creating ad view
- if ( extraParameters )
- {
- for ( NSString *key in extraParameters )
- {
- [adView setExtraParameterForKey: key value: extraParameters[key]];
-
- [self handleExtraParameterChangesIfNeededForAdUnitIdentifier: adUnitIdentifier
- adFormat: adFormat
- key: key
- value: extraParameters[key]];
- }
-
- [self.adViewExtraParametersToSetAfterCreate removeObjectForKey: adUnitIdentifier];
- }
-
- // Handle initial local extra parameters if publisher sets it before creating ad view
- if ( self.adViewLocalExtraParametersToSetAfterCreate[adUnitIdentifier] )
- {
- NSDictionary<NSString *, NSString *> *localExtraParameters = self.adViewLocalExtraParametersToSetAfterCreate[adUnitIdentifier];
- for ( NSString *key in localExtraParameters )
- {
- [adView setLocalExtraParameterForKey: key value: localExtraParameters[key]];
- }
-
- [self.adViewLocalExtraParametersToSetAfterCreate removeObjectForKey: adUnitIdentifier];
- }
-
- // Handle initial custom data if publisher sets it before creating ad view
- if ( self.adViewCustomDataToSetAfterCreate[adUnitIdentifier] )
- {
- NSString *customData = self.adViewCustomDataToSetAfterCreate[adUnitIdentifier];
- adView.customData = customData;
-
- [self.adViewCustomDataToSetAfterCreate removeObjectForKey: adUnitIdentifier];
- }
-
- [adView loadAd];
-
- // Disable auto-refresh if publisher sets it before creating the ad view.
- if ( [self.disabledAutoRefreshAdViewAdUnitIdentifiers containsObject: adUnitIdentifier] )
- {
- [adView stopAutoRefresh];
- }
-
- // The publisher may have requested to show the banner before it was created. Now that the banner is created, show it.
- if ( [self.adUnitIdentifiersToShowAfterCreate containsObject: adUnitIdentifier] )
- {
- [self showAdViewWithAdUnitIdentifier: adUnitIdentifier adFormat: adFormat];
- [self.adUnitIdentifiersToShowAfterCreate removeObject: adUnitIdentifier];
- }
- });
- }
- - (void)loadAdViewWithAdUnitIdentifier:(NSString *)adUnitIdentifier adFormat:(MAAdFormat *)adFormat
- {
- max_unity_dispatch_on_main_thread(^{
- MAAdView *adView = [self retrieveAdViewForAdUnitIdentifier: adUnitIdentifier adFormat: adFormat];
- if ( !adView )
- {
- [self log: @"%@ does not exist for ad unit identifier \"%@\".", adFormat.label, adUnitIdentifier];
- return;
- }
-
- if ( ![self.disabledAutoRefreshAdViewAdUnitIdentifiers containsObject: adUnitIdentifier] )
- {
- if ( [adView isHidden] )
- {
- [self log: @"Auto-refresh will resume when the %@ ad is shown. You should only call LoadBanner() or LoadMRec() if you explicitly pause auto-refresh and want to manually load an ad.", adFormat.label];
- return;
- }
-
- [self log: @"You must stop auto-refresh if you want to manually load %@ ads.", adFormat.label];
- return;
- }
-
- [adView loadAd];
- });
- }
- - (void)setAdViewBackgroundColorForAdUnitIdentifier:(NSString *)adUnitIdentifier adFormat:(MAAdFormat *)adFormat hexColorCode:(NSString *)hexColorCode
- {
- max_unity_dispatch_on_main_thread(^{
- [self log: @"Setting %@ with ad unit identifier \"%@\" to color: \"%@\"", adFormat, adUnitIdentifier, hexColorCode];
-
- // In some cases, black color may get redrawn on each frame update, resulting in an undesired flicker
- NSString *hexColorCodeToUse = [hexColorCode containsString: @"FF000000"] ? @"FF000001" : hexColorCode;
- UIColor *convertedColor = [UIColor al_colorWithHexString: hexColorCodeToUse];
-
- MAAdView *view = [self retrieveAdViewForAdUnitIdentifier: adUnitIdentifier adFormat: adFormat];
- self.publisherBannerBackgroundColor = convertedColor;
- self.safeAreaBackground.backgroundColor = view.backgroundColor = convertedColor;
-
- // Position adView to ensure logic that depends on background color is properly run
- [self positionAdViewForAdUnitIdentifier: adUnitIdentifier adFormat: adFormat];
- });
- }
- - (void)setAdViewPlacement:(nullable NSString *)placement forAdUnitIdentifier:(NSString *)adUnitIdentifier adFormat:(MAAdFormat *)adFormat
- {
- max_unity_dispatch_on_main_thread(^{
- [self log: @"Setting placement \"%@\" for \"%@\" with ad unit identifier \"%@\"", placement, adFormat, adUnitIdentifier];
-
- MAAdView *adView = [self retrieveAdViewForAdUnitIdentifier: adUnitIdentifier adFormat: adFormat];
- adView.placement = placement;
- });
- }
- - (void)startAdViewAutoRefreshForAdUnitIdentifier:(NSString *)adUnitIdentifier adFormat:(MAAdFormat *)adFormat
- {
- max_unity_dispatch_on_main_thread(^{
- [self log: @"Starting %@ auto refresh for ad unit identifier \"%@\"", adFormat.label, adUnitIdentifier];
-
- [self.disabledAutoRefreshAdViewAdUnitIdentifiers removeObject: adUnitIdentifier];
-
- MAAdView *adView = [self retrieveAdViewForAdUnitIdentifier: adUnitIdentifier adFormat: adFormat];
- if ( !adView )
- {
- [self log: @"%@ does not exist for ad unit identifier %@.", adFormat.label, adUnitIdentifier];
- return;
- }
-
- [adView startAutoRefresh];
- });
- }
- - (void)stopAdViewAutoRefreshForAdUnitIdentifier:(NSString *)adUnitIdentifier adFormat:(MAAdFormat *)adFormat
- {
- max_unity_dispatch_on_main_thread(^{
- [self log: @"Stopping %@ auto refresh for ad unit identifier \"%@\"", adFormat.label, adUnitIdentifier];
-
- [self.disabledAutoRefreshAdViewAdUnitIdentifiers addObject: adUnitIdentifier];
-
- MAAdView *adView = [self retrieveAdViewForAdUnitIdentifier: adUnitIdentifier adFormat: adFormat];
- if ( !adView )
- {
- [self log: @"%@ does not exist for ad unit identifier %@.", adFormat.label, adUnitIdentifier];
- return;
- }
-
- [adView stopAutoRefresh];
- });
- }
- - (void)setAdViewWidth:(CGFloat)width forAdUnitIdentifier:(NSString *)adUnitIdentifier adFormat:(MAAdFormat *)adFormat
- {
- max_unity_dispatch_on_main_thread(^{
- [self log: @"Setting width %f for \"%@\" with ad unit identifier \"%@\"", width, adFormat, adUnitIdentifier];
-
- BOOL isBannerOrLeader = adFormat.isBannerOrLeaderAd;
-
- // Banners and leaders need to be at least 320pts wide.
- CGFloat minWidth = isBannerOrLeader ? MAAdFormat.banner.size.width : adFormat.size.width;
- if ( width < minWidth )
- {
- [self log: @"The provided width: %f is smaller than the minimum required width: %f for ad format: %@. Automatically setting width to %f.", width, minWidth, adFormat, minWidth];
- }
-
- CGFloat widthToSet = MAX( minWidth, width );
- self.adViewWidths[adUnitIdentifier] = @(widthToSet);
- [self positionAdViewForAdUnitIdentifier: adUnitIdentifier adFormat: adFormat];
- });
- }
- - (void)updateAdViewPosition:(NSString *)adViewPosition withOffset:(CGPoint)offset forAdUnitIdentifier:(NSString *)adUnitIdentifier adFormat:(MAAdFormat *)adFormat
- {
- max_unity_dispatch_on_main_thread(^{
- self.adViewPositions[adUnitIdentifier] = adViewPosition;
- self.adViewOffsets[adUnitIdentifier] = [NSValue valueWithCGPoint: offset];
- [self positionAdViewForAdUnitIdentifier: adUnitIdentifier adFormat: adFormat];
- });
- }
- - (void)setAdViewExtraParameterForAdUnitIdentifier:(NSString *)adUnitIdentifier adFormat:(MAAdFormat *)adFormat key:(NSString *)key value:(nullable NSString *)value
- {
- max_unity_dispatch_on_main_thread(^{
- [self log: @"Setting %@ extra with key: \"%@\" value: \"%@\"", adFormat, key, value];
-
- MAAdView *adView = [self retrieveAdViewForAdUnitIdentifier: adUnitIdentifier adFormat: adFormat];
- if ( adView )
- {
- [adView setExtraParameterForKey: key value: value];
- }
- else
- {
- [self log: @"%@ does not exist for ad unit identifier \"%@\". Saving extra parameter to be set when it is created.", adFormat, adUnitIdentifier];
-
- // The adView has not yet been created. Store the extra parameters, so that they can be added once the banner has been created.
- NSMutableDictionary<NSString *, NSString *> *extraParameters = self.adViewExtraParametersToSetAfterCreate[adUnitIdentifier];
- if ( !extraParameters )
- {
- extraParameters = [NSMutableDictionary dictionaryWithCapacity: 1];
- self.adViewExtraParametersToSetAfterCreate[adUnitIdentifier] = extraParameters;
- }
-
- extraParameters[key] = value;
- }
-
- // Certain extra parameters need to be handled immediately
- [self handleExtraParameterChangesIfNeededForAdUnitIdentifier: adUnitIdentifier
- adFormat: adFormat
- key: key
- value: value];
- });
- }
- - (void)setAdViewLocalExtraParameterForAdUnitIdentifier:(NSString *)adUnitIdentifier adFormat:(MAAdFormat *)adFormat key:(NSString *)key value:(nullable id)value
- {
- max_unity_dispatch_on_main_thread(^{
- [self log: @"Setting %@ local extra with key: \"%@\" value: \"%@\"", adFormat, key, value];
-
- MAAdView *adView = [self retrieveAdViewForAdUnitIdentifier: adUnitIdentifier adFormat: adFormat];
- if ( adView )
- {
- [adView setLocalExtraParameterForKey: key value: value];
- }
- else
- {
- [self log: @"%@ does not exist for ad unit identifier \"%@\". Saving local extra parameter to be set when it is created.", adFormat, adUnitIdentifier];
-
- // The adView has not yet been created. Store the loca extra parameters, so that they can be added once the adview has been created.
- NSMutableDictionary<NSString *, id> *localExtraParameters = self.adViewLocalExtraParametersToSetAfterCreate[adUnitIdentifier];
- if ( !localExtraParameters )
- {
- localExtraParameters = [NSMutableDictionary dictionaryWithCapacity: 1];
- self.adViewLocalExtraParametersToSetAfterCreate[adUnitIdentifier] = localExtraParameters;
- }
-
- localExtraParameters[key] = value;
- }
- });
- }
- - (void)setAdViewCustomData:(nullable NSString *)customData forAdUnitIdentifier:(NSString *)adUnitIdentifier adFormat:(MAAdFormat *)adFormat
- {
- max_unity_dispatch_on_main_thread(^{
-
- MAAdView *adView = [self retrieveAdViewForAdUnitIdentifier: adUnitIdentifier adFormat: adFormat];
- if ( adView )
- {
- adView.customData = customData;
- }
- else
- {
- [self log: @"%@ does not exist for ad unit identifier %@. Saving custom data to be set when it is created.", adFormat, adUnitIdentifier];
-
- // The adView has not yet been created. Store the custom data, so that they can be added once the AdView has been created.
- self.adViewCustomDataToSetAfterCreate[adUnitIdentifier] = customData;
- }
- });
- }
- - (void)handleExtraParameterChangesIfNeededForAdUnitIdentifier:(NSString *)adUnitIdentifier adFormat:(MAAdFormat *)adFormat key:(NSString *)key value:(nullable NSString *)value
- {
- if ( MAAdFormat.mrec != adFormat )
- {
- if ( [@"force_banner" isEqualToString: key] )
- {
- BOOL shouldForceBanner = [NSNumber al_numberWithString: value].boolValue;
- MAAdFormat *forcedAdFormat = shouldForceBanner ? MAAdFormat.banner : DEVICE_SPECIFIC_ADVIEW_AD_FORMAT;
-
- self.adViewAdFormats[adUnitIdentifier] = forcedAdFormat;
- [self positionAdViewForAdUnitIdentifier: adUnitIdentifier adFormat: forcedAdFormat];
- }
- else if ( [@"adaptive_banner" isEqualToString: key] )
- {
- BOOL shouldUseAdaptiveBanner = [NSNumber al_numberWithString: value].boolValue;
- if ( shouldUseAdaptiveBanner )
- {
- [self.disabledAdaptiveBannerAdUnitIdentifiers removeObject: adUnitIdentifier];
- }
- else
- {
- [self.disabledAdaptiveBannerAdUnitIdentifiers addObject: adUnitIdentifier];
- }
-
- [self positionAdViewForAdUnitIdentifier: adUnitIdentifier adFormat: adFormat];
- }
- else if ( [@"ignore_safe_area_landscape" isEqualToString: key] && [NSNumber al_numberWithString: value].boolValue )
- {
- [self.ignoreSafeAreaLandscapeAdUnitIdentifiers addObject: adUnitIdentifier];
- [self positionAdViewForAdUnitIdentifier: adUnitIdentifier adFormat: adFormat];
- }
- }
- }
- - (void)showAdViewWithAdUnitIdentifier:(NSString *)adUnitIdentifier adFormat:(MAAdFormat *)adFormat
- {
- max_unity_dispatch_on_main_thread(^{
- [self log: @"Showing %@ with ad unit identifier \"%@\"", adFormat, adUnitIdentifier];
-
- MAAdView *view = [self retrieveAdViewForAdUnitIdentifier: adUnitIdentifier adFormat: adFormat];
- if ( !view )
- {
- [self log: @"%@ does not exist for ad unit identifier %@.", adFormat, adUnitIdentifier];
-
- // The adView has not yet been created. Store the ad unit ID, so that it can be displayed once the banner has been created.
- [self.adUnitIdentifiersToShowAfterCreate addObject: adUnitIdentifier];
- }
- else
- {
- // Check edge case where ad may be detatched from view controller
- if ( !view.window.rootViewController )
- {
- [self log: @"%@ missing view controller - re-attaching to %@...", adFormat, [self unityViewController]];
-
- UIViewController *rootViewController = [self unityViewController];
- [rootViewController.view addSubview: view];
-
- [self positionAdViewForAdUnitIdentifier: adUnitIdentifier adFormat: adFormat];
- }
- }
-
- self.safeAreaBackground.hidden = NO;
- view.hidden = NO;
-
- if ( ![self.disabledAutoRefreshAdViewAdUnitIdentifiers containsObject: adUnitIdentifier] )
- {
- [view startAutoRefresh];
- }
- });
- }
- - (void)hideAdViewWithAdUnitIdentifier:(NSString *)adUnitIdentifier adFormat:(MAAdFormat *)adFormat
- {
- max_unity_dispatch_on_main_thread(^{
- [self log: @"Hiding %@ with ad unit identifier \"%@\"", adFormat, adUnitIdentifier];
- [self.adUnitIdentifiersToShowAfterCreate removeObject: adUnitIdentifier];
-
- MAAdView *view = [self retrieveAdViewForAdUnitIdentifier: adUnitIdentifier adFormat: adFormat];
- view.hidden = YES;
- self.safeAreaBackground.hidden = YES;
-
- [view stopAutoRefresh];
- });
- }
- - (NSString *)adViewLayoutForAdUnitIdentifier:(NSString *)adUnitIdentifier adFormat:(MAAdFormat *)adFormat
- {
- [self log: @"Getting %@ position with ad unit identifier \"%@\"", adFormat, adUnitIdentifier];
-
- MAAdView *view = [self retrieveAdViewForAdUnitIdentifier: adUnitIdentifier adFormat: adFormat];
- if ( !view )
- {
- [self log: @"%@ does not exist for ad unit identifier %@", adFormat, adUnitIdentifier];
-
- return @"";
- }
-
- CGRect adViewFrame = view.frame;
- return [MAUnityAdManager serializeParameters: @{@"origin_x" : [NSString stringWithFormat: @"%f", adViewFrame.origin.x],
- @"origin_y" : [NSString stringWithFormat: @"%f", adViewFrame.origin.y],
- @"width" : [NSString stringWithFormat: @"%f", CGRectGetWidth(adViewFrame)],
- @"height" : [NSString stringWithFormat: @"%f", CGRectGetHeight(adViewFrame)]}];
- }
- - (void)destroyAdViewWithAdUnitIdentifier:(NSString *)adUnitIdentifier adFormat:(MAAdFormat *)adFormat
- {
- max_unity_dispatch_on_main_thread(^{
- [self log: @"Destroying %@ with ad unit identifier \"%@\"", adFormat, adUnitIdentifier];
-
- MAAdView *view = [self retrieveAdViewForAdUnitIdentifier: adUnitIdentifier adFormat: adFormat];
- view.delegate = nil;
- view.revenueDelegate = nil;
- view.adReviewDelegate = nil;
-
- [view removeFromSuperview];
-
- [self.adViews removeObjectForKey: adUnitIdentifier];
- [self.adViewAdFormats removeObjectForKey: adUnitIdentifier];
- [self.adViewPositions removeObjectForKey: adUnitIdentifier];
- [self.adViewOffsets removeObjectForKey: adUnitIdentifier];
- [self.adViewWidths removeObjectForKey: adUnitIdentifier];
- [self.verticalAdViewFormats removeObjectForKey: adUnitIdentifier];
- [self.disabledAdaptiveBannerAdUnitIdentifiers removeObject: adUnitIdentifier];
- });
- }
- - (void)logInvalidAdFormat:(MAAdFormat *)adFormat
- {
- [self log: @"invalid ad format: %@, from %@", adFormat, [NSThread callStackSymbols]];
- }
- - (void)log:(NSString *)format, ...
- {
- if (max_unity_should_disable_all_logs()) return;
-
- va_list valist;
- va_start(valist, format);
- NSString *message = [[NSString alloc] initWithFormat: format arguments: valist];
- va_end(valist);
-
- NSLog(@"[%@] [%@] %@", SDK_TAG, TAG, message);
- }
- + (void)log:(NSString *)format, ...
- {
- if (max_unity_should_disable_all_logs()) return;
- va_list valist;
- va_start(valist, format);
- NSString *message = [[NSString alloc] initWithFormat: format arguments: valist];
- va_end(valist);
-
- NSLog(@"[%@] [%@] %@", SDK_TAG, TAG, message);
- }
- - (MAInterstitialAd *)retrieveInterstitialForAdUnitIdentifier:(NSString *)adUnitIdentifier
- {
- MAInterstitialAd *result = self.interstitials[adUnitIdentifier];
- if ( !result )
- {
- result = [[MAInterstitialAd alloc] initWithAdUnitIdentifier: adUnitIdentifier sdk: self.sdk];
- result.delegate = self;
- result.revenueDelegate = self;
- result.adReviewDelegate = self;
- result.expirationDelegate = self;
-
- self.interstitials[adUnitIdentifier] = result;
- }
-
- return result;
- }
- - (MAAppOpenAd *)retrieveAppOpenAdForAdUnitIdentifier:(NSString *)adUnitIdentifier
- {
- MAAppOpenAd *result = self.appOpenAds[adUnitIdentifier];
- if ( !result )
- {
- result = [[MAAppOpenAd alloc] initWithAdUnitIdentifier: adUnitIdentifier sdk: self.sdk];
- result.delegate = self;
- result.revenueDelegate = self;
- result.expirationDelegate = self;
-
- self.appOpenAds[adUnitIdentifier] = result;
- }
-
- return result;
- }
- - (MARewardedAd *)retrieveRewardedAdForAdUnitIdentifier:(NSString *)adUnitIdentifier
- {
- MARewardedAd *result = self.rewardedAds[adUnitIdentifier];
- if ( !result )
- {
- result = [MARewardedAd sharedWithAdUnitIdentifier: adUnitIdentifier sdk: self.sdk];
- result.delegate = self;
- result.revenueDelegate = self;
- result.adReviewDelegate = self;
- result.expirationDelegate = self;
-
- self.rewardedAds[adUnitIdentifier] = result;
- }
-
- return result;
- }
- - (MAAdView *)retrieveAdViewForAdUnitIdentifier:(NSString *)adUnitIdentifier adFormat:(MAAdFormat *)adFormat
- {
- return [self retrieveAdViewForAdUnitIdentifier: adUnitIdentifier adFormat: adFormat atPosition: nil withOffset: CGPointZero];
- }
- - (MAAdView *)retrieveAdViewForAdUnitIdentifier:(NSString *)adUnitIdentifier adFormat:(MAAdFormat *)adFormat atPosition:(NSString *)adViewPosition withOffset:(CGPoint)offset
- {
- MAAdView *result = self.adViews[adUnitIdentifier];
- if ( !result && adViewPosition )
- {
- result = [[MAAdView alloc] initWithAdUnitIdentifier: adUnitIdentifier adFormat: adFormat sdk: self.sdk];
- // There is a Unity bug where if an empty UIView is on screen with user interaction enabled, and a user interacts with it, it just passes the events to random parts of the screen.
- result.userInteractionEnabled = NO;
- result.translatesAutoresizingMaskIntoConstraints = NO;
- result.delegate = self;
- result.revenueDelegate = self;
- result.adReviewDelegate = self;
-
- self.adViews[adUnitIdentifier] = result;
- self.adViewPositions[adUnitIdentifier] = adViewPosition;
- self.adViewOffsets[adUnitIdentifier] = [NSValue valueWithCGPoint: offset];
-
- UIViewController *rootViewController = [self unityViewController];
- [rootViewController.view addSubview: result];
-
- // Allow pubs to pause auto-refresh immediately, by default.
- [result setExtraParameterForKey: @"allow_pause_auto_refresh_immediately" value: @"true"];
- }
-
- return result;
- }
- - (void)positionAdViewForAd:(MAAd *)ad
- {
- [self positionAdViewForAdUnitIdentifier: ad.adUnitIdentifier adFormat: ad.format];
- }
- - (void)positionAdViewForAdUnitIdentifier:(NSString *)adUnitIdentifier adFormat:(MAAdFormat *)adFormat
- {
- max_unity_dispatch_on_main_thread(^{
- MAAdView *adView = [self retrieveAdViewForAdUnitIdentifier: adUnitIdentifier adFormat: adFormat];
- NSString *adViewPosition = self.adViewPositions[adUnitIdentifier];
- NSValue *adViewPositionValue = self.adViewOffsets[adUnitIdentifier];
- CGPoint adViewOffset = [adViewPositionValue CGPointValue];
- BOOL isAdaptiveBannerDisabled = [self.disabledAdaptiveBannerAdUnitIdentifiers containsObject: adUnitIdentifier];
- BOOL isWidthPtsOverridden = self.adViewWidths[adUnitIdentifier] != nil;
-
- UIView *superview = adView.superview;
- if ( !superview ) return;
-
- // Deactivate any previous constraints and reset rotation so that the banner can be positioned again.
- NSArray<NSLayoutConstraint *> *activeConstraints = self.adViewConstraints[adUnitIdentifier];
- [NSLayoutConstraint deactivateConstraints: activeConstraints];
- adView.transform = CGAffineTransformIdentity;
- [self.verticalAdViewFormats removeObjectForKey: adUnitIdentifier];
-
- // Ensure superview contains the safe area background.
- if ( ![superview.subviews containsObject: self.safeAreaBackground] )
- {
- [self.safeAreaBackground removeFromSuperview];
- [superview insertSubview: self.safeAreaBackground belowSubview: adView];
- }
-
- // Deactivate any previous constraints and reset visibility state so that the safe area background can be positioned again.
- [NSLayoutConstraint deactivateConstraints: self.safeAreaBackground.constraints];
- self.safeAreaBackground.hidden = adView.hidden;
-
- //
- // Determine ad width
- //
- CGFloat adViewWidth;
-
- // Check if publisher has overridden width as points
- if ( isWidthPtsOverridden )
- {
- adViewWidth = self.adViewWidths[adUnitIdentifier].floatValue;
- }
- // Top center / bottom center stretches full screen
- else if ( [adViewPosition isEqual: @"top_center"] || [adViewPosition isEqual: @"bottom_center"] )
- {
- adViewWidth = CGRectGetWidth(KEY_WINDOW.bounds);
- }
- // Else use standard widths of 320, 728, or 300
- else
- {
- adViewWidth = adFormat.size.width;
- }
-
- //
- // Determine ad height
- //
- CGFloat adViewHeight;
-
- if ( (adFormat == MAAdFormat.banner || adFormat == MAAdFormat.leader) && !isAdaptiveBannerDisabled )
- {
- adViewHeight = [adFormat adaptiveSizeForWidth: adViewWidth].height;
- }
- else
- {
- adViewHeight = adFormat.size.height;
- }
-
- CGSize adViewSize = CGSizeMake(adViewWidth, adViewHeight);
-
- // All positions have constant height
- NSMutableArray<NSLayoutConstraint *> *constraints = [NSMutableArray arrayWithObject: [adView.heightAnchor constraintEqualToConstant: adViewSize.height]];
-
- UILayoutGuide *layoutGuide = superview.safeAreaLayoutGuide;
- BOOL shouldIgnoreSafeArea = [self shouldIgnoreSafeAreaForAdUnitIdentifier: adUnitIdentifier];
- NSLayoutAnchor *topAnchor = shouldIgnoreSafeArea ? superview.topAnchor : layoutGuide.topAnchor;
-
- if ( [adViewPosition isEqual: @"top_center"] || [adViewPosition isEqual: @"bottom_center"] )
- {
- // Non AdMob banners will still be of 50/90 points tall. Set the auto sizing mask such that the inner ad view is pinned to the bottom or top according to the ad view position.
- if ( !isAdaptiveBannerDisabled )
- {
- adView.autoresizingMask = UIViewAutoresizingFlexibleWidth;
-
- if ( [@"top_center" isEqual: adViewPosition] )
- {
- adView.autoresizingMask |= UIViewAutoresizingFlexibleBottomMargin;
- }
- else // bottom_center
- {
- adView.autoresizingMask |= UIViewAutoresizingFlexibleTopMargin;
- }
- }
-
- // If publisher actually provided a banner background color
- if ( self.publisherBannerBackgroundColor && adFormat != MAAdFormat.mrec )
- {
- if ( isWidthPtsOverridden )
- {
- [constraints addObjectsFromArray: @[[adView.widthAnchor constraintEqualToConstant: adViewWidth],
- [adView.centerXAnchor constraintEqualToAnchor: layoutGuide.centerXAnchor],
- [self.safeAreaBackground.widthAnchor constraintEqualToConstant: adViewWidth],
- [self.safeAreaBackground.centerXAnchor constraintEqualToAnchor: layoutGuide.centerXAnchor]]];
-
- if ( [adViewPosition isEqual: @"top_center"] )
- {
- [constraints addObjectsFromArray: @[[adView.topAnchor constraintEqualToAnchor: topAnchor],
- [self.safeAreaBackground.topAnchor constraintEqualToAnchor: superview.topAnchor],
- [self.safeAreaBackground.bottomAnchor constraintEqualToAnchor: adView.topAnchor]]];
- }
- else // bottom_center
- {
- [constraints addObjectsFromArray: @[[adView.bottomAnchor constraintEqualToAnchor: layoutGuide.bottomAnchor],
- [self.safeAreaBackground.topAnchor constraintEqualToAnchor: adView.bottomAnchor],
- [self.safeAreaBackground.bottomAnchor constraintEqualToAnchor: superview.bottomAnchor]]];
- }
- }
- else
- {
- [constraints addObjectsFromArray: @[[adView.leftAnchor constraintEqualToAnchor: superview.leftAnchor],
- [adView.rightAnchor constraintEqualToAnchor: superview.rightAnchor],
- [self.safeAreaBackground.leftAnchor constraintEqualToAnchor: superview.leftAnchor],
- [self.safeAreaBackground.rightAnchor constraintEqualToAnchor: superview.rightAnchor]]];
-
- if ( [adViewPosition isEqual: @"top_center"] )
- {
- [constraints addObjectsFromArray: @[[adView.topAnchor constraintEqualToAnchor: topAnchor],
- [self.safeAreaBackground.topAnchor constraintEqualToAnchor: superview.topAnchor],
- [self.safeAreaBackground.bottomAnchor constraintEqualToAnchor: adView.topAnchor]]];
- }
- else // bottom_center
- {
- [constraints addObjectsFromArray: @[[adView.bottomAnchor constraintEqualToAnchor: layoutGuide.bottomAnchor],
- [self.safeAreaBackground.topAnchor constraintEqualToAnchor: adView.bottomAnchor],
- [self.safeAreaBackground.bottomAnchor constraintEqualToAnchor: superview.bottomAnchor]]];
- }
- }
- }
- // If pub does not have a background color set or this is not a banner
- else
- {
- self.safeAreaBackground.hidden = YES;
-
- [constraints addObjectsFromArray: @[[adView.widthAnchor constraintEqualToConstant: adViewWidth],
- [adView.centerXAnchor constraintEqualToAnchor: layoutGuide.centerXAnchor]]];
-
- if ( [adViewPosition isEqual: @"top_center"] )
- {
- [constraints addObject: [adView.topAnchor constraintEqualToAnchor: topAnchor]];
- }
- else // BottomCenter
- {
- [constraints addObject: [adView.bottomAnchor constraintEqualToAnchor: layoutGuide.bottomAnchor]];
- }
- }
- }
- // Check if the publisher wants vertical banners.
- else if ( [adViewPosition isEqual: @"center_left"] || [adViewPosition isEqual: @"center_right"] )
- {
- if ( MAAdFormat.mrec == adFormat )
- {
- [constraints addObject: [adView.widthAnchor constraintEqualToConstant: adViewSize.width]];
-
- if ( [adViewPosition isEqual: @"center_left"] )
- {
- [constraints addObjectsFromArray: @[[adView.centerYAnchor constraintEqualToAnchor: layoutGuide.centerYAnchor],
- [adView.leftAnchor constraintEqualToAnchor: superview.leftAnchor]]];
-
- [constraints addObjectsFromArray: @[[self.safeAreaBackground.rightAnchor constraintEqualToAnchor: layoutGuide.leftAnchor],
- [self.safeAreaBackground.leftAnchor constraintEqualToAnchor: superview.leftAnchor]]];
- }
- else // center_right
- {
- [constraints addObjectsFromArray: @[[adView.centerYAnchor constraintEqualToAnchor: layoutGuide.centerYAnchor],
- [adView.rightAnchor constraintEqualToAnchor: superview.rightAnchor]]];
-
- [constraints addObjectsFromArray: @[[self.safeAreaBackground.leftAnchor constraintEqualToAnchor: layoutGuide.rightAnchor],
- [self.safeAreaBackground.rightAnchor constraintEqualToAnchor: superview.rightAnchor]]];
- }
- }
- else
- {
- /* Align the center of the view such that when rotated it snaps into place.
- *
- * +---+---+-------+
- * | | |
- * | | |
- * | | |
- * | | |
- * | | |
- * | | |
- * +-------------+---+-----------+--+
- * | | + | + | |
- * +-------------+---+-----------+--+
- * <+> | |
- * |+ | |
- * || | |
- * || | |
- * || | |
- * || | |
- * +|--+-----------+
- * v
- * Banner Half Height
- */
- self.safeAreaBackground.hidden = YES;
-
- adView.transform = CGAffineTransformRotate(CGAffineTransformIdentity, M_PI_2);
-
- CGFloat width;
- // If the publiser has a background color set - set the width to the height of the screen, to span the ad across the screen after it is rotated.
- if ( self.publisherBannerBackgroundColor )
- {
- if ( isWidthPtsOverridden )
- {
- width = adViewWidth;
- }
- else
- {
- width = CGRectGetHeight(KEY_WINDOW.bounds);
- }
- }
- // Otherwise - we shouldn't span the banner the width of the realm (there might be user-interactable UI on the sides)
- else
- {
- width = adViewWidth;
- }
- [constraints addObject: [adView.widthAnchor constraintEqualToConstant: width]];
-
- // Set constraints such that the center of the banner aligns with the center left or right as needed. That way, once rotated, the banner snaps into place.
- [constraints addObject: [adView.centerYAnchor constraintEqualToAnchor: superview.centerYAnchor]];
-
- // Place the center of the banner half the height of the banner away from the side. If we align the center exactly with the left/right anchor, only half the banner will be visible.
- CGFloat bannerHalfHeight = adViewSize.height / 2.0;
- UIInterfaceOrientation orientation = [UIApplication sharedApplication].statusBarOrientation;
- if ( [adViewPosition isEqual: @"center_left"] )
- {
- NSLayoutAnchor *anchor = ( orientation == UIInterfaceOrientationLandscapeRight ) ? layoutGuide.leftAnchor : superview.leftAnchor;
- [constraints addObject: [adView.centerXAnchor constraintEqualToAnchor: anchor constant: bannerHalfHeight]];
- }
- else // CenterRight
- {
- NSLayoutAnchor *anchor = ( orientation == UIInterfaceOrientationLandscapeLeft ) ? layoutGuide.rightAnchor : superview.rightAnchor;
- [constraints addObject: [adView.centerXAnchor constraintEqualToAnchor: anchor constant: -bannerHalfHeight]];
- }
-
- // Store the ad view with format, so that it can be updated when the orientation changes.
- self.verticalAdViewFormats[adUnitIdentifier] = adFormat;
-
- // If adaptive - make top flexible since we anchor with the bottom of the banner at the edge of the screen
- if ( !isAdaptiveBannerDisabled )
- {
- adView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleTopMargin;
- }
- }
- }
- // Otherwise, publisher will likely construct his own views around the adview
- else
- {
- self.safeAreaBackground.hidden = YES;
-
- [constraints addObject: [adView.widthAnchor constraintEqualToConstant: adViewWidth]];
-
- if ( [adViewPosition isEqual: @"top_left"] )
- {
- [constraints addObjectsFromArray: @[[adView.leftAnchor constraintEqualToAnchor: superview.leftAnchor constant: adViewOffset.x],
- [adView.topAnchor constraintEqualToAnchor: topAnchor constant: adViewOffset.y]]];
- }
- else if ( [adViewPosition isEqual: @"top_right"] )
- {
- [constraints addObjectsFromArray: @[[adView.topAnchor constraintEqualToAnchor: topAnchor],
- [adView.rightAnchor constraintEqualToAnchor: superview.rightAnchor]]];
- }
- else if ( [adViewPosition isEqual: @"centered"] )
- {
- [constraints addObjectsFromArray: @[[adView.centerXAnchor constraintEqualToAnchor: layoutGuide.centerXAnchor],
- [adView.centerYAnchor constraintEqualToAnchor: layoutGuide.centerYAnchor]]];
- }
- else if ( [adViewPosition isEqual: @"bottom_left"] )
- {
- [constraints addObjectsFromArray: @[[adView.bottomAnchor constraintEqualToAnchor: layoutGuide.bottomAnchor],
- [adView.leftAnchor constraintEqualToAnchor: superview.leftAnchor]]];
- }
- else if ( [adViewPosition isEqual: @"bottom_right"] )
- {
- [constraints addObjectsFromArray: @[[adView.bottomAnchor constraintEqualToAnchor: layoutGuide.bottomAnchor],
- [adView.rightAnchor constraintEqualToAnchor: superview.rightAnchor]]];
- }
- }
-
- self.adViewConstraints[adUnitIdentifier] = constraints;
-
- [NSLayoutConstraint activateConstraints: constraints];
- });
- }
- - (UIViewController *)unityViewController
- {
- // Handle edge case where `UnityGetGLViewController()` returns nil
- return UnityGetGLViewController() ?: UnityGetMainWindow().rootViewController ?: [KEY_WINDOW rootViewController];
- }
- - (void)forwardUnityEventWithArgs:(NSDictionary<NSString *, id> *)args
- {
- #if !IS_TEST_APP
- extern bool _didResignActive;
- // We should not call any script callbacks when application is not active. Suspend the callback queue if resign is active.
- // We'll resume the queue once the application becomes active again.
- self.backgroundCallbackEventsQueue.suspended = _didResignActive;
- #endif
-
- [self.backgroundCallbackEventsQueue addOperationWithBlock:^{
- NSString *serializedParameters = [MAUnityAdManager serializeParameters: args];
- backgroundCallback(serializedParameters.UTF8String);
- }];
- }
- + (NSString *)serializeParameters:(NSDictionary<NSString *, id> *)dict
- {
- NSData *jsonData = [NSJSONSerialization dataWithJSONObject: dict options: 0 error: nil];
- return [[NSString alloc] initWithData: jsonData encoding: NSUTF8StringEncoding];
- }
- + (NSDictionary<NSString *, id> *)deserializeParameters:(nullable NSString *)serialized
- {
- if ( serialized.length > 0 )
- {
- NSError *error;
- NSDictionary<NSString *, id> *deserialized = [NSJSONSerialization JSONObjectWithData: [serialized dataUsingEncoding:NSUTF8StringEncoding]
- options: 0
- error: &error];
- if ( error )
- {
- [self log: @"Failed to deserialize (%@) with error %@", serialized, error];
- return @{};
- }
-
- return deserialized;
- }
- else
- {
- return @{};
- }
- }
- - (MAAdFormat *)adViewAdFormatForAdUnitIdentifier:(NSString *)adUnitIdentifier
- {
- if ( self.adViewAdFormats[adUnitIdentifier] )
- {
- return self.adViewAdFormats[adUnitIdentifier];
- }
- else
- {
- return DEVICE_SPECIFIC_ADVIEW_AD_FORMAT;
- }
- }
- - (NSString *)requestLatencyMillisFromRequestLatency:(NSTimeInterval)requestLatency
- {
- // Convert latency from seconds to milliseconds to match Android.
- long requestLatencyMillis = requestLatency * 1000;
- return @(requestLatencyMillis).stringValue;
- }
- #pragma mark - User Service
- - (void)didDismissUserConsentDialog
- {
- dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
- [self forwardUnityEventWithArgs: @{@"name" : @"OnSdkConsentDialogDismissedEvent"}];
- });
- }
- #pragma mark - CMP Service
- - (void)showCMPForExistingUser
- {
- [self.sdk.cmpService showCMPForExistingUserWithCompletion:^(ALCMPError * _Nullable error) {
-
- dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
- NSMutableDictionary<NSString *, id> *args = [NSMutableDictionary dictionaryWithCapacity: 2];
- args[@"name"] = @"OnCmpCompletedEvent";
-
- if ( error )
- {
- args[@"error"] = @{@"code": @(error.code),
- @"message": error.message,
- @"cmpCode": @(error.cmpCode),
- @"cmpMessage": error.cmpMessage,
- @"keepInBackground": @(YES)};
- }
-
- [self forwardUnityEventWithArgs: args];
- });
- }];
- }
- #pragma mark - Application
- - (void)applicationPaused:(NSNotification *)notification
- {
- [self notifyApplicationStateChangedEventForPauseState: YES];
- }
- - (void)applicationResumed:(NSNotification *)notification
- {
- [self notifyApplicationStateChangedEventForPauseState: NO];
- }
- - (void)notifyApplicationStateChangedEventForPauseState:(BOOL)isPaused
- {
- dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
- [self forwardUnityEventWithArgs: @{@"name": @"OnApplicationStateChanged",
- @"isPaused": @(isPaused)}];
- });
- }
- - (MAAd *)adWithAdUnitIdentifier:(NSString *)adUnitIdentifier
- {
- @synchronized ( self.adInfoDictLock )
- {
- return self.adInfoDict[adUnitIdentifier];
- }
- }
- #pragma mark - Helper
- - (BOOL)shouldIgnoreSafeAreaForAdUnitIdentifier:(NSString *)adUnitIdentifier
- {
- if ( ![self.ignoreSafeAreaLandscapeAdUnitIdentifiers containsObject: adUnitIdentifier] ) return NO;
-
- // We should use the superview instead of layout guide if the application's orientation is landscape and the extra parameter is set
- UIInterfaceOrientation orientation = [UIApplication sharedApplication].statusBarOrientation;
- return orientation == UIInterfaceOrientationLandscapeRight || orientation == UIInterfaceOrientationLandscapeLeft;
- }
- @end
|