【问题标题】:react-native-webview error at first lunch第一次午餐时反应本机webview错误
【发布时间】:2021-07-15 13:20:56
【问题描述】:

我的 webview 源 (atrium://) 上有一个自定义 URL 方案 我正在处理“onNavigationStateChange”和“onShouldStartLoadWithRequest”中的 URL 一切正常,除非我启动应用程序。 如果我进入应用程序并转到 webview,它会给我错误“UNKNOW_URL_SCHEME URL :atrium://...”但是如果我返回并再次进入 webview,它就可以工作了! 我不知道在第一次加载应用程序时导致此错误的原因。 这是代码:

const handleWebViewNavigationStateChange = newNavState => {
     const { url } = newNavState;
     console.log("URL : ", url)
     if (url.startsWith('atrium://')) {
       return false
     }
  }




   <WebView
     setSupportMultipleWindows={false}
     ref={webView}
     originWhitelist={['http://*', 'https://*', 'atrium://*']}
     source={{ uri: route.params.url }}
     style={{ flex: 1 }}
     javaScriptEnabled={true}
     onNavigationStateChange={handleWebViewNavigationStateChange}
     onShouldStartLoadWithRequest={handleWebViewNavigationStateChange}     
   />

和 route.params.url 是这样的: “https://int-widgets.moneydesktop.com/md/connect/vx59gg6k65tynA78p53dnvs42qZrk8b1cc74t4670wnprf6kbm4c2scfpfjyy2dgcv6g7h54Axpjm8bgzddtjx1zh3fft9t5n7dllAn4b6442vt3wplzzppc7r7h56n0hqthsbcxtnwAps3sjz0sfsspqjzw94h14nvym22g3tv528yfhctkv1hd0k35v1jf2l101yhvq43zb2x68x43p7bjsfyApqkfj0lptscs5pwA6045yznh4sbmvtkkfxkh9xcxkjr8vxwlc3mxwh5gp02z8lxb28016ywwhn45pdrsltxxvbtcyxpxp281dvnjp1d1q5Atn5qjs5fg3czxc1q25jsscAmq8dnfkmh5/eyJ1aV9tZXNzYWdlX3ZlcnNpb24iOjQsInVpX21lc3NhZ2Vfd2Vidmlld191cmxfc2NoZW1lIjoiYXRyaXVtIiwiY29sb3Jfc2NoZW1lIjoibGlnaHQiLCJpc19tb2JpbGVfd2VidmlldyI6dHJ1ZX0%3D” P>

【问题讨论】:

    标签: javascript react-native react-native-webview


    【解决方案1】:

    试试这个:

     <WebView
            {...this.props}
            bounces={false}
            originWhitelist={["https://*", "http://*", "file://*", "sms://*", 'atrium://*']}
            allowFileAccess={true}
            domStorageEnabled={true}
            javaScriptEnabled={true}
            geolocationEnabled={true}
            saveFormDataDisabled={true}
            allowFileAccessFromFileURLS={true}
            allowUniversalAccessFromFileURLs={true}
          />
    

    或者像这样修改你的函数:

    function onShouldStartLoadWithRequest(request){
      if (!request || !request.url) {
        return true;
      }
    
      // list of schemas we will allow the webview
      // to open natively
      if(request.url.startsWith("tel:") ||
        request.url.startsWith("mailto:") ||
        request.url.startsWith("maps:") ||
        request.url.startsWith("geo:") ||
        request.url.startsWith("sms:")
        ){
        Linking.openURL(request.url).catch(er => {
          console.log('Failed to open Link:', er.message);
        });
        return false;
      }
    
      // let everything else to the webview
      return true;
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2018-02-19
      • 1970-01-01
      • 2019-09-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-09-03
      相关资源
      最近更新 更多