-
Notifications
You must be signed in to change notification settings - Fork 12
/
AppLovinMAX.m
1271 lines (1039 loc) · 45.2 KB
/
AppLovinMAX.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
//
// AppLovinMAX.m
// AppLovin MAX Cordova Plugin
//
// Created by Thomas So on 1/1/21.
// Copyright © 2021 AppLovin. All rights reserved.
//
#import "AppLovinMAX.h"
#define ROOT_VIEW_CONTROLLER (self.viewController)
#define DEVICE_SPECIFIC_ADVIEW_AD_FORMAT ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPad) ? MAAdFormat.leader : MAAdFormat.banner
// Internal
@interface UIColor (ALUtils)
+ (nullable UIColor *)al_colorWithHexString:(NSString *)hexString;
@end
@interface NSNumber (ALUtils)
+ (NSNumber *)al_numberWithString:(NSString *)string;
@end
@interface NSDictionary (ALUtils)
- (BOOL)al_containsValueForKey:(id)key;
@end
@interface NSString (ALUtils)
@property (nonatomic, assign, readonly, getter=al_isValidString) BOOL al_validString;
@end
@interface AppLovinMAX()<MAAdDelegate, MAAdViewAdDelegate, MARewardedAdDelegate>
// Parent Fields
@property (nonatomic, weak) ALSdk *sdk;
@property (nonatomic, assign, getter=isPluginInitialized) BOOL pluginInitialized;
@property (nonatomic, assign, getter=isSDKInitialized) BOOL sdkInitialized;
@property (nonatomic, strong) ALSdkConfiguration *sdkConfiguration;
// Store these values if pub attempts to set it before initializing
@property (nonatomic, copy, nullable) NSString *userIdentifierToSet;
@property (nonatomic, strong, nullable) NSArray<NSString *> *testDeviceIdentifiersToSet;
@property (nonatomic, strong, nullable) NSNumber *verboseLoggingToSet;
@property (nonatomic, strong, nullable) NSNumber *yearOfBirthToSet;
@property (nonatomic, strong, nullable) NSNumber *genderToSet;
@property (nonatomic, strong, nullable) NSNumber *contentRatingToSet;
@property (nonatomic, strong, nullable) NSString *emailToSet;
@property (nonatomic, strong, nullable) NSString *phoneNumberToSet;
@property (nonatomic, strong, nullable) NSArray<NSString *> *keywordsToSet;
@property (nonatomic, strong, nullable) NSArray<NSString *> *interestsToSet;
// Fullscreen Ad Fields
@property (nonatomic, strong) NSMutableDictionary<NSString *, MAInterstitialAd *> *interstitials;
@property (nonatomic, strong) NSMutableDictionary<NSString *, MARewardedAd *> *rewardedAds;
// Banner 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 *, NSArray<NSLayoutConstraint *> *> *adViewConstraints;
@property (nonatomic, strong) NSMutableArray<NSString *> *adUnitIdentifiersToShowAfterCreate;
@property (nonatomic, strong) UIView *safeAreaBackground;
@property (nonatomic, strong, nullable) UIColor *publisherBannerBackgroundColor;
@end
@implementation AppLovinMAX
static NSString *const SDK_TAG = @"AppLovinSdk";
static NSString *const TAG = @"AppLovinMAX";
- (void)pluginInitialize
{
[super pluginInitialize];
self.interstitials = [NSMutableDictionary dictionaryWithCapacity: 2];
self.rewardedAds = [NSMutableDictionary dictionaryWithCapacity: 2];
self.adViews = [NSMutableDictionary dictionaryWithCapacity: 2];
self.adViewAdFormats = [NSMutableDictionary dictionaryWithCapacity: 2];
self.adViewPositions = [NSMutableDictionary dictionaryWithCapacity: 2];
self.adViewConstraints = [NSMutableDictionary dictionaryWithCapacity: 2];
self.adUnitIdentifiersToShowAfterCreate = [NSMutableArray arrayWithCapacity: 2];
self.safeAreaBackground = [[UIView alloc] init];
self.safeAreaBackground.hidden = YES;
self.safeAreaBackground.backgroundColor = UIColor.clearColor;
self.safeAreaBackground.translatesAutoresizingMaskIntoConstraints = NO;
self.safeAreaBackground.userInteractionEnabled = NO;
[ROOT_VIEW_CONTROLLER.view addSubview: self.safeAreaBackground];
}
- (BOOL)isInitialized
{
return [self isPluginInitialized] && [self isSDKInitialized];
}
- (void)initialize:(CDVInvokedUrlCommand *)command
{
// Guard against running init logic multiple times
if ( [self isPluginInitialized] )
{
CDVPluginResult *result = [CDVPluginResult resultWithStatus: CDVCommandStatus_OK messageAsDictionary: [self initializationMessage]];
[self.commandDelegate sendPluginResult: result callbackId: command.callbackId];
return;
}
self.pluginInitialized = YES;
NSString *pluginVersion = [command argumentAtIndex: 0];
NSString *sdkKey = [command argumentAtIndex: 1];
[self log: @"Initializing AppLovin MAX Cordova v%@...", pluginVersion];
// If SDK key passed in is empty, check Info.plist
if ( ![sdkKey al_isValidString] )
{
if ( [[NSBundle mainBundle].infoDictionary al_containsValueForKey: @"AppLovinSdkKey"] )
{
sdkKey = [NSBundle mainBundle].infoDictionary[@"AppLovinSdkKey"];
}
else
{
[NSException raise: NSInternalInconsistencyException
format: @"Unable to initialize AppLovin SDK - no SDK key provided and not found in Info.plist!"];
}
}
// Initialize SDK
self.sdk = [ALSdk sharedWithKey: sdkKey];
[self.sdk setPluginVersion: [@"Cordova-" stringByAppendingString: pluginVersion]];
[self.sdk setMediationProvider: ALMediationProviderMAX];
// Set user id if needed
if ( [self.userIdentifierToSet al_isValidString] )
{
self.sdk.userIdentifier = self.userIdentifierToSet;
self.userIdentifierToSet = nil;
}
// Set test device ids if needed
if ( self.testDeviceIdentifiersToSet )
{
self.sdk.settings.testDeviceAdvertisingIdentifiers = self.testDeviceIdentifiersToSet;
self.testDeviceIdentifiersToSet = nil;
}
// Set verbose logging state if needed
if ( self.verboseLoggingToSet )
{
self.sdk.settings.verboseLoggingEnabled = self.verboseLoggingToSet.boolValue;
self.verboseLoggingToSet = nil;
}
[self.sdk initializeSdkWithCompletionHandler:^(ALSdkConfiguration *configuration)
{
[self log: @"SDK initialized"];
self.sdkConfiguration = configuration;
self.sdkInitialized = YES;
CDVPluginResult *result = [CDVPluginResult resultWithStatus: CDVCommandStatus_OK messageAsDictionary: [self initializationMessage]];
[self.commandDelegate sendPluginResult: result callbackId: command.callbackId];
}];
}
- (NSDictionary<NSString *, id> *)initializationMessage
{
NSMutableDictionary<NSString *, id> *message = [NSMutableDictionary dictionaryWithCapacity: 4];
if ( self.sdkConfiguration )
{
message[@"consentDialogState"] = @(self.sdkConfiguration.consentDialogState);
message[@"countryCode"] = self.sdkConfiguration.countryCode;
}
else
{
message[@"consentDialogState"] = @(ALConsentDialogStateUnknown);
}
message[@"hasUserConsent"] = @([ALPrivacySettings hasUserConsent]);
message[@"isAgeRestrictedUser"] = @([ALPrivacySettings isAgeRestrictedUser]);
message[@"isDoNotSell"] = @([ALPrivacySettings isDoNotSell]);
message[@"isTablet"] = @([self isTablet]);
return message;
}
#pragma mark - General Public API
- (BOOL)isTablet
{
return [UIDevice currentDevice].userInterfaceIdiom == UIUserInterfaceIdiomPad;
}
- (void)showMediationDebugger:(CDVInvokedUrlCommand *)command
{
if ( !_sdk )
{
[self log: @"Failed to show mediation debugger - please ensure the AppLovin MAX Cordova Plugin has been initialized by calling 'AppLovinMAX.initialize(...);'!"];
[self sendErrorPluginResultForCommand: command];
return;
}
[self.sdk showMediationDebugger];
[self sendOKPluginResultForCommand: command];
}
- (void/*BOOL*/)getConsentDialogState:(CDVInvokedUrlCommand *)command
{
if ( ![self isInitialized] )
{
CDVPluginResult *result = [CDVPluginResult resultWithStatus: CDVCommandStatus_ERROR
messageAsNSInteger: ALConsentDialogStateUnknown];
[self.commandDelegate sendPluginResult: result callbackId: command.callbackId];
return;
}
CDVPluginResult *result = [CDVPluginResult resultWithStatus: CDVCommandStatus_OK
messageAsNSInteger: self.sdkConfiguration.consentDialogState];
[self.commandDelegate sendPluginResult: result callbackId: command.callbackId];
}
- (void)setHasUserConsent:(CDVInvokedUrlCommand *)command
{
BOOL hasUserConsent = ((NSNumber *)[command argumentAtIndex: 0]).boolValue;
[ALPrivacySettings setHasUserConsent: hasUserConsent];
[self sendOKPluginResultForCommand: command];
}
- (void/*BOOL*/)hasUserConsent:(CDVInvokedUrlCommand *)command
{
[self sendOKPluginResultForCommand: command withBool: [ALPrivacySettings hasUserConsent]];
}
- (void)setIsAgeRestrictedUser:(CDVInvokedUrlCommand *)command
{
BOOL isAgeRestrictedUser = ((NSNumber *)[command argumentAtIndex: 0]).boolValue;
[ALPrivacySettings setIsAgeRestrictedUser: isAgeRestrictedUser];
[self sendOKPluginResultForCommand: command];
}
- (void/*BOOL*/)isAgeRestrictedUser:(CDVInvokedUrlCommand *)command
{
[self sendOKPluginResultForCommand: command withBool: [ALPrivacySettings isAgeRestrictedUser]];
}
- (void)setDoNotSell:(CDVInvokedUrlCommand *)command
{
BOOL doNotSell = ((NSNumber *)[command argumentAtIndex: 0]).boolValue;
[ALPrivacySettings setDoNotSell: doNotSell];
[self sendOKPluginResultForCommand: command];
}
- (void/*BOOL*/)isDoNotSell:(CDVInvokedUrlCommand *)command
{
[self sendOKPluginResultForCommand: command withBool: [ALPrivacySettings isDoNotSell]];
}
- (void)setUserId:(CDVInvokedUrlCommand *)command
{
NSString *userId = [command argumentAtIndex: 0];
if ( [self isPluginInitialized] )
{
self.sdk.userIdentifier = userId;
self.userIdentifierToSet = nil;
}
else
{
self.userIdentifierToSet = userId;
}
[self sendOKPluginResultForCommand: command];
}
- (void)setMuted:(CDVInvokedUrlCommand *)command
{
if ( ![self isPluginInitialized] )
{
[self sendErrorPluginResultForCommand: command];
return;
}
BOOL muted = ((NSNumber *)[command argumentAtIndex: 0]).boolValue;
self.sdk.settings.muted = muted;
[self sendOKPluginResultForCommand: command];
}
- (void)setVerboseLogging:(CDVInvokedUrlCommand *)command
{
BOOL enabled = ((NSNumber *)[command argumentAtIndex: 0]).boolValue;
if ( [self isPluginInitialized] )
{
self.sdk.settings.verboseLoggingEnabled = enabled;
self.verboseLoggingToSet = nil;
}
else
{
self.verboseLoggingToSet = @(enabled);
}
[self sendOKPluginResultForCommand: command];
}
- (void)setTestDeviceAdvertisingIds:(CDVInvokedUrlCommand *)command
{
NSArray<NSString *> *testDeviceAdvertisingIds = [command argumentAtIndex: 0];
if ( [self isPluginInitialized] )
{
self.sdk.settings.testDeviceAdvertisingIdentifiers = testDeviceAdvertisingIds;
self.testDeviceIdentifiersToSet = nil;
}
else
{
self.testDeviceIdentifiersToSet = testDeviceAdvertisingIds;
}
[self sendOKPluginResultForCommand: command];
}
- (void)setYearOfBirth:(CDVInvokedUrlCommand *)command
{
NSNumber *yearOfBirth = [command argumentAtIndex: 0];
if ( [self isPluginInitialized] )
{
self.sdk.targetingData.yearOfBirth = yearOfBirth;
self.yearOfBirthToSet = nil;
}
else
{
self.yearOfBirthToSet = yearOfBirth;
}
[self sendOKPluginResultForCommand: command];
}
- (void)setGender:(CDVInvokedUrlCommand *)command
{
NSNumber *gender = [command argumentAtIndex: 0];
if ( [self isPluginInitialized] )
{
self.sdk.targetingData.gender = [gender intValue];
self.genderToSet = nil;
}
else
{
self.genderToSet = gender;
}
[self sendOKPluginResultForCommand: command];
}
- (void)setMaximumAdContentRating:(CDVInvokedUrlCommand *)command
{
NSNumber *contentRating = [command argumentAtIndex: 0];
if ( [self isPluginInitialized] )
{
self.sdk.targetingData.maximumAdContentRating = [contentRating intValue];
self.contentRatingToSet = nil;
}
else
{
self.contentRatingToSet = contentRating;
}
[self sendOKPluginResultForCommand: command];
}
- (void)setEmail:(CDVInvokedUrlCommand *)command
{
NSString *email = [command argumentAtIndex: 0];
if ( [self isPluginInitialized] )
{
self.sdk.targetingData.email = email;
self.emailToSet = nil;
}
else
{
self.emailToSet = email;
}
[self sendOKPluginResultForCommand: command];
}
- (void)setPhoneNumber:(CDVInvokedUrlCommand *)command
{
NSString *phoneNumber = [command argumentAtIndex: 0];
if ( [self isPluginInitialized] )
{
self.sdk.targetingData.phoneNumber = phoneNumber;
self.phoneNumberToSet = nil;
}
else
{
self.phoneNumberToSet = phoneNumber;
}
[self sendOKPluginResultForCommand: command];
}
- (void)setKeywords:(CDVInvokedUrlCommand *)command
{
NSArray<NSString *> *keywords = [command argumentAtIndex: 0];
if ( [self isPluginInitialized] )
{
self.sdk.targetingData.keywords = keywords;
self.keywordsToSet = nil;
}
else
{
self.keywordsToSet = keywords;
}
[self sendOKPluginResultForCommand: command];
}
- (void)setInterests:(CDVInvokedUrlCommand *)command
{
NSArray<NSString *> *interests = [command argumentAtIndex: 0];
if ( [self isPluginInitialized] )
{
self.sdk.targetingData.interests = interests;
self.interestsToSet = nil;
}
else
{
self.interestsToSet = interests;
}
[self sendOKPluginResultForCommand: command];
}
- (void)clearAllTargetingData:(CDVInvokedUrlCommand *)command
{
if ( !_sdk )
{
[self log: @"Failed to clear targeting data - please ensure the AppLovin MAX Cordova Plugin has been initialized by calling 'AppLovinMAX.initialize(...);'!"];
[self sendErrorPluginResultForCommand: command];
return;
}
[self.sdk.targetingData clearAll];
[self sendOKPluginResultForCommand: command];
}
#pragma mark - Event Tracking
- (void)trackEvent:(CDVInvokedUrlCommand *)command
{
NSString *event = [command argumentAtIndex: 0];
NSDictionary<NSString *, id> *parameters = [command argumentAtIndex: 1];
[self.sdk.eventService trackEvent: event parameters: parameters];
[self sendOKPluginResultForCommand: command];
}
#pragma mark - Banners
- (void)createBanner:(CDVInvokedUrlCommand *)command
{
NSString *adUnitIdentifier = [command argumentAtIndex: 0];
NSString *bannerPosition = [command argumentAtIndex: 1];
[self createAdViewWithAdUnitIdentifier: adUnitIdentifier adFormat: DEVICE_SPECIFIC_ADVIEW_AD_FORMAT atPosition: bannerPosition command: command];
}
- (void)setBannerBackgroundColor:(CDVInvokedUrlCommand *)command
{
NSString *adUnitIdentifier = [command argumentAtIndex: 0];
NSString *hexColorCode = [command argumentAtIndex: 1];
[self setAdViewBackgroundColorForAdUnitIdentifier: adUnitIdentifier adFormat: DEVICE_SPECIFIC_ADVIEW_AD_FORMAT hexColorCode: hexColorCode command: command];
}
- (void)setBannerPlacement:(CDVInvokedUrlCommand *)command
{
NSString *placement = [command argumentAtIndex: 0];
NSString *adUnitIdentifier = [command argumentAtIndex: 1];
[self setAdViewPlacement: placement forAdUnitIdentifier: adUnitIdentifier adFormat: DEVICE_SPECIFIC_ADVIEW_AD_FORMAT command: command];
}
- (void)updateBannerPosition:(CDVInvokedUrlCommand *)command
{
NSString *bannerPosition = [command argumentAtIndex: 0];
NSString *adUnitIdentifier = [command argumentAtIndex: 1];
[self updateAdViewPosition: bannerPosition forAdUnitIdentifier: adUnitIdentifier adFormat: DEVICE_SPECIFIC_ADVIEW_AD_FORMAT command: command];
}
- (void)setBannerExtraParameter:(CDVInvokedUrlCommand *)command
{
NSString *adUnitIdentifier = [command argumentAtIndex: 0];
NSString *key = [command argumentAtIndex: 1];
NSString *value = [command argumentAtIndex: 2];
[self setAdViewExtraParameterForAdUnitIdentifier: adUnitIdentifier adFormat: DEVICE_SPECIFIC_ADVIEW_AD_FORMAT key: key value: value command: command];
}
- (void)showBanner:(CDVInvokedUrlCommand *)command
{
NSString *adUnitIdentifier = [command argumentAtIndex: 0];
[self showAdViewWithAdUnitIdentifier: adUnitIdentifier adFormat: DEVICE_SPECIFIC_ADVIEW_AD_FORMAT command: command];
}
- (void)hideBanner:(CDVInvokedUrlCommand *)command
{
NSString *adUnitIdentifier = [command argumentAtIndex: 0];
[self hideAdViewWithAdUnitIdentifier: adUnitIdentifier adFormat: DEVICE_SPECIFIC_ADVIEW_AD_FORMAT command: command];
}
- (void)destroyBanner:(CDVInvokedUrlCommand *)command
{
NSString *adUnitIdentifier = [command argumentAtIndex: 0];
[self destroyAdViewWithAdUnitIdentifier: adUnitIdentifier adFormat: DEVICE_SPECIFIC_ADVIEW_AD_FORMAT command: command];
}
#pragma mark - MRECs
- (void)createMRec:(CDVInvokedUrlCommand *)command
{
NSString *adUnitIdentifier = [command argumentAtIndex: 0];
NSString *mrecPosition = [command argumentAtIndex: 1];
[self createAdViewWithAdUnitIdentifier: adUnitIdentifier adFormat: MAAdFormat.mrec atPosition: mrecPosition command: command];
}
- (void)setMRecPlacement:(CDVInvokedUrlCommand *)command
{
NSString *placement = [command argumentAtIndex: 0];
NSString *adUnitIdentifier = [command argumentAtIndex: 1];
[self setAdViewPlacement: placement forAdUnitIdentifier: adUnitIdentifier adFormat: MAAdFormat.mrec command: command];
}
- (void)updateMRecPosition:(CDVInvokedUrlCommand *)command
{
NSString *mrecPosition = [command argumentAtIndex: 0];
NSString *adUnitIdentifier = [command argumentAtIndex: 1];
[self updateAdViewPosition: mrecPosition forAdUnitIdentifier: adUnitIdentifier adFormat: MAAdFormat.mrec command: command];
}
- (void)showMRec:(CDVInvokedUrlCommand *)command
{
NSString *adUnitIdentifier = [command argumentAtIndex: 0];
[self showAdViewWithAdUnitIdentifier: adUnitIdentifier adFormat: MAAdFormat.mrec command: command];
}
- (void)hideMRec:(CDVInvokedUrlCommand *)command
{
NSString *adUnitIdentifier = [command argumentAtIndex: 0];
[self hideAdViewWithAdUnitIdentifier: adUnitIdentifier adFormat: MAAdFormat.mrec command: command];
}
- (void)destroyMRec:(CDVInvokedUrlCommand *)command
{
NSString *adUnitIdentifier = [command argumentAtIndex: 0];
[self destroyAdViewWithAdUnitIdentifier: adUnitIdentifier adFormat: MAAdFormat.mrec command: command];
}
#pragma mark - Interstitials
- (void)loadInterstitial:(CDVInvokedUrlCommand *)command
{
NSString *adUnitIdentifier = [command argumentAtIndex: 0];
MAInterstitialAd *interstitial = [self retrieveInterstitialForAdUnitIdentifier: adUnitIdentifier];
[interstitial loadAd];
[self sendOKPluginResultForCommand: command];
}
- (void)showInterstitial:(CDVInvokedUrlCommand *)command
{
NSString *adUnitIdentifier = [command argumentAtIndex: 0];
NSString *placement = [command argumentAtIndex: 1];
MAInterstitialAd *interstitial = [self retrieveInterstitialForAdUnitIdentifier: adUnitIdentifier];
[interstitial showAdForPlacement: placement];
[self sendOKPluginResultForCommand: command];
}
- (void)setInterstitialExtraParameter:(CDVInvokedUrlCommand *)command
{
NSString *adUnitIdentifier = [command argumentAtIndex: 0];
NSString *key = [command argumentAtIndex: 1];
NSString *value = [command argumentAtIndex: 2];
MAInterstitialAd *interstitial = [self retrieveInterstitialForAdUnitIdentifier: adUnitIdentifier];
[interstitial setExtraParameterForKey: key value: value];
[self sendOKPluginResultForCommand: command];
}
#pragma mark - Rewarded
- (void)loadRewardedAd:(CDVInvokedUrlCommand *)command
{
NSString *adUnitIdentifier = [command argumentAtIndex: 0];
MARewardedAd *rewardedAd = [self retrieveRewardedAdForAdUnitIdentifier: adUnitIdentifier];
[rewardedAd loadAd];
[self sendOKPluginResultForCommand: command];
}
- (void)showRewardedAd:(CDVInvokedUrlCommand *)command
{
NSString *adUnitIdentifier = [command argumentAtIndex: 0];
NSString *placement = [command argumentAtIndex: 1];
MARewardedAd *rewardedAd = [self retrieveRewardedAdForAdUnitIdentifier: adUnitIdentifier];
[rewardedAd showAdForPlacement: placement];
[self sendOKPluginResultForCommand: command];
}
- (void)setRewardedAdExtraParameter:(CDVInvokedUrlCommand *)command
{
NSString *adUnitIdentifier = [command argumentAtIndex: 0];
NSString *key = [command argumentAtIndex: 1];
NSString *value = [command argumentAtIndex: 2];
MARewardedAd *rewardedAd = [self retrieveRewardedAdForAdUnitIdentifier: adUnitIdentifier];
[rewardedAd setExtraParameterForKey: key value: value];
[self sendOKPluginResultForCommand: command];
}
#pragma mark - Ad Callbacks
- (void)didLoadAd:(MAAd *)ad
{
NSString *name;
MAAdFormat *adFormat = ad.format;
if ( MAAdFormat.banner == adFormat || MAAdFormat.leader == adFormat || MAAdFormat.mrec == adFormat )
{
MAAdView *adView = [self retrieveAdViewForAdUnitIdentifier: ad.adUnitIdentifier adFormat: adFormat];
// An ad is now being shown, enable user interaction.
adView.userInteractionEnabled = YES;
name = ( MAAdFormat.mrec == adFormat ) ? @"OnMRecAdLoadedEvent" : @"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.rewarded == adFormat )
{
name = @"OnRewardedAdLoadedEvent";
}
else
{
[self logInvalidAdFormat: adFormat];
return;
}
[self fireWindowEventWithName: name body: [self adInfoForAd: ad]];
}
- (void)didFailToLoadAdForAdUnitIdentifier:(NSString *)adUnitIdentifier withError:(MAError *)error
{
if ( !adUnitIdentifier )
{
[self log: @"adUnitIdentifier cannot be nil from %@", [NSThread callStackSymbols]];
return;
}
NSString *name;
if ( self.adViews[adUnitIdentifier] )
{
name = ( MAAdFormat.mrec == self.adViewAdFormats[adUnitIdentifier] ) ? @"OnMRecAdLoadFailedEvent" : @"OnBannerAdLoadFailedEvent";
}
else if ( self.interstitials[adUnitIdentifier] )
{
name = @"OnInterstitialLoadFailedEvent";
}
else if ( self.rewardedAds[adUnitIdentifier] )
{
name = @"OnRewardedAdLoadFailedEvent";
}
else
{
[self log: @"invalid adUnitId from %@", [NSThread callStackSymbols]];
return;
}
// TODO: Add "code", "message", and "adLoadFailureInfo"
NSString *errorCodeStr = [@(error.code) stringValue];
[self fireWindowEventWithName: name body: @{@"adUnitId" : adUnitIdentifier,
@"errorCode" : errorCodeStr}];
}
- (void)didClickAd:(MAAd *)ad
{
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.rewarded == adFormat )
{
name = @"OnRewardedAdClickedEvent";
}
else
{
[self logInvalidAdFormat: adFormat];
return;
}
[self fireWindowEventWithName: name body: [self adInfoForAd: ad]];
}
- (void)didDisplayAd:(MAAd *)ad
{
// BMLs do not support [DISPLAY] events in Unity
MAAdFormat *adFormat = ad.format;
if ( adFormat != MAAdFormat.interstitial && adFormat != MAAdFormat.rewarded ) return;
NSString *name;
if ( MAAdFormat.interstitial == adFormat )
{
name = @"OnInterstitialDisplayedEvent";
}
else // REWARDED
{
name = @"OnRewardedAdDisplayedEvent";
}
[self fireWindowEventWithName: name body: [self adInfoForAd: ad]];
}
- (void)didFailToDisplayAd:(MAAd *)ad withError:(MAError *)error
{
// BMLs do not support [DISPLAY] events in Unity
MAAdFormat *adFormat = ad.format;
if ( adFormat != MAAdFormat.interstitial && adFormat != MAAdFormat.rewarded ) return;
NSString *name;
if ( MAAdFormat.interstitial == adFormat )
{
name = @"OnInterstitialAdFailedToDisplayEvent";
}
else // REWARDED
{
name = @"OnRewardedAdFailedToDisplayEvent";
}
// TODO: Add "code", "message"
NSMutableDictionary *body = [@{@"errorCode" : @(error.code)} mutableCopy];
[body addEntriesFromDictionary: [self adInfoForAd: ad]];
[self fireWindowEventWithName: name body: body];
}
- (void)didHideAd:(MAAd *)ad
{
// BMLs do not support [HIDDEN] events in Unity
MAAdFormat *adFormat = ad.format;
if ( adFormat != MAAdFormat.interstitial && adFormat != MAAdFormat.rewarded ) return;
NSString *name;
if ( MAAdFormat.interstitial == adFormat )
{
name = @"OnInterstitialHiddenEvent";
}
else // REWARDED
{
name = @"OnRewardedAdHiddenEvent";
}
[self fireWindowEventWithName: name body: [self adInfoForAd: ad]];
}
- (void)didExpandAd:(MAAd *)ad
{
MAAdFormat *adFormat = ad.format;
if ( adFormat != MAAdFormat.banner && adFormat != MAAdFormat.leader && adFormat != MAAdFormat.mrec )
{
[self logInvalidAdFormat: adFormat];
return;
}
[self fireWindowEventWithName: ( MAAdFormat.mrec == adFormat ) ? @"OnMRecAdExpandedEvent" : @"OnBannerAdExpandedEvent"
body: [self adInfoForAd: ad]];
}
- (void)didCollapseAd:(MAAd *)ad
{
MAAdFormat *adFormat = ad.format;
if ( adFormat != MAAdFormat.banner && adFormat != MAAdFormat.leader && adFormat != MAAdFormat.mrec )
{
[self logInvalidAdFormat: adFormat];
return;
}
[self fireWindowEventWithName: ( MAAdFormat.mrec == adFormat ) ? @"OnMRecAdCollapsedEvent" : @"OnBannerAdCollapsedEvent"
body: [self adInfoForAd: ad]];
}
- (void)didRewardUserForAd:(MAAd *)ad withReward:(MAReward *)reward
{
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];
NSMutableDictionary *body = [@{@"rewardLabel": rewardLabel,
@"rewardAmount": rewardAmount} mutableCopy];
[body addEntriesFromDictionary: [self adInfoForAd: ad]];
[self fireWindowEventWithName: @"OnRewardedAdReceivedRewardEvent" body: body];
}
#pragma mark - Internal Methods
- (void)createAdViewWithAdUnitIdentifier:(NSString *)adUnitIdentifier adFormat:(MAAdFormat *)adFormat atPosition:(NSString *)adViewPosition command:(CDVInvokedUrlCommand *)command
{
[self log: @"Creating %@ with ad unit identifier \"%@\" and position: \"%@\"", adFormat, adUnitIdentifier, adViewPosition];
// Retrieve ad view from the map
MAAdView *adView = [self retrieveAdViewForAdUnitIdentifier: adUnitIdentifier adFormat: adFormat atPosition: adViewPosition];
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];
[adView loadAd];
// 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 command: nil];
[self.adUnitIdentifiersToShowAfterCreate removeObject: adUnitIdentifier];
}
[self sendOKPluginResultForCommand: command];
}
- (void)setAdViewBackgroundColorForAdUnitIdentifier:(NSString *)adUnitIdentifier adFormat:(MAAdFormat *)adFormat hexColorCode:(NSString *)hexColorCode command:(CDVInvokedUrlCommand *)command
{
[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
UIColor *convertedColor;
if ( [hexColorCode containsString: @"FF000000"] )
{
convertedColor = [UIColor al_colorWithHexString: @"FF000001"];
}
else
{
convertedColor = [UIColor al_colorWithHexString: hexColorCode];
}
MAAdView *view = [self retrieveAdViewForAdUnitIdentifier: adUnitIdentifier adFormat: adFormat];
self.publisherBannerBackgroundColor = convertedColor;
self.safeAreaBackground.backgroundColor = view.backgroundColor = convertedColor;
[self sendOKPluginResultForCommand: command];
}
- (void)setAdViewPlacement:(nullable NSString *)placement forAdUnitIdentifier:(NSString *)adUnitIdentifier adFormat:(MAAdFormat *)adFormat command:(CDVInvokedUrlCommand *)command
{
[self log: @"Setting placement \"%@\" for \"%@\" with ad unit identifier \"%@\"", placement, adFormat, adUnitIdentifier];
MAAdView *adView = [self retrieveAdViewForAdUnitIdentifier: adUnitIdentifier adFormat: adFormat];
adView.placement = placement;
[self sendOKPluginResultForCommand: command];
}
- (void)updateAdViewPosition:(NSString *)adViewPosition forAdUnitIdentifier:(NSString *)adUnitIdentifier adFormat:(MAAdFormat *)adFormat command:(CDVInvokedUrlCommand *)command
{
// Check if the previous position is same as the new position. If so, no need to update the position again.
NSString *previousPosition = self.adViewPositions[adUnitIdentifier];
if ( !adViewPosition || [adViewPosition isEqualToString: previousPosition] ) return;
self.adViewPositions[adUnitIdentifier] = adViewPosition;
[self positionAdViewForAdUnitIdentifier: adUnitIdentifier adFormat: adFormat];
[self sendOKPluginResultForCommand: command];
}
- (void)setAdViewExtraParameterForAdUnitIdentifier:(NSString *)adUnitIdentifier adFormat:(MAAdFormat *)adFormat key:(NSString *)key value:(nullable NSString *)value command:(CDVInvokedUrlCommand *)command
{
[self log: @"Setting %@ extra with key: \"%@\" value: \"%@\"", adFormat, key, value];
MAAdView *adView = [self retrieveAdViewForAdUnitIdentifier: adUnitIdentifier adFormat: adFormat];
[adView setExtraParameterForKey: key value: value];
if ( [@"force_banner" isEqualToString: key] && MAAdFormat.mrec != adFormat )
{
// Handle local changes as needed
MAAdFormat *adFormat;
BOOL shouldForceBanner = [NSNumber al_numberWithString: value].boolValue;
if ( shouldForceBanner )
{
adFormat = MAAdFormat.banner;
}
else
{
adFormat = DEVICE_SPECIFIC_ADVIEW_AD_FORMAT;
}
self.adViewAdFormats[adUnitIdentifier] = adFormat;
[self positionAdViewForAdUnitIdentifier: adUnitIdentifier adFormat: adFormat];
}
[self sendOKPluginResultForCommand: command];
}
- (void)showAdViewWithAdUnitIdentifier:(NSString *)adUnitIdentifier adFormat:(MAAdFormat *)adFormat command:(nullable CDVInvokedUrlCommand *)command
{
[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];
}
self.safeAreaBackground.hidden = NO;
view.hidden = NO;
[view startAutoRefresh];
[self sendOKPluginResultForCommand: command];
}
- (void)hideAdViewWithAdUnitIdentifier:(NSString *)adUnitIdentifier adFormat:(MAAdFormat *)adFormat command:(CDVInvokedUrlCommand *)command
{
[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];
[self sendOKPluginResultForCommand: command];
}
- (void)destroyAdViewWithAdUnitIdentifier:(NSString *)adUnitIdentifier adFormat:(MAAdFormat *)adFormat command:(CDVInvokedUrlCommand *)command
{
[self log: @"Destroying %@ with ad unit identifier \"%@\"", adFormat, adUnitIdentifier];
MAAdView *view = [self retrieveAdViewForAdUnitIdentifier: adUnitIdentifier adFormat: adFormat];
view.delegate = nil;
[view removeFromSuperview];
[self.adViews removeObjectForKey: adUnitIdentifier];
[self.adViewPositions removeObjectForKey: adUnitIdentifier];
[self.adViewAdFormats removeObjectForKey: adUnitIdentifier];
[self sendOKPluginResultForCommand: command];
}
- (void)logInvalidAdFormat:(MAAdFormat *)adFormat
{
[self log: @"invalid ad format: %@, from %@", adFormat, [NSThread callStackSymbols]];
}
- (void)log:(NSString *)format, ...