嗨,我正在使用 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 中打开它。
关于react-native - React Native - 在浏览器中打开链接,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35531679/