-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathUrlHandler.js
117 lines (102 loc) · 3.04 KB
/
UrlHandler.js
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
/**
* @flow
*/
'use strict';
let EventEmitter = require('eventemitter3');
let React = require('react-native');
let {
DeviceEventEmitter,
NativeModules: {
EXURLHandler,
},
} = React;
let url = require('url');
let emitter = new EventEmitter();
let UrlHandler = {
/**
* The list of URL protocols handled by this app.
*/
schemes: EXURLHandler.schemes,
/**
* The URL that opens the settings for this app. It is defined on iOS 8 and
* up.
*/
settingsUrl: EXURLHandler.settingsURL,
/**
* The URL that launched this app if it was launched from a URL.
*/
initialUrl: EXURLHandler.initialURL,
/**
* Referrer information about the URL that launched this app if it was
* launched from a URL.
*/
initialReferrer: EXURLHandler.initialReferrer,
/**
* Opens the given URL. The URL may be an external URL or an in-app URL. URLs
* without a host are treated as in-app URLs.
*/
openUrl(targetUrl: string) {
if (this.isInternalUrl(targetUrl)) {
emitter.emit('url', { url: targetUrl });
} else {
EXURLHandler.openURLAsync(targetUrl).catch(error => {
console.error('Error opening URL: ' + error.message);
});
}
},
/**
* Returns whether the OS can open the given URL. This method returns false if
* no app on the device can open the provided URL.
*/
canOpenUrlAsync(targetUrl: string) {
if (this.isInternalUrl(targetUrl)) {
return Promise.resolve(true);
}
return EXURLHandler.canOpenURLAsync(targetUrl);
},
/**
* Returns whether the given URL is an in-app URL or an external URL.
*/
isInternalUrl(targetUrl: string): bool {
// Parse the query string and have "//" denote the hostname
let { protocol } = url.parse(targetUrl, false, true);
if (!protocol) {
return true;
}
return false; // TODO: Come up with a better way to handle this.
// We want a message passing channel between different instances of the JavaScript
// The problem here is that an event is fired within the inner Frame, but the browser
// can't repond to events fired within the frame in JavaScript
// let scheme = protocol.substring(0, protocol.length - 1);
// return this.schemes.indexOf(scheme) !== -1;
},
/**
* Adds a listener for the specified event. Supported events are:
*
* url: the app has been instructed to open a URL
* Event:
* url: string
* referrer?: Referrer
*/
addEventListener(type: string, listener: Function) {
emitter.addListener(type, listener);
},
/**
* Removes a listener for the specified event.
*/
removeEventListener(type: string, listener: Function) {
emitter.removeListener(type, listener);
},
};
DeviceEventEmitter.addListener('EXURLHandler.openURL', (event) => {
let { url, sourceApplication, annotation } = event;
if (sourceApplication != null) {
var referrer: Referrer = { sourceApplication, annotation };
}
emitter.emit('url', { url, referrer });
});
type Referrer = {
sourceApplication: string;
annotation?: any;
};
module.exports = UrlHandler;