19
19
@implementation RNAdMobUnifiedAdQueueWrapper {
20
20
GADAdLoader* adLoader;
21
21
GAMRequest* adRequest;
22
-
22
+
23
23
NSMutableSet <id <AdListener>>* attachedAdListeners;
24
24
UnifiedNativeAdLoadedListener* unifiedNativeAdLoadedListener;
25
25
GADVideoOptions* adVideoOptions;
26
26
GADNativeAdMediaAdLoaderOptions* adMediaOptions;
27
27
GADNativeAdViewAdOptions* adPlacementOptions;
28
+ GADNativeAdCustomClickGestureOptions *clickGestureOptions;
28
29
NSDictionary * targetingOptions;
29
30
int loadingAdRequestCount;
30
31
int retryDelay;
@@ -45,10 +46,10 @@ -(instancetype)initWithConfig:(NSDictionary *)config repo:(NSString *)repo{
45
46
adVideoOptions = [[GADVideoOptions alloc ]init];
46
47
adMediaOptions = [[GADNativeAdMediaAdLoaderOptions alloc ] init ];
47
48
adPlacementOptions = [[GADNativeAdViewAdOptions alloc ]init];
48
-
49
+
49
50
attachedAdListeners = [[NSMutableSet <id <AdListener>> alloc] init ];
50
51
}
51
-
52
+
52
53
// Set repository settings
53
54
_adUnitId = [config objectForKey: @" adUnitId" ] ;
54
55
_name = repo;
@@ -58,29 +59,29 @@ -(instancetype)initWithConfig:(NSDictionary *)config repo:(NSString *)repo{
58
59
if ([config objectForKey: @" totalRetryCount" ]){
59
60
totalRetryCount = ((NSNumber *)[config objectForKey: @" totalRetryCount" ]).boolValue ;
60
61
}
61
-
62
+
62
63
if ([config objectForKey: @" retryDelay" ]){
63
64
retryDelay = ((NSNumber *)[config objectForKey: @" retryDelay" ]).boolValue ;
64
65
}
65
-
66
+
66
67
_nativeAds = [[NSMutableArray <RNAdMobUnifiedAdContainer *> alloc]init];
67
-
68
+
68
69
if ([config objectForKey: @" expirationPeriod" ]){
69
70
_expirationInterval = ((NSNumber *)[config objectForKey: @" expirationPeriod" ]).intValue ;
70
71
}
71
72
if ([config objectForKey: @" mediationEnabled" ]){
72
73
_isMediationEnabled = ((NSNumber *)[config objectForKey: @" mediationEnabled" ]).boolValue ;
73
74
}
74
-
75
-
75
+
76
+
76
77
// Set request options
77
78
if ([config objectForKey: @" adChoicesPlacement" ]){
78
79
[adPlacementOptions setPreferredAdChoicesPosition: ((NSNumber *)[config objectForKey: @" adChoicesPlacement" ]).intValue];
79
80
}
80
81
if ([config objectForKey: @" mediaAspectRatio" ]){
81
82
[adMediaOptions setMediaAspectRatio: ((NSNumber *)[config objectForKey: @" mediaAspectRatio" ]).intValue];
82
83
}
83
-
84
+
84
85
if ([config objectForKey: @" videoOptions" ]){
85
86
[self configVideoOptions: [config objectForKey: @" videoOptions" ]];
86
87
}
@@ -90,15 +91,38 @@ -(instancetype)initWithConfig:(NSDictionary *)config repo:(NSString *)repo{
90
91
if ([config objectForKey: @" targetingOptions" ]){
91
92
[self configTargetOptions: [config objectForKey: @" targetingOptions" ]];
92
93
}
93
-
94
-
94
+
95
+ if ([config valueForKey: @" swipeGestureDirection" ]) {
96
+
97
+ clickGestureOptions = [[GADNativeAdCustomClickGestureOptions alloc ] initWithSwipeGestureDirection: UISwipeGestureRecognizerDirectionUp tapsAllowed: false ];
98
+
99
+ int direction = ((NSNumber *)[config objectForKey: @" swipeGestureDirection" ]).intValue ;
100
+
101
+ if (direction == 1 ) {
102
+ [clickGestureOptions setSwipeGestureDirection: UISwipeGestureRecognizerDirectionRight];
103
+ } else if (direction == 2 ) {
104
+ [clickGestureOptions setSwipeGestureDirection: UISwipeGestureRecognizerDirectionLeft];
105
+ } else if (direction == 4 ) {
106
+ [clickGestureOptions setSwipeGestureDirection: UISwipeGestureRecognizerDirectionUp];
107
+ } else if (direction == 8 ) {
108
+ [clickGestureOptions setSwipeGestureDirection: UISwipeGestureRecognizerDirectionDown];
109
+ }
110
+
111
+ if ([config valueForKey: @" tapsAllowed" ]) {
112
+ [clickGestureOptions setTapsAllowed: [config valueForKey: @" tapsAllowed" ]];
113
+ }
114
+ }
115
+
116
+
117
+
118
+
95
119
if ([config objectForKey: @" requestNonPersonalizedAdsOnly" ]){
96
120
GADCustomEventExtras *extras = [[GADCustomEventExtras alloc ] init ];
97
121
bool npa = ((NSNumber *)[config objectForKey: @" requestNonPersonalizedAdsOnly" ]).boolValue ;
98
122
[extras setExtras: @{@" npa" : @([NSNumber numberWithInt: npa].intValue )} forLabel: @" npa" ];
99
123
[adRequest registerAdNetworkExtras: extras];
100
124
}
101
-
125
+
102
126
unifiedNativeAdLoadedListener = [[UnifiedNativeAdLoadedListener alloc ]initWithRepo:repo nativeAds: _nativeAds tAds: _totalAds];
103
127
return self;
104
128
}
@@ -114,22 +138,27 @@ -(void) detachAdListener:(id<AdListener>) listener{
114
138
if use mediation,you can't use GADMultipleAdsAdLoaderOptions for load ads
115
139
*/
116
140
-(void ) fillAds {
117
-
141
+
118
142
int require2fill = _totalAds-((int )_nativeAds.count );
119
-
143
+
120
144
if ( [self isLoading ] || require2fill<=0 ){
121
145
return ;
122
146
}
147
+
123
148
NSMutableArray <GADAdLoaderOptions *>* options = [NSMutableArray arrayWithArray: @[adMediaOptions,adVideoOptions,adPlacementOptions]];
124
-
149
+
150
+ if (clickGestureOptions) {
151
+ [options addObject: clickGestureOptions];
152
+ }
153
+
125
154
if (!_isMediationEnabled) {
126
155
GADMultipleAdsAdLoaderOptions* multipleAdsOptions = [[GADMultipleAdsAdLoaderOptions alloc ] init ];
127
156
multipleAdsOptions.numberOfAds = MAX (require2fill,0 );
128
157
[options addObject: multipleAdsOptions];
129
158
}
130
159
adLoader = [[GADAdLoader alloc ] initWithAdUnitID: _adUnitId rootViewController: nil adTypes: @[GADAdLoaderAdTypeNative] options: options];
131
160
[adLoader setDelegate: self ];
132
-
161
+
133
162
loadingAdRequestCount = require2fill;
134
163
if (_isMediationEnabled){
135
164
printf (" admob request count:" ,MIN (require2fill,5 ));
@@ -144,14 +173,14 @@ -(void) fillAds{
144
173
-(RNAdMobUnifiedAdContainer*) getAd {
145
174
long long now = (long long )([[NSDate date ] timeIntervalSince1970 ] * 1000.0 );
146
175
RNAdMobUnifiedAdContainer *ad = nil ;
147
-
176
+
148
177
if (!(_nativeAds.count == 0 )){
149
178
// sortAds
150
179
[_nativeAds sortUsingComparator: ^NSComparisonResult (id <Comparable, NSObject > _Nonnull obj1,
151
180
id <Comparable, NSObject > _Nonnull obj2) {
152
181
return [obj1 compareTo: obj2] > 0 ; // find lowest showCount
153
182
}];
154
-
183
+
155
184
NSMutableArray <RNAdMobUnifiedAdContainer *> *discardedItems = [NSMutableArray <RNAdMobUnifiedAdContainer *> array];
156
185
for (RNAdMobUnifiedAdContainer *item in self.nativeAds ) {
157
186
if (item != nil && (now - item.loadTime ) < _expirationInterval) {
@@ -188,89 +217,101 @@ - (void)adLoader:(nonnull GADAdLoader *)adLoader didReceiveNativeAd:(nonnull GAD
188
217
loadingAdRequestCount--;
189
218
retryCount = 0 ;
190
219
[unifiedNativeAdLoadedListener adLoader: adLoader didReceiveNativeAd: nativeAd];
191
-
220
+
192
221
// to prevent a crash (check the link below), first copy attachedAdListeners into a new array
193
222
// link:https://stackoverflow.com/questions/44648610/collection-nsarraym-was-mutated-while-being-enumerated
194
223
for (id <AdListener> listener in [attachedAdListeners copy ]){
195
224
[listener didAdLoaded: nativeAd];
196
225
}
197
- [EventEmitter.sharedInstance sendEvent: [CacheManager EVENT_AD_PRELOAD_LOADED: _name] dict: nil ];
198
-
226
+ [EventEmitter.sharedInstance sendEvent: [CacheManager EVENT_AD_PRELOAD_LOADED: _name] dict: [ self getDefaultEventData ] ];
227
+
199
228
if (loadingAdRequestCount == 0 ){
200
229
[self fillAds ];// fill up repository if need
201
230
}
202
231
// The adLoader has finished loading ads, and a new request can be sent.
203
232
}
204
233
234
+ - (NSMutableDictionary *)getDefaultEventData {
235
+ NSMutableDictionary *dict = [NSMutableDictionary dictionary ];
236
+ [dict setValue: @" adUnitId" forKey: _adUnitId];
237
+ [dict setValue: @" repo" forKey: _name];
238
+ return dict;
239
+ }
240
+
205
241
- (void )nativeAdDidRecordClick : (GADNativeAd *)nativeAd {
206
- [EventEmitter.sharedInstance sendEvent: [CacheManager EVENT_AD_CLICKED: _name] dict: nil ];
242
+ [EventEmitter.sharedInstance sendEvent: [CacheManager EVENT_AD_CLICKED: _name] dict: [ self getDefaultEventData ] ];
207
243
}
208
244
209
245
- (void )nativeAdDidRecordImpression : (GADNativeAd *)nativeAd {
210
- [EventEmitter.sharedInstance sendEvent: [CacheManager EVENT_AD_IMPRESSION: _name] dict: nil ];
246
+ [EventEmitter.sharedInstance sendEvent: [CacheManager EVENT_AD_IMPRESSION: _name] dict: [ self getDefaultEventData ] ];
211
247
}
212
248
213
249
- (void )nativeAdWillPresentScreen : (GADNativeAd *)nativeAd {
214
- [EventEmitter.sharedInstance sendEvent: [CacheManager EVENT_AD_OPEN: _name] dict: nil ];
250
+ [EventEmitter.sharedInstance sendEvent: [CacheManager EVENT_AD_OPEN: _name] dict: [ self getDefaultEventData ] ];
215
251
}
216
252
217
253
- (void )nativeAdDidDismissScreen : (GADNativeAd *)nativeAd {
218
- [EventEmitter.sharedInstance sendEvent: [CacheManager EVENT_AD_CLOSED: _name] dict: nil ];
254
+ [EventEmitter.sharedInstance sendEvent: [CacheManager EVENT_AD_CLOSED: _name] dict: [ self getDefaultEventData ] ];
219
255
}
220
256
221
257
- (void )adLoader : (nonnull GADAdLoader *)adLoader didFailToReceiveAdWithError : (nonnull NSError *)error {
222
- if (_isMediationEnabled){
223
- loadingAdRequestCount--;
224
- }else {
225
- loadingAdRequestCount = 0 ;
226
- }
227
-
258
+ if (_isMediationEnabled){
259
+ loadingAdRequestCount--;
260
+ }else {
261
+ loadingAdRequestCount = 0 ;
262
+ }
263
+
228
264
[unifiedNativeAdLoadedListener adLoader: adLoader didFailToReceiveAdWithError: error];
229
-
230
-
265
+
266
+
231
267
BOOL stopPreloading = false ;
232
268
switch (error.code ) {
233
269
case GADErrorInternalError:
234
270
case GADErrorInvalidRequest:
235
271
stopPreloading = true ;
236
272
break ;
237
273
}
238
-
274
+
239
275
if (stopPreloading) {
276
+
240
277
NSDictionary *errorDic = @{
241
- @" domain" :error.domain ,
242
- @" message" :error.localizedDescription ,
243
- @" code" :@(error.code ).stringValue
244
- };
245
- NSDictionary *event = @{
246
- @" error" :errorDic,
247
- };
248
-
249
- [EventEmitter.sharedInstance sendEvent: [CacheManager EVENT_AD_PRELOAD_ERROR: _name] dict: event];
278
+ @" domain" :error.domain ,
279
+ @" message" :error.localizedDescription ,
280
+ @" code" :@(error.code ).stringValue
281
+ };
282
+
283
+ NSMutableDictionary *dict = [self getDefaultEventData ];
284
+ [dict addEntriesFromDictionary: @{
285
+ @" error" :errorDic,
286
+ }];
287
+
288
+ [EventEmitter.sharedInstance sendEvent: [CacheManager EVENT_AD_PRELOAD_ERROR: _name] dict: dict];
250
289
251
- for (id <AdListener> listener in [attachedAdListeners copy ]){
252
- [listener didFailToReceiveAdWithError: error];
253
- }
254
- return ;
290
+ for (id <AdListener> listener in [attachedAdListeners copy ]){
291
+ [listener didFailToReceiveAdWithError: error];
292
+ }
293
+ return ;
255
294
}
256
-
295
+
257
296
if (retryCount >= totalRetryCount){
258
297
NSDictionary *errorDic = @{
259
- @" domain" :@" " ,
260
- @" message" :@" reach max retry" ,
261
- @" code" :@" "
262
- };
263
- NSDictionary *event = @{
264
- @" error" :errorDic,
265
- };
266
- [EventEmitter.sharedInstance sendEvent: [CacheManager EVENT_AD_PRELOAD_ERROR: _name] dict: event];
298
+ @" domain" :@" " ,
299
+ @" message" :@" reach max retry" ,
300
+ @" code" :@" "
301
+ };
302
+ NSMutableDictionary *dict = [self getDefaultEventData ];
303
+ [dict addEntriesFromDictionary: @{
304
+ @" error" :errorDic,
305
+ }];
306
+
307
+ [EventEmitter.sharedInstance sendEvent: [CacheManager EVENT_AD_PRELOAD_ERROR: _name] dict: dict];
267
308
for (id <AdListener> listener in [attachedAdListeners copy ]){
268
- [listener didFailToReceiveAdWithError: error];
269
- }
309
+ [listener didFailToReceiveAdWithError: error];
310
+ }
270
311
return ;
271
312
}
272
-
273
-
313
+
314
+
274
315
if (loadingAdRequestCount == 0 ) {
275
316
retryCount++;
276
317
__weak typeof (self) weakSelf = self;
@@ -279,42 +320,42 @@ - (void)adLoader:(nonnull GADAdLoader *)adLoader didFailToReceiveAdWithError:(no
279
320
[weakSelf fillAds ];
280
321
});
281
322
}
282
-
323
+
283
324
}
284
325
285
326
-(void )configVideoOptions : (NSDictionary *)config {
286
-
327
+
287
328
if ([config objectForKey: @" mute" ]) {
288
329
bool muted = ((NSNumber *)[config objectForKey: @" mute" ]).boolValue ;
289
330
[adVideoOptions setStartMuted: muted];
290
331
}
291
-
332
+
292
333
if ([config objectForKey: @" clickToExpand" ]) {
293
334
bool clickToExpand = ((NSNumber *)[config objectForKey: @" clickToExpand" ]).boolValue ;
294
335
[adVideoOptions setClickToExpandRequested: clickToExpand];
295
336
}
296
-
337
+
297
338
if ([config objectForKey: @" customControlsRequested" ]) {
298
339
bool customControlsRequested = ((NSNumber *)[config objectForKey: @" customControlsRequested" ]).boolValue ;
299
340
[adVideoOptions setCustomControlsRequested: customControlsRequested];
300
341
}
301
342
}
302
343
303
344
-(void )configTargetOptions : (NSDictionary *)config {
304
-
345
+
305
346
if ([config objectForKey: @" targets" ]){
306
347
NSArray <NSDictionary *>* targets = (NSArray <NSDictionary *> *)[config objectForKey: @" targets" ];
307
348
for (NSDictionary * target in targets){
308
- [adRequest setCustomTargeting: target];
349
+ [adRequest setCustomTargeting: target];
309
350
}
310
-
351
+
311
352
if ([config objectForKey: @" categoryExclusions" ]){
312
353
[adRequest setCategoryExclusions: (NSArray <NSString *> *)[config objectForKey: @" categoryExclusions" ]];
313
354
}
314
355
if ([config objectForKey: @" publisherId" ]){
315
356
[adRequest setPublisherProvidedID: (NSString *)[config objectForKey: @" publisherId" ]];
316
357
}
317
-
358
+
318
359
if ([config objectForKey: @" requestAgent" ]){
319
360
[adRequest setRequestAgent: (NSString *)[config objectForKey: @" requestAgent" ]];
320
361
}
@@ -327,23 +368,23 @@ -(void)configTargetOptions:(NSDictionary *)config{
327
368
if ([config objectForKey: @" neighboringContentUrls" ]){
328
369
[adRequest setNeighboringContentURLStrings: (NSArray <NSString *> *)[config objectForKey: @" neighboringContentUrls" ]];
329
370
}
330
-
371
+
331
372
}
332
373
}
333
374
334
375
-(void )configMediationOptions : (NSDictionary *)config {
335
376
#ifdef MEDIATION_FACEBOOK
336
- GADFBNetworkExtras * extras = [[GADFBNetworkExtras alloc ] init ];
337
-
338
- if ([config valueForKey: @" nativeBanner" ]) {
339
- extras.nativeAdFormat = GADFBAdFormatNative;
340
- } else {
341
- extras.nativeAdFormat = GADFBAdFormatNativeBanner;
342
- }
343
-
344
- [adRequest registerAdNetworkExtras: extras];
377
+ GADFBNetworkExtras * extras = [[GADFBNetworkExtras alloc ] init ];
378
+
379
+ if ([config valueForKey: @" nativeBanner" ]) {
380
+ extras.nativeAdFormat = GADFBAdFormatNative;
381
+ } else {
382
+ extras.nativeAdFormat = GADFBAdFormatNativeBanner;
383
+ }
384
+
385
+ [adRequest registerAdNetworkExtras: extras];
345
386
#endif
346
-
387
+
347
388
}
348
389
349
390
@end
0 commit comments