-
Notifications
You must be signed in to change notification settings - Fork 144
/
BranchSDK.m
749 lines (617 loc) · 30.6 KB
/
BranchSDK.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
#import "BranchSDK.h"
NSString * const pluginVersion = @"%BRANCH_PLUGIN_VERSION%";
@interface BranchSDK()
@property (strong, nonatomic) NSString *deepLinkUrl;
- (void)doShareLinkResponse:(int)callbackId sendResponse:(NSDictionary*)response;
@end
@implementation BranchSDK
- (void)pluginInitialize
{
self.branchUniversalObjArray = [[NSMutableArray alloc] init];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(handleOpenURLNotification:) name:CDVPluginHandleOpenURLNotification object:nil];
}
- (void)handleOpenURLNotification:(NSNotification*)notification
{
NSURL* url = [notification object];
[[Branch getInstance] application:[UIApplication sharedApplication] openURL:url options:@{}];
}
#pragma mark - Private APIs
#pragma mark - Global Instance Accessors
- (Branch *)getInstance
{
return [Branch getInstance];
}
- (Branch *)getInstance:(NSString *)branchKey
{
if (branchKey) {
return [Branch getInstance:branchKey];
}
else {
return [Branch getInstance];
}
}
- (Branch *)getTestInstance
{
return [Branch getTestInstance];
}
#pragma mark - Deep Linking Handlers
- (id)handleDeepLink:(CDVInvokedUrlCommand*)command
{
NSString *arg = [command.arguments objectAtIndex:0];
NSURL *url = [NSURL URLWithString:arg];
self.deepLinkUrl = [url absoluteString];
return [NSNumber numberWithBool:[[Branch getInstance] handleDeepLink:url]];
}
- (id)handleDeepLinkWithNewSession:(CDVInvokedUrlCommand*)command
{
NSString *arg = [command.arguments objectAtIndex:0];
NSURL *url = [NSURL URLWithString:arg];
self.deepLinkUrl = [url absoluteString];
return [NSNumber numberWithBool:[[Branch getInstance] handleDeepLinkWithNewSession:url]];
}
- (void)continueUserActivity:(CDVInvokedUrlCommand*)command
{
NSString *activityType = (NSString *)[command.arguments objectAtIndex:0];
NSDictionary *userInfo = (NSDictionary*)[command.arguments objectAtIndex:1];
NSUserActivity *userActivity = [[NSUserActivity alloc] initWithActivityType:activityType];
[userActivity setUserInfo:userInfo];
if ([userActivity.activityType isEqualToString:NSUserActivityTypeBrowsingWeb]) {
self.deepLinkUrl = [userActivity.webpageURL absoluteString];
}
[[Branch getInstance] continueUserActivity:userActivity];
}
#pragma mark - Public APIs
#pragma mark - Branch Basic Methods
- (void)enableTestMode:(CDVInvokedUrlCommand*)command
{
[Branch setUseTestBranchKey:TRUE];
}
- (void)initSession:(CDVInvokedUrlCommand*)command
{
[[Branch getInstance] registerPluginName:@"CordovaIonic" version:pluginVersion];
[[Branch getInstance] initSessionWithLaunchOptions:nil andRegisterDeepLinkHandler:^(NSDictionary *params, NSError *error) {
NSString *resultString = nil;
CDVPluginResult *pluginResult = nil;
if (!error) {
if (params != nil && [params count] > 0) {
NSError *err;
NSData *jsonData = [NSJSONSerialization dataWithJSONObject:params options:0 error:&err];
if (!jsonData) {
NSLog(@"Parsing Error: %@", [err localizedDescription]);
NSDictionary *errorDict = [NSDictionary dictionaryWithObjectsAndKeys:[err localizedDescription], @"error", nil];
NSData* errorJSON = [NSJSONSerialization dataWithJSONObject:errorDict options:NSJSONWritingPrettyPrinted error:&err];
resultString = [[NSString alloc] initWithData:errorJSON encoding:NSUTF8StringEncoding];
pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_ERROR messageAsString:resultString];
} else {
resultString = [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];
pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsDictionary:params];
}
}
}
else {
NSLog(@"Init Error: %@", [error localizedDescription]);
// We create a JSON string result, because we're getting an error if we directly return a string result.
NSDictionary *errorDict = [NSDictionary dictionaryWithObjectsAndKeys:[error localizedDescription], @"error", nil];
NSData* errorJSON = [NSJSONSerialization dataWithJSONObject:errorDict options:NSJSONWritingPrettyPrinted error:&error];
resultString = [[NSString alloc] initWithData:errorJSON encoding:NSUTF8StringEncoding];
pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_ERROR messageAsString:resultString];
}
if (command != nil) {
[self.commandDelegate sendPluginResult: pluginResult callbackId: command.callbackId];
}
}];
}
- (void)setRequestMetadata:(CDVInvokedUrlCommand*)command
{
[[Branch getInstance] setRequestMetadataKey:[command.arguments objectAtIndex:0] value:[command.arguments objectAtIndex:1]];
}
- (void)disableTracking:(CDVInvokedUrlCommand*)command
{
bool enabled = [[command.arguments objectAtIndex:0] boolValue];
[Branch setTrackingDisabled:enabled];
CDVPluginResult* pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsBool:enabled];
[self.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId];
}
- (void)enableLogging:(CDVInvokedUrlCommand*)command
{
bool enableLogging = [[command.arguments objectAtIndex:0] boolValue];
if (enableLogging) {
[[Branch getInstance] enableLogging];
}
CDVPluginResult* pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsBool:enableLogging];
[self.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId];
}
- (void)getAutoInstance:(CDVInvokedUrlCommand*)command
{
[self initSession:nil];
}
- (void)getLatestReferringParams:(CDVInvokedUrlCommand*)command
{
Branch *branch = [self getInstance];
NSDictionary *sessionParams = [branch getLatestReferringParams];
CDVPluginResult* pluginResult = nil;
if (sessionParams != nil && [sessionParams count] > 0) {
pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsDictionary:sessionParams];
} else {
pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsBool:FALSE];
}
[self.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId];
}
- (void)getFirstReferringParams:(CDVInvokedUrlCommand*)command
{
Branch *branch = [self getInstance];
NSDictionary *installParams = [branch getFirstReferringParams];
CDVPluginResult* pluginResult = nil;
if (installParams != nil && [installParams count] > 0) {
pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsDictionary:installParams];
} else {
pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsBool:FALSE];
}
[self.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId];
}
- (void)setIdentity:(CDVInvokedUrlCommand*)command
{
Branch *branch = [self getInstance];
[branch setIdentity:[command.arguments objectAtIndex:0] withCallback:^(NSDictionary *params, NSError *error) {
CDVPluginResult* pluginResult = nil;
if (!error) {
pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsDictionary:params];
}
else {
pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_ERROR messageAsString:[error localizedDescription]];
}
[self.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId];
}];
}
- (void)registerDeepLinkController:(CDVInvokedUrlCommand*)command
{
UIViewController<BranchDeepLinkingController> *controller = (UIViewController<BranchDeepLinkingController>*)self.viewController;
Branch *branch = [self getInstance];
[branch registerDeepLinkController:controller forKey:[command.arguments objectAtIndex:0]];
}
-(void)sendBranchEvent:(CDVInvokedUrlCommand*)command
{
NSString *eventName = [command.arguments objectAtIndex:0];
NSDictionary *metadata;
if ([command.arguments count] == 2) {
metadata = [command.arguments objectAtIndex:1];
}
BranchEvent *event = [BranchEvent customEventWithName:eventName];
for (id key in metadata) {
if ([key isEqualToString:@"transactionID"]) {
event.transactionID = [metadata objectForKey:key];
}
else if ([key isEqualToString:@"currency"]) {
event.currency = [metadata objectForKey:key];
}
else if ([key isEqualToString:@"shipping"]) {
NSString *value = ([[metadata objectForKey:key] isKindOfClass:[NSString class]]) ? [metadata objectForKey:key] : [[metadata objectForKey:key] stringValue];
event.shipping = [NSDecimalNumber decimalNumberWithString:value];
}
else if ([key isEqualToString:@"tax"]) {
NSString *value = ([[metadata objectForKey:key] isKindOfClass:[NSString class]]) ? [metadata objectForKey:key] : [[metadata objectForKey:key] stringValue];
event.tax = [NSDecimalNumber decimalNumberWithString:value];
}
else if ([key isEqualToString:@"coupon"]) {
event.coupon = [metadata objectForKey:key];
}
else if ([key isEqualToString:@"affiliation"]) {
event.affiliation = [metadata objectForKey:key];
}
else if ([key isEqualToString:@"eventDescription"]) {
event.eventDescription = [metadata objectForKey:key];
}
else if ([key isEqualToString:@"revenue"]) {
NSString *value = ([[metadata objectForKey:key] isKindOfClass:[NSString class]]) ? [metadata objectForKey:key] : [[metadata objectForKey:key] stringValue];
event.revenue = [NSDecimalNumber decimalNumberWithString:value];
}
else if ([key isEqualToString:@"searchQuery"]) {
event.searchQuery = [metadata objectForKey:key];
}
else if ([key isEqualToString:@"description"]) {
event.eventDescription = [metadata objectForKey:key];
}
else if ([key isEqualToString:@"customerEventAlias"]) {
event.alias = [metadata objectForKey:key];
}
else if ([key isEqualToString:@"customData"] && [[metadata objectForKey:key] isKindOfClass:[NSMutableDictionary class]]) {
event.customData = [metadata objectForKey:key];
}
else if ([key isEqualToString:@"contentMetadata"]){
NSMutableArray *mArray = [[NSMutableArray alloc]init];
for (NSDictionary *dataDictionary in [metadata objectForKey:key]){
BranchUniversalObject *contentItem = [BranchUniversalObject objectWithDictionary:(dataDictionary)];
[mArray addObject:contentItem];
}
event.contentItems = [mArray copy];
}
}
[event logEvent];
}
- (void)logout:(CDVInvokedUrlCommand*)command
{
Branch *branch = [self getInstance];
[branch logoutWithCallback:^(BOOL changed, NSError *error) {
CDVPluginResult *pluginResult = nil;
if (!error) {
pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsBool:changed];
} else {
pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_ERROR messageAsString:[error localizedDescription]];
}
[self.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId];
}];
self.branchUniversalObjArray = [[NSMutableArray alloc] init];
}
- (void)setDMAParamsForEEA:(CDVInvokedUrlCommand*)command {
if (command.arguments.count < 3) {
CDVPluginResult* pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_ERROR messageAsString:@"Insufficient arguments"];
[self.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId];
return;
}
BOOL eeaRegion = [[command.arguments objectAtIndex:0] boolValue];
BOOL adPersonalizationConsent = [[command.arguments objectAtIndex:1] boolValue];
BOOL adUserDataUsageConsent = [[command.arguments objectAtIndex:2] boolValue];
[Branch setDMAParamsForEEA:eeaRegion AdPersonalizationConsent:adPersonalizationConsent AdUserDataUsageConsent:adUserDataUsageConsent];
CDVPluginResult* pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK];
[self.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId];
}
#pragma mark - Branch Universal Object Methods
- (void)createBranchUniversalObject:(CDVInvokedUrlCommand*)command
{
NSDictionary *properties = [command.arguments objectAtIndex:0];
BranchUniversalObject *branchUniversalObj = [[BranchUniversalObject alloc] init];
for (id key in properties) {
if ([key isEqualToString:@"contentMetadata"]){
NSMutableDictionary<NSString *,NSString *> *metadata = (NSMutableDictionary<NSString *,NSString *> *)[properties valueForKey:key];
[[branchUniversalObj contentMetadata] setCustomMetadata:metadata];
}
else if ([key isEqualToString:@"contentIndexingMode"]) {
NSString *indexingMode = [properties valueForKey:key];
// Default contentIndexMode is always public
if ([indexingMode isEqualToString:@"private"]) {
branchUniversalObj.publiclyIndex = false;
}
else {
branchUniversalObj.publiclyIndex = true;
}
}
else if ([key isEqualToString:@"canonicalIdentifier"]) {
branchUniversalObj.canonicalIdentifier = [properties valueForKey:key];
}
else if ([key isEqualToString:@"title"]) {
branchUniversalObj.title = [properties valueForKey:key];
}
else if ([key isEqualToString:@"contentDescription"]) {
branchUniversalObj.contentDescription = [properties valueForKey:key];
}
else if ([key isEqualToString:@"contentImageUrl"]){
NSString *imageUrl = [properties valueForKey:key];
branchUniversalObj.imageUrl = imageUrl;
}
else {
[branchUniversalObj setValue:[properties objectForKey:key] forKey:key];
}
}
// [self.branchUniversalObjArray addObject:branchUniversalObj];
// Instantiate callback ids
NSMutableDictionary *branchUniversalObjDict = [NSMutableDictionary dictionaryWithDictionary:@{
@"branchUniversalObj": branchUniversalObj,
@"onShareSheetDismissed": command.callbackId,
@"onShareSheetLaunched": command.callbackId,
@"onLinkShareResponse": command.callbackId,
@"onChannelSelected": command.callbackId
}];
[self.branchUniversalObjArray addObject:branchUniversalObjDict];
NSNumber *branchUniversalObjectId = [[NSNumber alloc] initWithInteger:([self.branchUniversalObjArray count] - 1)];
NSString *message = @"createBranchUniversalObject Success";
NSDictionary *params = [[NSDictionary alloc] initWithObjectsAndKeys:message, @"message", branchUniversalObjectId, @"branchUniversalObjectId", nil];
CDVPluginResult* pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsDictionary:params];
[self.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId];
}
- (void)registerView:(CDVInvokedUrlCommand*)command
{
int branchUniversalObjectId = [[command.arguments objectAtIndex:0] intValue];
NSMutableDictionary *branchUniversalObjDict = [self.branchUniversalObjArray objectAtIndex:branchUniversalObjectId];
BranchUniversalObject *branchUniversalObj = [branchUniversalObjDict objectForKey:@"branchUniversalObj"];
[branchUniversalObj registerViewWithCallback:^(NSDictionary *params, NSError *error) {
CDVPluginResult *pluginResult = nil;
if (!error) {
pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsDictionary:params];
} else {
pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_ERROR messageAsString:[error localizedDescription]];
}
[self.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId];
}];
}
- (void)generateShortUrl:(CDVInvokedUrlCommand*)command
{
int branchUniversalObjectId = [[command.arguments objectAtIndex:0] intValue];
NSDictionary *arg1 = [command.arguments objectAtIndex:1];
NSDictionary *arg2 = [command.arguments objectAtIndex:2];
NSMutableDictionary *branchUniversalObjDict = [self.branchUniversalObjArray objectAtIndex:branchUniversalObjectId];
BranchUniversalObject *branchUniversalObj = [branchUniversalObjDict objectForKey:@"branchUniversalObj"];
BranchLinkProperties *props = [[BranchLinkProperties alloc] init];
for (id key in arg1) {
if ([key isEqualToString:@"duration"]) {
props.matchDuration = (NSUInteger)[((NSNumber *)[arg1 objectForKey:key]) integerValue];
}
else if ([key isEqualToString:@"feature"]) {
props.feature = [arg1 objectForKey:key];
}
else if ([key isEqualToString:@"stage"]) {
props.stage = [arg1 objectForKey:key];
}
else if ([key isEqualToString:@"campaign"]) {
props.campaign = [arg1 objectForKey:key];
}
else if ([key isEqualToString:@"alias"]) {
props.alias = [arg1 objectForKey:key];
}
else if ([key isEqualToString:@"channel"]) {
props.channel = [arg1 objectForKey:key];
}
else if ([key isEqualToString:@"tags"] && [[arg1 objectForKey:key] isKindOfClass:[NSArray class]]) {
props.tags = [arg1 objectForKey:key];
}
}
if (arg2) {
for (id key in arg2) {
[props addControlParam:key withValue:[arg2 objectForKey:key]];
}
}
[branchUniversalObj getShortUrlWithLinkProperties:props andCallback:^(NSString *url, NSError *error) {
CDVPluginResult* pluginResult = nil;
if (url) {
NSError *err;
NSDictionary *jsonObj = [[NSDictionary alloc] initWithObjectsAndKeys:url, @"url", 0, @"options", &err, @"error", nil];
if (!jsonObj) {
NSLog(@"Parsing Error: %@", [err localizedDescription]);
pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_ERROR messageAsString:[err localizedDescription]];
} else {
NSLog(@"Success");
pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsDictionary:jsonObj];
}
}
else {
pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_ERROR messageAsString:[error localizedDescription]];
}
[self.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId];
}];
}
- (void)showShareSheet:(CDVInvokedUrlCommand*)command
{
NSString *shareText = @"Share Link";
if ([command.arguments count] >= 4) {
shareText = [command.arguments objectAtIndex:3];
}
int branchUniversalObjectId = [[command.arguments objectAtIndex:0] intValue];
NSDictionary *arg1 = [command.arguments objectAtIndex:1];
NSDictionary *arg2 = [command.arguments objectAtIndex:2];
NSMutableDictionary *branchUniversalObjDict = [self.branchUniversalObjArray objectAtIndex:branchUniversalObjectId];
BranchUniversalObject *branchUniversalObj = [branchUniversalObjDict objectForKey:@"branchUniversalObj"];
BranchLinkProperties *linkProperties = [[BranchLinkProperties alloc] init];
for (id key in arg1) {
if ([key isEqualToString:@"duration"]) {
linkProperties.matchDuration = (NSUInteger)[((NSNumber *)[arg1 objectForKey:key]) integerValue];
}
else {
[linkProperties setValue:[arg1 objectForKey:key] forKey:key];
}
}
if (arg2) {
for (id key in arg2) {
[linkProperties addControlParam:key withValue:[arg2 objectForKey:key]];
}
}
[branchUniversalObj showShareSheetWithLinkProperties:linkProperties andShareText:shareText fromViewController:self.viewController completionWithError:^(NSString * _Nullable activityType, BOOL completed, NSError * _Nullable error) {
int listenerCallbackId = [[command.arguments objectAtIndex:0] intValue];
if (completed) {
NSLog(@"Share link complete");
[branchUniversalObj getShortUrlWithLinkProperties:linkProperties andCallback:^(NSString *url, NSError *error) {
if (!error) {
NSDictionary *response = [[NSDictionary alloc] initWithObjectsAndKeys:url, @"sharedLink", activityType, @"sharedChannel", nil];
[self doShareLinkResponse:listenerCallbackId sendResponse:response];
}
}];
}
CDVPluginResult *shareDialogDismissed = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK];
NSMutableDictionary *branchUniversalObjDict = [self.branchUniversalObjArray objectAtIndex:listenerCallbackId];
[shareDialogDismissed setKeepCallbackAsBool:TRUE];
[self.commandDelegate sendPluginResult:shareDialogDismissed callbackId:[branchUniversalObjDict objectForKey:@"onShareSheetDismissed"]];
}];
}
- (void)doShareLinkResponse:(int)callbackId sendResponse:(NSDictionary*)response {
CDVPluginResult *linkShareResponse = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsDictionary:response];
NSMutableDictionary *branchUniversalObjDict = [self.branchUniversalObjArray objectAtIndex:callbackId];
[linkShareResponse setKeepCallbackAsBool:TRUE];
[self.commandDelegate sendPluginResult:linkShareResponse callbackId:[branchUniversalObjDict objectForKey:@"onLinkShareResponse"]];
}
- (void)onShareLinkDialogDismissed:(CDVInvokedUrlCommand*)command
{
int listenerCallbackId = [[command.arguments objectAtIndex:0] intValue];
NSMutableDictionary *newBranchUniversalObjDict = [self.branchUniversalObjArray objectAtIndex:listenerCallbackId];
[newBranchUniversalObjDict setObject:command.callbackId forKey:@"onShareSheetDismissed"];
[self.branchUniversalObjArray replaceObjectAtIndex:listenerCallbackId withObject:newBranchUniversalObjDict];
}
- (void)onLinkShareResponse:(CDVInvokedUrlCommand*)command
{
int listenerCallbackId = [[command.arguments objectAtIndex:0] intValue];
NSMutableDictionary *newBranchUniversalObjDict = [self.branchUniversalObjArray objectAtIndex:listenerCallbackId];
[newBranchUniversalObjDict setObject:command.callbackId forKey:@"onLinkShareResponse"];
[self.branchUniversalObjArray replaceObjectAtIndex:listenerCallbackId withObject:newBranchUniversalObjDict];
}
- (void)listOnSpotlight:(CDVInvokedUrlCommand*)command {
int branchUniversalObjectId = [[command.arguments objectAtIndex:0] intValue];
NSMutableDictionary *branchUniversalObjDict = [self.branchUniversalObjArray objectAtIndex:branchUniversalObjectId];
BranchUniversalObject *branchUniversalObj = [branchUniversalObjDict objectForKey:@"branchUniversalObj"];
[branchUniversalObj listOnSpotlightWithCallback:^(NSString *string, NSError *error) {
CDVPluginResult* pluginResult = nil;
if (!error) {
NSError *err;
NSData *jsonData = [NSJSONSerialization dataWithJSONObject:@{@"result":string} options:0 error:&err];
if (!jsonData) {
NSLog(@"Parsing Error: %@", [err localizedDescription]);
pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_ERROR messageAsString:[err localizedDescription]];
} else {
NSString *jsonString = [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];
pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsString:jsonString];
}
}
else {
pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_ERROR messageAsString:[error localizedDescription]];
}
[self.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId];
}];
}
#pragma mark Branch Query Methods
- (void)lastAttributedTouchData:(CDVInvokedUrlCommand *)command {
NSMutableDictionary *json = [NSMutableDictionary new];
Branch *branch = [self getInstance];
[branch lastAttributedTouchDataWithAttributionWindow:30 completion:^(BranchLastAttributedTouchData * _Nullable latd, NSError * _Nullable error) {
CDVPluginResult* pluginResult = nil;
if (latd) {
[json setObject:latd.attributionWindow forKey:@"attribution_window"];
[json setObject:latd.lastAttributedTouchJSON forKey:@"last_attributed_touch_data"];
pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsDictionary:json];
} else {
pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_ERROR messageAsString:@"No LATD available"];
}
[self.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId];
}];
}
- (void)getBranchQRCode:(CDVInvokedUrlCommand*)command
{
int branchUniversalObjectId = [[command.arguments objectAtIndex:1] intValue];
NSMutableDictionary *branchUniversalObjDict = [self.branchUniversalObjArray objectAtIndex:branchUniversalObjectId];
BranchUniversalObject *branchUniversalObj = [branchUniversalObjDict objectForKey:@"branchUniversalObj"];
BranchLinkProperties *linkProperties = [BranchLinkProperties new];
NSDictionary *arg1 = [command.arguments objectAtIndex:2];
NSDictionary *arg2 = [command.arguments objectAtIndex:3];
for (id key in arg1) {
if ([key isEqualToString:@"duration"]) {
linkProperties.matchDuration = (NSUInteger)[((NSNumber *)[arg1 objectForKey:key]) integerValue];
}
else if ([key isEqualToString:@"feature"]) {
linkProperties.feature = [arg1 objectForKey:key];
}
else if ([key isEqualToString:@"stage"]) {
linkProperties.stage = [arg1 objectForKey:key];
}
else if ([key isEqualToString:@"campaign"]) {
linkProperties.campaign = [arg1 objectForKey:key];
}
else if ([key isEqualToString:@"alias"]) {
linkProperties.alias = [arg1 objectForKey:key];
}
else if ([key isEqualToString:@"channel"]) {
linkProperties.channel = [arg1 objectForKey:key];
}
else if ([key isEqualToString:@"tags"] && [[arg1 objectForKey:key] isKindOfClass:[NSArray class]]) {
linkProperties.tags = [arg1 objectForKey:key];
}
}
if (arg2) {
for (id key in arg2) {
[linkProperties addControlParam:key withValue:[arg2 objectForKey:key]];
}
}
NSMutableDictionary *qrCodeSettingsMap = [command.arguments objectAtIndex:0];
BranchQRCode *qrCode = [BranchQRCode new];
if (qrCodeSettingsMap[@"codeColor"]) {
qrCode.codeColor = [self colorWithHexString:qrCodeSettingsMap[@"codeColor"]];
}
if (qrCodeSettingsMap[@"backgroundColor"]) {
qrCode.backgroundColor = [self colorWithHexString:qrCodeSettingsMap[@"backgroundColor"]];
}
if (qrCodeSettingsMap[@"centerLogo"]) {
qrCode.centerLogo = qrCodeSettingsMap[@"centerLogo"];
}
if (qrCodeSettingsMap[@"width"]) {
qrCode.width = qrCodeSettingsMap[@"width"];
}
if (qrCodeSettingsMap[@"margin"]) {
qrCode.margin = qrCodeSettingsMap[@"margin"];
}
if (qrCodeSettingsMap[@"imageFormat"]) {
if ([qrCodeSettingsMap[@"imageFormat"] isEqual:@"JPEG"]) {
qrCode.imageFormat = BranchQRCodeImageFormatJPEG;
} else {
qrCode.imageFormat = BranchQRCodeImageFormatPNG;
}
}
[qrCode getQRCodeAsData:branchUniversalObj linkProperties:linkProperties completion:^(NSData * _Nonnull qrCodeData, NSError * _Nonnull error) {
CDVPluginResult* pluginResult = nil;
if (!error) {
NSString* imageString = [qrCodeData base64EncodedStringWithOptions:nil];
pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsString:imageString];
} else {
pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_ERROR messageAsString:[error localizedDescription]];
}
[self.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId];
}];
}
- (UIColor *) colorWithHexString: (NSString *) hexString {
NSString *colorString = [[hexString stringByReplacingOccurrencesOfString: @"#" withString: @""] uppercaseString];
CGFloat alpha, red, blue, green;
switch ([colorString length]) {
case 3: // #RGB
alpha = 1.0f;
red = [self colorComponentFrom: colorString start: 0 length: 1];
green = [self colorComponentFrom: colorString start: 1 length: 1];
blue = [self colorComponentFrom: colorString start: 2 length: 1];
break;
case 4: // #ARGB
alpha = [self colorComponentFrom: colorString start: 0 length: 1];
red = [self colorComponentFrom: colorString start: 1 length: 1];
green = [self colorComponentFrom: colorString start: 2 length: 1];
blue = [self colorComponentFrom: colorString start: 3 length: 1];
break;
case 6: // #RRGGBB
alpha = 1.0f;
red = [self colorComponentFrom: colorString start: 0 length: 2];
green = [self colorComponentFrom: colorString start: 2 length: 2];
blue = [self colorComponentFrom: colorString start: 4 length: 2];
break;
case 8: // #AARRGGBB
alpha = [self colorComponentFrom: colorString start: 0 length: 2];
red = [self colorComponentFrom: colorString start: 2 length: 2];
green = [self colorComponentFrom: colorString start: 4 length: 2];
blue = [self colorComponentFrom: colorString start: 6 length: 2];
break;
default:
NSLog(@"Error: Invalid color value. It should be a hex value of the form #RBG, #ARGB, #RRGGBB, or #AARRGGBB");
break;
}
return [UIColor colorWithRed: red green: green blue: blue alpha: alpha];
}
- (CGFloat) colorComponentFrom: (NSString *) string start: (NSUInteger) start length: (NSUInteger) length {
NSString *substring = [string substringWithRange: NSMakeRange(start, length)];
NSString *fullHex = length == 2 ? substring : [NSString stringWithFormat: @"%@%@", substring, substring];
unsigned hexComponent;
[[NSScanner scannerWithString: fullHex] scanHexInt: &hexComponent];
return hexComponent / 255.0;
}
#pragma mark - URL Methods (not fully implemented YET!)
- (NSString *)getShortURL:(CDVInvokedUrlCommand*)command
{
Branch *branch = [self getInstance];
return [branch getShortURL];
}
- (id)getShortURLWithParams:(CDVInvokedUrlCommand*)command
{
Branch *branch = [self getInstance];
NSDictionary *params = [command.arguments objectAtIndex:0];
return [branch getShortURLWithParams:params];
}
- (NSString *)getLongURLWithParams:(CDVInvokedUrlCommand*)command
{
id params = [command.arguments objectAtIndex:0];
return [[self getInstance] getLongURLWithParams:params];
}
- (void)getBranchActivityItemWithParams:(CDVInvokedUrlCommand*)command
{
UIActivityItemProvider *provider = [Branch getBranchActivityItemWithParams:[command.arguments objectAtIndex:0]];
UIActivityViewController *shareViewController = [[UIActivityViewController alloc] initWithActivityItems:@[ provider ] applicationActivities:nil];
[self.viewController presentViewController:shareViewController animated:YES completion:nil];
}
@end