RGriffiths
RGriffiths

Reputation: 5970

Limiting time for checking internet connectivity in iPad app

I have an app that uses the following to check whether there is an internet connection. Is there any way of limiting the time that it takes to check? If the wireless connection is weak then it can take a while for the app to check.

- (BOOL)connectedToInternet
{
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL: [NSURL URLWithString:@"http://www.google.com/"]];
[request setHTTPMethod:@"HEAD"];
NSHTTPURLResponse *response;
[NSURLConnection sendSynchronousRequest:request returningResponse:&response error: NULL];
return ([response statusCode] == 200) ? YES : NO;
}

Upvotes: 0

Views: 28

Answers (1)

Peter Pei Guo
Peter Pei Guo

Reputation: 7870

Just set the timeout:

[request setTimeoutInterval:10];

The above will set timeout to 10 seconds, and you can adjust that number to what you need.

Upvotes: 2

Related Questions