次のような NSURL があります。
NSURL *url = [NSURL URLWithString:@"http://jamessuske.com/isthedomeopen/isthedomeopenGetData.php"];
しかし、インターネット接続や電話信号がない場合、アプリがクラッシュします。使用前に最初に接続をテストする方法はありますか?
試してキャッチする必要がありますか?について読んNSURLConnection
でいますが、それがどのように機能するかは不明です。何か案は?
私が得ているエラーはThread 6:signal SIGABRT
、これが何を意味するのか分かりません。以下のすべての例を試しましたが、どれもうまくいきませんでした。
私はこれを追加しました:
- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error
{
// release the connection, and the data object
// receivedData is declared as a method instance elsewhere
[connection release];
// inform the user
UIAlertView *didFailWithErrorMessage = [[UIAlertView alloc] initWithTitle: @"NSURLConnection " message: @"didFailWithError" delegate: self cancelButtonTitle: @"Ok" otherButtonTitles: nil];
[didFailWithErrorMessage show];
[didFailWithErrorMessage release];
}
インターネットから Mac のプラグを抜き、シミュレーターでアプリを実行すると、これが表示されますThread 6:signal SIGABRT
以下のコードを使用しました
Reachability *networkReachability = [Reachability reachabilityForInternetConnection];
NetworkStatus networkStatus = [networkReachability currentReachabilityStatus];
if (networkStatus == NotReachable) {
dispatch_async(dispatch_get_main_queue(), ^{
UIAlertView *alert = [[UIAlertView alloc] initWithTitle: @"Announcement" message: @"No Connection" delegate: nil cancelButtonTitle:@"OK" otherButtonTitles:nil]; [alert show]; [alert release];
});
} else {
dispatch_async(dispatch_get_main_queue(), ^{
UIAlertView *alert = [[UIAlertView alloc] initWithTitle: @"Announcement" message: @"Connection" delegate: nil cancelButtonTitle:@"OK" otherButtonTitles:nil]; [alert show]; [alert release];
});
入力に接続している場合は機能しますが、インターネットをオフにするとこのエラーが発生し、else アラートが表示されません
Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'data parameter is nil'