views:

178

answers:

1

Hello all

I am developing an iPhone application in which I am loading lots of data from another server via web-services .

I want have read somewhere in the apple guides that for network aware applications you have to set the network time out and after that you have alert the user for the same that "The network is not available".

How can I do so ?

+2  A: 

Here is the sample code to call Web Service with setup of request-timeout = 20. If it will not respond within time 20 then it will stop connecting and we get a nil data.

NSString* str = [NSString stringWithFormat:@"http://ws.geonames.org/findNearbyPostalCodes?lat=%f&lng=%f",curr_latitude,curr_longitude];
NSMutableURLRequest* request2=[NSMutableURLRequest requestWithURL:[NSURL URLWithString:str]];
[request2 setHTTPMethod:@"POST"]; 
[request2 setTimeoutInterval:20];
NSURLResponse *response=nil;
NSError *err=nil;
NSData *data1=[[NSURLConnection sendSynchronousRequest:request2 returningResponse:&response error:&err] retain];
if(data1 == nil)
{
 UIAlertView* alert = [[UIAlertView alloc]initWithTitle:@"Alert" message:@"The network is not available.\n Please check the Internet connection." delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];
 [alert show];
 [alert release];

}
  else
  {
         // It will store all data to data1
         // Here you can proceed with data1
  }
Jay Vachhani