Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to detect if NSURLConnection's sendSynchronousRequest:returningResponse:error: ended up being timed out or other error

I use NSURLConnection's sendSynchronousRequest:returningResponse:error: method (in a separate NSOperation thread) to connect to external server to retreive data. How do I know if the operation ended timed out, or some other network error?

like image 566
ikevin8me Avatar asked Oct 10 '12 12:10

ikevin8me


1 Answers

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
}
like image 119
g_fred Avatar answered Oct 05 '22 08:10

g_fred