2016-01-26 22:34:00 +00:00
|
|
|
/**
|
|
|
|
* Copyright (c) 2015-present, Facebook, Inc.
|
|
|
|
*
|
2018-02-17 02:24:55 +00:00
|
|
|
* This source code is licensed under the MIT license found in the
|
|
|
|
* LICENSE file in the root directory of this source tree.
|
2016-01-26 22:34:00 +00:00
|
|
|
*
|
2018-05-11 02:06:46 +00:00
|
|
|
* @format
|
2016-01-26 22:34:00 +00:00
|
|
|
* @flow
|
|
|
|
*/
|
2018-05-11 02:06:46 +00:00
|
|
|
|
2016-01-26 22:34:00 +00:00
|
|
|
'use strict';
|
|
|
|
|
2016-05-27 17:14:12 +00:00
|
|
|
const NativeEventEmitter = require('NativeEventEmitter');
|
|
|
|
const NativeModules = require('NativeModules');
|
2016-01-26 22:34:00 +00:00
|
|
|
const Platform = require('Platform');
|
|
|
|
|
2016-05-27 17:14:12 +00:00
|
|
|
const invariant = require('fbjs/lib/invariant');
|
2016-01-26 22:34:00 +00:00
|
|
|
|
2018-05-11 02:06:46 +00:00
|
|
|
const LinkingManager =
|
|
|
|
Platform.OS === 'android'
|
|
|
|
? NativeModules.IntentAndroid
|
|
|
|
: NativeModules.LinkingManager;
|
2016-01-26 22:34:00 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* `Linking` gives you a general interface to interact with both incoming
|
|
|
|
* and outgoing app links.
|
2018-02-22 15:04:35 +00:00
|
|
|
*
|
2018-01-30 00:10:49 +00:00
|
|
|
* See https://facebook.github.io/react-native/docs/linking.html
|
2016-01-26 22:34:00 +00:00
|
|
|
*/
|
2016-05-27 17:14:12 +00:00
|
|
|
class Linking extends NativeEventEmitter {
|
|
|
|
constructor() {
|
2016-08-09 13:32:41 +00:00
|
|
|
super(LinkingManager);
|
2016-05-27 17:14:12 +00:00
|
|
|
}
|
2016-08-09 13:32:41 +00:00
|
|
|
|
2016-01-26 22:34:00 +00:00
|
|
|
/**
|
|
|
|
* Add a handler to Linking changes by listening to the `url` event type
|
|
|
|
* and providing the handler
|
2018-02-22 15:04:35 +00:00
|
|
|
*
|
2018-01-30 00:10:49 +00:00
|
|
|
* See https://facebook.github.io/react-native/docs/linking.html#addeventlistener
|
2016-01-26 22:34:00 +00:00
|
|
|
*/
|
2016-05-27 17:14:12 +00:00
|
|
|
addEventListener(type: string, handler: Function) {
|
2016-06-06 01:46:32 +00:00
|
|
|
this.addListener(type, handler);
|
2016-01-26 22:34:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2018-01-30 00:10:49 +00:00
|
|
|
* Remove a handler by passing the `url` event type and the handler.
|
2018-02-22 15:04:35 +00:00
|
|
|
*
|
2018-01-30 00:10:49 +00:00
|
|
|
* See https://facebook.github.io/react-native/docs/linking.html#removeeventlistener
|
2016-01-26 22:34:00 +00:00
|
|
|
*/
|
2018-05-11 02:06:46 +00:00
|
|
|
removeEventListener(type: string, handler: Function) {
|
2016-06-06 01:46:32 +00:00
|
|
|
this.removeListener(type, handler);
|
2016-01-26 22:34:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Try to open the given `url` with any of the installed apps.
|
|
|
|
*
|
2018-01-30 00:10:49 +00:00
|
|
|
* See https://facebook.github.io/react-native/docs/linking.html#openurl
|
2016-01-26 22:34:00 +00:00
|
|
|
*/
|
2016-06-03 09:37:43 +00:00
|
|
|
openURL(url: string): Promise<any> {
|
2016-01-26 22:34:00 +00:00
|
|
|
this._validateURL(url);
|
|
|
|
return LinkingManager.openURL(url);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Determine whether or not an installed app can handle a given URL.
|
|
|
|
*
|
2018-01-30 00:10:49 +00:00
|
|
|
* See https://facebook.github.io/react-native/docs/linking.html#canopenurl
|
2016-01-26 22:34:00 +00:00
|
|
|
*/
|
2016-05-27 17:14:12 +00:00
|
|
|
canOpenURL(url: string): Promise<boolean> {
|
2016-01-26 22:34:00 +00:00
|
|
|
this._validateURL(url);
|
|
|
|
return LinkingManager.canOpenURL(url);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2016-05-27 17:14:12 +00:00
|
|
|
* If the app launch was triggered by an app link,
|
2016-01-26 22:34:00 +00:00
|
|
|
* it will give the link url, otherwise it will give `null`
|
|
|
|
*
|
2018-01-30 00:10:49 +00:00
|
|
|
* See https://facebook.github.io/react-native/docs/linking.html#getinitialurl
|
2016-01-26 22:34:00 +00:00
|
|
|
*/
|
2016-05-27 17:14:12 +00:00
|
|
|
getInitialURL(): Promise<?string> {
|
|
|
|
return LinkingManager.getInitialURL();
|
2016-01-26 22:34:00 +00:00
|
|
|
}
|
|
|
|
|
2016-05-27 17:14:12 +00:00
|
|
|
_validateURL(url: string) {
|
2016-01-26 22:34:00 +00:00
|
|
|
invariant(
|
|
|
|
typeof url === 'string',
|
2018-05-11 02:06:46 +00:00
|
|
|
'Invalid URL: should be a string. Was: ' + url,
|
2016-01-26 22:34:00 +00:00
|
|
|
);
|
2018-05-11 02:06:46 +00:00
|
|
|
invariant(url, 'Invalid URL: cannot be empty');
|
2016-01-26 22:34:00 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-05-27 17:14:12 +00:00
|
|
|
module.exports = new Linking();
|