152

I am new in react-native and i want to open url in default browser like Chrome in Android and iPhone both.

We open url via intent in Android same like functionality i want to achieve.

I have search many times but it will give me the result of Deepklinking.

4 Answers 4

305

You should use Linking.

Example from the docs:

class OpenURLButton extends React.Component {
  static propTypes = { url: React.PropTypes.string };
  handleClick = () => {
    Linking.canOpenURL(this.props.url).then(supported => {
      if (supported) {
        Linking.openURL(this.props.url);
      } else {
        console.log("Don't know how to open URI: " + this.props.url);
      }
    });
  };
  render() {
    return (
      <TouchableOpacity onPress={this.handleClick}>
        {" "}
        <View style={styles.button}>
          {" "}<Text style={styles.text}>Open {this.props.url}</Text>{" "}
        </View>
        {" "}
      </TouchableOpacity>
    );
  }
}

Here's an example you can try on Expo Snack:

import React, { Component } from 'react';
import { View, StyleSheet, Button, Linking } from 'react-native';
import { Constants } from 'expo';

export default class App extends Component {
  render() {
    return (
      <View style={styles.container}>
       <Button title="Click me" onPress={ ()=>{ Linking.openURL('https://google.com')}} />
      </View>
    );
  }
}

const styles = StyleSheet.create({
  container: {
    flex: 1,
    alignItems: 'center',
    justifyContent: 'center',
    paddingTop: Constants.statusBarHeight,
    backgroundColor: '#ecf0f1',
  },
});
Sign up to request clarification or add additional context in comments.

5 Comments

@RajKumarN, how would this work if you have an app that grabs news articles and you want to open the news articles' hyperlinks in a separate browser?
Check this out, facebook.github.io/react-native/docs/…. This can be helpful @Daniel
@RajKumarN How can we open the external URL inside the app not redirecting to the browser?
The link at the top of this answer no longer works. Gotta love the irony :)
@CalebKoch Dropping the .html at the end of the URL makes the link work again. Updated my answer, thanks for the heads up.
23

A simpler way which eliminates checking if the app can open the url.

  loadInBrowser = () => {
    Linking.openURL(this.state.url).catch(err => console.error("Couldn't load page", err));
  };

Calling it with a button.

<Button title="Open in Browser" onPress={this.loadInBrowser} />

4 Comments

How do I handle null values? let's say I'm receiving URL from backend but it returned null instead of a string, the app is crashing.
@ASN You do that check prior to passing that url to the openURL method. For example: If (this.state.url) Linking.openURL(this.state.url). You can also put the catch clause to use if you don't want to check prior.
Hi! Just a question, What do you suggest to do for android if app is selected to open by default and I need to open the same link but in browser?
simple, short, useful solution, can you comment about it shows blank screen when I comeback manually from browser to app.
12

Try this:

import React, { useCallback } from "react";
import { Linking } from "react-native";
OpenWEB = () => {
  Linking.openURL(url);
};

const App = () => {
  return <View onPress={() => OpenWeb}>OPEN YOUR WEB</View>;
};

Hope this will solve your problem.

Comments

12

In React 16.8+, the following can be used to create an ExternalLinkBtn component, similar to an a tag on the web. The link will be opened externally in the default browser.

import React from 'react';
import { Button, Linking } from 'react-native';

const ExternalLinkBtn = (props) => {
  return <Button
            title={props.title}
            onPress={() => {
                Linking.openURL(props.url)
                .catch(err => {
                    console.error("Failed opening page because: ", err)
                    alert('Failed to open page')
                })}}
          />
}

Below is an example of using our ExternalLinkBtn in a component

export default function exampleUse() {
  return (
    <View>
      <ExternalLinkBtn title="Example Link" url="https://example.com" />
    </View>
  )
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.