Search code examples
uiwebviewreact-native

How to open links in Safari from React Native's WebView component


In the app, I'm using React Native's WebView component. If the user clicks on any links explicitly I would like it to open in Safari, instead of the WebView component being directed to that URL.

 <WebView
    source={{uri: 'https://some-url-here'}}
    style={{marginTop: 20}}
  />

Solution

  • For IOS, you can use onShouldStartLoadWithRequest prop. The prop is called when webview is opening a new page. You can return false to stop loading and use React native Linking api to open url in safari.

    Your onShouldStartLoadWithRequest could look like following:

    onShouldStartLoadWithRequest={(navState)=>{
          Linking.openURL(navState.url)
          return false;
        }}