Timeout a ASIHTTP request
asihttprequest, iphone
Solution
[request setTimeOutSeconds:10];
Update with more code:
NSURL *url = [NSURL URLWithString:@"A URL WITH A FORM"];
ASIFormDataRequest *requestPOST = [ASIFormDataRequest requestWithURL:url];
[requestPOST setPostValue:un forKey:@"username"];
[requestPOST setPostValue:pw forKey:@"password"];
[requestPOST setPostValue:@"Login" forKey:@"submit"];
[requestPOST setTimeOutSeconds:10];
[requestPOST setDelegate:self]
[requestPOST startAsynchronous];
Failed handler:
- (void)requestFailed:(ASIHTTPRequest *)request {
NSError *error = [request error];
if ([error isKindOfClass:[ASIRequestTimedOutError class]]) {
// Actions specific to timeout
}
}
Success handler:
- (void)requestFinished:(ASIHTTPRequest *)request
{
// Use when fetching text data
NSString *responseString = [request responseString];
// Use when fetching binary data
NSData *responseData = [request responseData];
// Do something with the response.
}
Problem
I'm using the ASIHTTPRequest library to request some data from a server in my iPhone app. But i cannot figure out how to create a timeout so that if the server goes down or the iPhone doesnt have internet connection the app doesnt crash. Thanks in advance EDIT>>> tt.Kilew your code doesnt work... I posted a bit of sample code ``` NSURL *url = [NSURL URLWithString:@"A URL WITH A FORM"]; ASIFormDataRequest *requestPOST = [ASIFormDataRequest requestWithURL:url]; [requestPOST setPostValue:un forKey:@"username"]; [requestPOST setPostValue:pw forKey:@"password"]; [requestPOST setPostValue:@"Login" forKey:@"submit"]; [requestPOST start]; [requestPOST setTimeOutSeconds:10]; NSLog(@"Fail: %@", [requestPOST failWithError:ASIRequestTimedOutError]); ```