5

NSURLConnection's sendSynchronousRequest:returningResponse:error:メソッド(別のスレッド)を使用NSOperationして外部サーバーに接続し、データを取得します。操作がタイムアウトしたか、またはその他のネットワークエラーが発生したかどうかを確認するにはどうすればよいですか?

4

3 に答える 3

14

If there was an error, the error parameter will be non-nil when sendSynchronousRequest:returningResponse:error: returns.

You can retrieve the error code by checking the value returned by [NSError code]. The error code for time out is NSURLErrorTimedOut.

For instance:

NSError *error = nil;
[NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error]

if (error.code == NSURLErrorTimedOut) {
// Handle time out here
}
于 2012-10-10T12:24:58.013 に答える
-1

ユーザーにアラートを提示し、エラー パラメータをsendSynchronousRequest:returningResponse:error:アラートのメッセージに渡すことができます。

コードは次のようになります。

[NSURLConnection sendSynchronousRequest: req returningResponse: &response error: &error];

if (error)
{
UIAlertView * alert = [[UIAlertView alloc]initWithTitle:@"Error" message:[error localizedDescription] delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil, nil];
[alert show];
}

それが役に立てば幸い!!

于 2012-10-10T12:17:13.233 に答える