2012-12-05 89 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

相關問題