NOTE: IntentAndroid
is being deprecated. Use Linking
instead.
IntentAndroid
gives you a general interface to handle external links.
Basic Usage #
Handling deep links #
If your app was launched from an external url registered to your app you can
access and handle it from any component you want with
componentDidMount() {
var url = IntentAndroid.getInitialURL(url => {
if (url) {
console.log('Initial url is: ' + url);
}
});
}
Example to add support for deep linking inside your React Native app.
More Info: Enabling Deep Links for App Content - Add Intent Filters for Your Deep Links.
Edit in android/app/src/main/AndroidManifest.xml
<intent-filter>
<action android:name="android.intent.action.VIEW" />
<category android:name="android.intent.category.DEFAULT" />
<category android:name="android.intent.category.BROWSABLE" />
<!-- Accepts URIs that begin with "http://www.facebook.com/react -->
<data android:scheme="http"
android:host="www.facebook.com"
android:pathPrefix="/react" />
<!-- note that the leading "/" is required for pathPrefix-->
<!-- Accepts URIs that begin with "facebook://react -->
<!-- <data android:scheme="facebook" android:host="react" /> -->
</intent-filter>
Opening external links #
To start the corresponding activity for a link (web URL, email, contact etc.), call
IntentAndroid.openURL(url)
If you want to check if any installed app can handle a given URL beforehand you can call
IntentAndroid.canOpenURL(url, (supported) => {
if (!supported) {
console.log('Can\'t handle url: ' + url);
} else {
IntentAndroid.openURL(url);
}
});
Methods #
static openURL(url: string) #
Starts a corresponding external activity for the given URL.
For example, if the URL is "https://www.facebook.com", the system browser will be opened,
or the "choose application" dialog will be shown.
You can use other URLs, like a location (e.g. "geo:37.484847,-122.148386"), a contact,
or any other URL that can be opened with {@code Intent.ACTION_VIEW}.
NOTE: This method will fail if the system doesn't know how to open the specified URL.
If you're passing in a non-http(s) URL, it's best to check {@code canOpenURL} first.
NOTE: For web URLs, the protocol ("http://", "https://") must be set accordingly!
@deprecated
static canOpenURL(url: string, callback: Function) #
Determine whether or not an installed app can handle a given URL.
You can use other URLs, like a location (e.g. "geo:37.484847,-122.148386"), a contact,
or any other URL that can be opened with {@code Intent.ACTION_VIEW}.
NOTE: For web URLs, the protocol ("http://", "https://") must be set accordingly!
@param URL the URL to open
@deprecated
static getInitialURL(callback: Function) #
'use strict';
var React = require('react-native');
var {
IntentAndroid,
StyleSheet,
Text,
TouchableNativeFeedback,
View,
} = React;
var UIExplorerBlock = require('./UIExplorerBlock');
var OpenURLButton = React.createClass({
propTypes: {
url: React.PropTypes.string,
},
handleClick: function() {
IntentAndroid.canOpenURL(this.props.url, (supported) => {
if (supported) {
IntentAndroid.openURL(this.props.url);
} else {
console.log('Don\'t know how to open URI: ' + this.props.url);
}
});
},
render: function() {
return (
<TouchableNativeFeedback
onPress={this.handleClick}>
<View style={styles.button}>
<Text style={styles.text}>Open {this.props.url}</Text>
</View>
</TouchableNativeFeedback>
);
}
});
var IntentAndroidExample = React.createClass({
statics: {
title: 'IntentAndroid',
description: 'Shows how to use Android Intents to open URLs.',
},
render: function() {
return (
<UIExplorerBlock title="Open external URLs">
<OpenURLButton url={'https://www.facebook.com'} />
<OpenURLButton url={'http://www.facebook.com'} />
<OpenURLButton url={'http://facebook.com'} />
<OpenURLButton url={'geo:37.484847,-122.148386'} />
</UIExplorerBlock>
);
},
});
var styles = StyleSheet.create({
container: {
flex: 1,
backgroundColor: 'white',
padding: 10,
paddingTop: 30,
},
button: {
padding: 10,
backgroundColor: '#3B5998',
marginBottom: 10,
},
text: {
color: 'white',
},
});
module.exports = IntentAndroidExample;