onShouldStartLoadWithRequest automatically calling in iOS React Native on loading of any url in WebView, How to control it?

故事扮演 提交于 2021-01-06 03:19:45

问题


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)
  }}
/>

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


回答1:


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

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


来源:https://stackoverflow.com/questions/60493866/onshouldstartloadwithrequest-automatically-calling-in-ios-react-native-on-loadin

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!