This repository was archived by the owner on Aug 17, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 224
Expand file tree
/
Copy pathIonicDeeplinkPlugin.m
More file actions
142 lines (104 loc) · 4.06 KB
/
IonicDeeplinkPlugin.m
File metadata and controls
142 lines (104 loc) · 4.06 KB
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
#import "IonicDeeplinkPlugin.h"
#import <Cordova/CDVAvailability.h>
#import "DeeplinkService.h"
@implementation IonicDeeplinkPlugin
- (void)pluginInitialize {
_handlers = [[NSMutableArray alloc] init];
NSURL* lastURL = [DeeplinkService getLastURL];
if (lastURL != nil)
{
[self handleLink:lastURL];
}
[DeeplinkService clearLastURL];
NSUserActivity* lastUserActivity = [DeeplinkService getLastUserActivity];
if (lastUserActivity != nil)
{
[self handleContinueUserActivity:lastUserActivity];
}
[DeeplinkService clearLastUserActivity];
}
/* ------------------------------------------------------------- */
- (void)onAppTerminate {
_handlers = nil;
[super onAppTerminate];
}
- (void)canOpenApp:(CDVInvokedUrlCommand *)command {
CDVPluginResult* result = nil;
NSString* scheme = [command.arguments objectAtIndex:0];
if ([[UIApplication sharedApplication] canOpenURL:[NSURL URLWithString:scheme]]) {
result = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsBool:(true)];
} else {
result = [CDVPluginResult resultWithStatus:CDVCommandStatus_ERROR messageAsBool:(false)];
}
[self.commandDelegate sendPluginResult:result callbackId:command.callbackId];
}
- (void)onDeepLink:(CDVInvokedUrlCommand *)command {
[_handlers addObject:command.callbackId];
// Try to consume any events we got before we were listening
[self sendToJs];
}
- (BOOL)handleLink:(NSURL *)url {
NSLog(@"IonicDeepLinkPlugin: Handle link (internal) %@", url);
_lastEvent = [self createResult:url];
[self sendToJs];
return YES;
}
- (BOOL)handleContinueUserActivity:(NSUserActivity *)userActivity {
if (![userActivity.activityType isEqualToString:NSUserActivityTypeBrowsingWeb] || userActivity.webpageURL == nil) {
return NO;
}
NSURL *url = userActivity.webpageURL;
_lastEvent = [self createResult:url];
NSLog(@"IonicDeepLinkPlugin: Handle continueUserActivity (internal) %@", url);
[self sendToJs];
return NO;
}
- (void) sendToJs {
// Send the last event to JS if we have one
if (_handlers.count == 0 || _lastEvent == nil) {
return;
}
// Iterate our handlers and send the event
for (id callbackID in _handlers) {
[self.commandDelegate sendPluginResult:_lastEvent callbackId:callbackID];
}
// Clear out the last event
_lastEvent = nil;
}
- (CDVPluginResult *)createResult:(NSURL *)url {
NSDictionary* data = @{
@"url": [url absoluteString] ?: @"",
@"path": [url path] ?: @"",
@"queryString": [url query] ?: @"",
@"scheme": [url scheme] ?: @"",
@"host": [url host] ?: @"",
@"fragment": [url fragment] ?: @""
};
CDVPluginResult *result = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsDictionary:data];
[result setKeepCallbackAsBool:YES];
return result;
}
- (void)getHardwareInfo:(CDVInvokedUrlCommand *)command {
NSMutableDictionary *info = [[NSMutableDictionary alloc] init];
// Get the as id in a way that doesn't require it be linked
Class asIdManClass = NSClassFromString(@"ASIdentifierManager");
if(asIdManClass) {
SEL sharedManagerSel = NSSelectorFromString(@"sharedManager");
id sharedManager = ((id (*)(id, SEL))[asIdManClass methodForSelector:sharedManagerSel])(asIdManClass, sharedManagerSel);
SEL advertisingIdentifierSelector = NSSelectorFromString(@"advertisingIdentifier");
NSUUID *uuid = ((NSUUID* (*)(id, SEL))[sharedManager methodForSelector:advertisingIdentifierSelector])(sharedManager, advertisingIdentifierSelector);
NSString *adId = [uuid UUIDString];
// Check if ad tracking is disabled (happens on iOS 10+)
NSString *disabledString = @"00000000-0000-0000-0000-000000000000";
if (![adId isEqualToString:disabledString]) {
[info setObject:adId forKey:@"adid"];
}
}
NSString *uuid = [[UIDevice currentDevice].identifierForVendor UUIDString];
if(uuid && [uuid length] > 0) {
[info setObject:uuid forKey:@"uuid"];
}
CDVPluginResult *result = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsDictionary:info];
[self.commandDelegate sendPluginResult:result callbackId:command.callbackId];
}
@end