1

コードバを使用しwebviewて、js を使用してクリック時に shouldStartLoad イベントをトリガーするボタンを持つ html ファイルをロードしています。

同じボタンが押されたときに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を使用しているため、ロードリクエストを実行する前に、 ConnectionAPIを使用してインターネット接続を最初に確認できます。

<!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 に答える