Kar*_*ava 3 ios react-native react-native-ios react-native-webview
我正在为我的应用程序实施应用程序WebView。我必须打开一些信息页面,并且必须根据 web 视图中任何特定位置(包含不同类型的数据)的点击来获取一些数据。但在 iOS 中,在加载任何 URL 时onShouldStartLoadWithRequest会自动调用,这会导致在 HTML 内容中打开不同的 URL。但它在 Android 中按预期工作。
<WebView
originWhitelist={["*"]}
style={style}
source={source}
showsVerticalScrollIndicator={showsVerticalScrollIndicator}
startInLoadingState={startInLoadingState}
javaScriptEnabled={true}
onShouldStartLoadWithRequest={request => {
return onLoadWebViewOnClick(request)
}}
/>
Run Code Online (Sandbox Code Playgroud)
在这个handleUrlNavigation中会处理任何点击动作的请求,但是它每次都会自动调用。我该如何处理这个问题?
Tay*_*ach 10
我就是这样解决的。它需要平台检查,因为 iOS 在加载时调用该函数,但 android 不会。
在 ios 和 android 上,我的 webview 都会加载我的 HTML,当用户单击 webview 内的链接时,它们会被重定向到本机浏览器。
onShouldStartLoadWithRequest={event => {
const isExternalLink =
Platform.OS === 'ios' ? event.navigationType === 'click' : true;
if (event.url.slice(0, 4) === 'http' && isExternalLink) {
Linking.canOpenURL(event.url).then(supported => {
if (supported) {
Linking.openURL(event.url);
}
});
return false;
}
return true;
}}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
10228 次 |
| 最近记录: |