mirror of
https://github.com/expo/react-native-url-handler.git
synced 2026-11-15 17:40:02 -10:00
Lowercase the supported URL schemes, and use scheme list to determine if URL is internal
If a URL has no scheme, it is considered an internal URL. If it has a scheme, we need to lop off the trailing ":" from the protocol and then compare it against the entries in the supported scheme list. Note that Cocoa does not automatically lowercase the supported schemes (we do this in native though) and Node's url library does.
This commit is contained in:
1 parent
83bfe6cbcc
commit
de7ea51fd4
2 files changed
+18
-5
No files matched your search
+15
-4
@@ -46,10 +46,8 @@ var URLHandler = {
|
||||
* without a host are treated as in-app URLs.
|
||||
*/
|
||||
openURL(targetURL: string) {
|
||||
// Parse the query string and have "//" denote the hostname
|
||||
var components = url.parse(targetURL, false, true);
|
||||
if (components.protocol === 'xxx:') {
|
||||
emitter.emit('request', url);
|
||||
if (this.isInternalURL(targetURL)) {
|
||||
emitter.emit('request', targetURL);
|
||||
} else {
|
||||
NTURLHandler.openURL(targetURL, () => {}, (error) => {
|
||||
console.error('Error opening URL: ' + error.stack);
|
||||
@@ -57,6 +55,19 @@ var URLHandler = {
|
||||
}
|
||||
},
|
||||
|
||||
/**
|
||||
* 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
|
||||
var {protocol} = url.parse(targetURL, false, true);
|
||||
if (!protocol) {
|
||||
return true;
|
||||
}
|
||||
var scheme = protocol.substring(0, protocol.length - 1);
|
||||
return this.schemes.indexOf(scheme) !== -1;
|
||||
},
|
||||
|
||||
/**
|
||||
* Adds a listener that receives an object with URL information when the app
|
||||
* has been instructed to open a URL. See the Request type.
|
||||
|
||||
@@ -74,7 +74,9 @@ static NSString * const NTURLHandlerOpenURLNotification = @"NTURLHandlerOpenURL"
|
||||
NSMutableSet *schemes = [NSMutableSet set];
|
||||
NSDictionary *info = [NSBundle mainBundle].infoDictionary;
|
||||
for (NSDictionary *urlType in info[@"CFBundleURLTypes"]) {
|
||||
[schemes addObjectsFromArray:urlType[@"CFBundleURLSchemes"]];
|
||||
for (NSString *scheme in urlType[@"CFBundleURLSchemes"]) {
|
||||
[schemes addObject:scheme.lowercaseString];
|
||||
}
|
||||
}
|
||||
return schemes;
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user