2012-03-22 15 views
0

インターネットに接続していないときはどうすれば検出できますか?または接続がタイムアウトした場合。 NSURLConnectionクラスを使用しています。インターネットに接続していないことを検出する方法

+1

で説明したようにあなたは 'Reachability'クラスを使用することができます[?有効なインターネット接続を検出するために、到達可能性クラスを使用する方法](http://stackoverflow.com/questions/5195012/how-to-use-到達可能性クラス間検出インターネット接続) – sch

+1

重複:http://stackoverflow.com/questions/1083701/how-to-check-for-an-active-internet-connection-on-iphone- SDK –

答えて

0

インターネット接続を確認する最も簡単な方法は、次のコードを使用することです。

NSString *URLString = [NSString stringWithContentsOfURL:[NSURL URLWithString:@"http://www.google.com"]]; 
NSString *result = [[NSString alloc] init]; 
result = (URLString != NULL) ? @"Yes" : @"No"; 
NSLog(@"Internet connection availability : %@", result); 

が、このコードは、google.com

1

アップルの可用性に依存していypurプロジェクトにこれを追加し、あなたに到達可能性クラスを提供します。 Reachability.hとReachability.mファイルをインポートします。 systemConfigurationフレームワークを追加します。そしてあなたのコードに次のスニペットを追加してください。

Reachability *internetReachableinst; 
internetReachableinst= [Reachability reachabilityWithHostName:@"www.google.com"]; 
internetReachableinst.reachableBlock = ^(Reachability*reach) 
    { 

     // Update the UI on the main thread 
     dispatch_async(dispatch_get_main_queue(), ^{ 
      NSLog(@"Internet is connected"); 
     }); 
    }; 

    //Internet is not reachable 
    internetReachableinst.unreachableBlock = ^(Reachability*reach) 
    { 

     //Update the UI on the main thread 
     dispatch_async(dispatch_get_main_queue(), ^{ 
      NSLog(@"No internet"); 
     }); 
    }; 
    [internetReachableinst startNotifier]; 
関連する問題