onShouldStartLoadWithRequest 在加载 WebView 中的任何 url 时自动调用 iOS React Native,如何控制它? [英] onShouldStartLoadWithRequest automatically calling in iOS React Native on loading of any url in WebView, How to control it?

查看:33
本文介绍了onShouldStartLoadWithRequest 在加载 WebView 中的任何 url 时自动调用 iOS React Native,如何控制它?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在为我的应用在 App WebView 中实现.我必须打开一些信息页面,并且我必须根据在 webview 中单击任何特定位置(包含不同类型的数据)来获取一些数据.但是在 iOS 中,在加载任何 URL 时 onShouldStartLoadWithRequest 会自动调用,这会导致在 HTML 内容中打开不同的 URL.但它在 Android 中按预期工作.

I'm implementing in App WebView for my App. I've to open some info pages and I've to get some data based on the click of any particular place(which contains a different type of data) in webview. But in iOS, while loading any URL onShouldStartLoadWithRequest calling automatically which leads opening different URLs in the HTML content. But it is working as expected in Android.

<WebView
  originWhitelist={["*"]}
  style={style}
  source={source}
  showsVerticalScrollIndicator={showsVerticalScrollIndicator}
  startInLoadingState={startInLoadingState}
  javaScriptEnabled={true}
  onShouldStartLoadWithRequest={request => {
    return onLoadWebViewOnClick(request)
  }}
/>

在这个handleUrlNavigation中会处理任何点击动作的请求,但是每次都会自动调用.我该如何处理?

in this handleUrlNavigation will handle the request of any click action, but it is automatically calling every time. How can I handle this?

推荐答案

我就是这样解决的.它需要平台检查,因为 iOS 会在加载时调用该函数,而 android 不会.

This is how I solved it. It requires a platform check since iOS calls the function on load but android does not.

在 ios 和 android 上,我的 webview 都会加载我的 HTML,当用户单击 webview 内的链接时,它们会被重定向到本机浏览器.

With this both on ios and android my webview loads with my HTML and when a user clicks a link inside the webview they are redirected to the native browser.

    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;
      }}

这篇关于onShouldStartLoadWithRequest 在加载 WebView 中的任何 url 时自动调用 iOS React Native,如何控制它?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆