I writed a simple code to get string value from a web server, now I wanna to set timeout, like, do getData 10 sec then if server not responding after 10 second cancel getting data and do “noConnect”. any idea how can I do that?
-(void)getData {
NSURL *url = [NSURL URLWithString: [NSString stringWithFormat:
@"http://exaple.com/example.plist"]];
NSDictionary *data = [NSDictionary dictionaryWithContentsOfURL:url];
NSString *aKey = [chamUpdateData objectForKey:@"count"];
}
-(void)noConnect{}
You could wrap the call to
dictionaryWith...in anNSOperationin order to execute it asynchronously; then start anNSTimerso that then it fires, you can check the status of theNSOperation; if it is stillrunning, then you cancel it.Keep in mind that there is no way I know of to actually stop the network request, once you have sent it. So, you simply have a timeout to manage the case when the connection takes longer than that. If the response arrives in 1 minute, you will simply ignore that data.