2012-12-05 171 views
0

我正在使用cordova webview加载一个带有按钮的html文件,该按钮会在点击时使用js触发shouldStartLoad事件。当没有互联网连接时,ios phonegap webview事件

所有工作正常,除非当没有互联网连接,当按下相同的按钮,shouldStartLoad事件没有被解雇。我需要拦截该触发器才能显示本地警报,但似乎没有任何反应,如果互联网连接再次可用,事件也会在点击时再次触发。 控制台未显示任何信息。如何在没有连接时在科尔多瓦webview上拦截此状态?

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

回答

0

如果没有互联网连接,你按下按钮来触发某些事件,检查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> 
+0

didFailWithError也没有触发,并使用HTML是没办法了。假设html在webview上正确加载并且具有触发事件onClick的按钮,那么我将禁用设备上的Internet连接。现在点击时,我必须捕捉任何事件以检查互联网连接。但交互状态在conn check之前。 – Jaume

相关问题