1

我正在使用cordovawebview加载一个html文件,该文件有一个按钮,使用js在点击时触发shouldStartLoad事件。

一切工作正常,除非没有互联网连接,按下相同的按钮时shouldStartLoad不会触发事件。我需要拦截该触发器以显示本机警报,但似乎没有任何反应,如果互联网连接再次可用,单击时也会再次触发事件。控制台未显示任何信息。webview没有连接时如何在科尔多瓦上拦截这种状态?

- (BOOL)webView:(UIWebView*)theWebView shouldStartLoadWithRequest:(NSURLRequest*)request 
navigationType:(UIWebViewNavigationType)navigationType;
4

1 回答 1

-1

如果没有互联网连接并且您按下按钮来触发某些事件,请检查UIWebViewDelegate调用的方法didFailWithError

- (void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error {

     //Check the error type and show the appropriate alert to user.
}

并且当您使用 Phonegap 时,您始终可以在触发任何加载请求之前先使用Connection API检查互联网连接:

<!DOCTYPE html>
<html>
  <head>
    <title>navigator.connection.type Example</title>

    <script type="text/javascript" charset="utf-8" src="cordova-2.2.0.js"></script>
    <script type="text/javascript" charset="utf-8">

    document.addEventListener("deviceready", onDeviceReady, false);

    function onDeviceReady() {
        //checkConnection();
    }

    function checkConnection() {
        var networkState = navigator.connection.type;

        var states = {};
        states[Connection.UNKNOWN]  = 'Unknown connection';
        states[Connection.ETHERNET] = 'Ethernet connection';
        states[Connection.WIFI]     = 'WiFi connection';
        states[Connection.CELL_2G]  = 'Cell 2G connection';
        states[Connection.CELL_3G]  = 'Cell 3G connection';
        states[Connection.CELL_4G]  = 'Cell 4G connection';
        states[Connection.NONE]     = 'No network connection';

        alert('Connection type: ' + states[networkState]);
        if(networkState==Connection.NONE)
          return false;
        else
         return true; 
    }
    function loadGoogle() {

      if(checkConnection()){
       // Do your logical stuff here
       window.location="https://google.com";
      }
      else {
        // Handle connection error 
      }
    }
    </script>
  </head>
  <body>
    <p>A dialog box will report the network state.</p>
    <button onclick="loadGoogle()">Load Google</button>
  </body>
</html>
于 2012-12-05T11:29:11.343 回答