react-native - React Native - 在浏览器中打开链接

标签 react-native

嗨,我正在使用 React Native 的 webview 来显示一些 html, 我希望每当用户单击该 html 中的链接时,它都会使用该链接打开用户的浏览器。

这可能吗?

编辑1:

我最终使用了这个包:npmjs.com/package/react-native-communications,它会打开浏览器。 当 URL 更改时,我调用浏览器在 onNavigationStateChange 上打开。

现在的问题是,尽管我已经转移到浏览器,WebView仍然继续处理请求,我怎样才能停止请求?

最佳答案

这是一个完整的工作解决方案:

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

export default class WebViewThatOpensLinksInNavigator extends Component {
  render() {
    const uri = 'http://stackoverflow.com/questions/35531679/react-native-open-links-in-browser';
    return (
      <WebView
        ref={(ref) => { this.webview = ref; }}
        source={{ uri }}
        onNavigationStateChange={(event) => {
          if (event.url !== uri) {
            this.webview.stopLoading();
            Linking.openURL(event.url);
          }
        }}
      />
    );
  }
}

它使用一个简单的 WebView,拦截任何 url 更改,如果该 url 与原始 URL 不同,则停止加载,防止页面更改,并在 OS Navigator 中打开它。

ios simulator

关于react-native - React Native - 在浏览器中打开链接,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35531679/

相关文章:

javascript - 对象作为 React Child 无效(找到 : object with keys{id, name})

react-native - 如何在 aws-amplify/aws-cognito 中验证/更新属性时重新发送验证码?

javascript - React Native app,安装release apk,解析错误

react-native - React Native Animated.ScrollView 不允许我以编程方式滚动到某个位置

reactjs - react native "The expo SDK requires Expo to run. .... this code is not running on Expo."

react-native - 在 AsyncStorage 中 setItem 的正确方法

android - React native 中的 CSV 预览

javascript - 当父状态改变时如何重新渲染子组件?

javascript - 从列表数组中追加一个值 - React Native

reactjs - 如何使用 PanResponder 在 React Native 中缩放图像?