mirror of
https://github.com/expo/react-native-url-handler.git
synced 2026-11-15 17:40:02 -10:00
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.
96 lines
2.3 KiB
JavaScript
96 lines
2.3 KiB
JavaScript
/*
|
|
* Copyright 2015-present 650 Industries. All rights reserved.
|
|
*
|
|
* @flow
|
|
*/
|
|
'use strict';
|
|
|
|
var EventEmitter = require('EventEmitter');
|
|
var React = require('react-native');
|
|
var {
|
|
DeviceEventEmitter,
|
|
NativeModules: {
|
|
NTURLHandler,
|
|
},
|
|
} = React;
|
|
|
|
var url = require('url');
|
|
|
|
var emitter = new EventEmitter();
|
|
|
|
var URLHandler = {
|
|
/**
|
|
* The list of URL protocols handled by this app.
|
|
*/
|
|
schemes: NTURLHandler.schemes,
|
|
|
|
/**
|
|
* The URL that opens the settings for this app. It is defined on iOS 8 and
|
|
* up.
|
|
*/
|
|
settingsURL: NTURLHandler.settingsURL,
|
|
|
|
/**
|
|
* The URL that launched this app if it was launched from a URL.
|
|
*/
|
|
initialURL: NTURLHandler.initialURL,
|
|
|
|
/**
|
|
* Referrer information about the URL that launched this app if it was
|
|
* launched from a URL.
|
|
*/
|
|
initialReferrer: NTURLHandler.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('request', targetURL);
|
|
} else {
|
|
NTURLHandler.openURL(targetURL, () => {}, (error) => {
|
|
console.error('Error opening URL: ' + error.stack);
|
|
});
|
|
}
|
|
},
|
|
|
|
/**
|
|
* 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.
|
|
*
|
|
* This method returns a subscription to later remove the listener.
|
|
*/
|
|
addListener(listener: (url: string, referrer: ?Referrer) => void) {
|
|
return emitter.addListener('request', listener);
|
|
},
|
|
};
|
|
|
|
DeviceEventEmitter.addListener('NTURLHandler.openURL', (event) => {
|
|
var {url, sourceApplication, annotation} = event;
|
|
if (sourceApplication != null) {
|
|
var referrer = {sourceApplication, annotation};
|
|
}
|
|
emitter.emit('request', url, referrer);
|
|
});
|
|
|
|
type Referrer = {
|
|
sourceApplication: string;
|
|
annotation?: any;
|
|
};
|
|
|
|
module.exports = URLHandler;
|