views:

651

answers:

2

Hello,

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]);
+2  A: 
[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.
}
chrissr
Does that return something? because i need to have a statement like if (timeout == true) { //Some Error message }else { //Proceed to next step}
Zen_silence
That just tells your request that it should timeout after 10 seconds. If the request times out, it'll call your requestFailed: delegate which you can check for the ASIRequestTimedOutError (as explained by tt.Kilew below).
chrissr
Can you please post some sample code i cant get this to work at all
Zen_silence
More code added.
chrissr
Working Perfectly thanks... now i just have to rewrite my login function :(
Zen_silence
Glad to help. Please mark this as correct when you get the chance.
chrissr
A: 
// Number of seconds to wait before timing out - default is 10
NSTimeInterval timeOutSeconds;

If timeout will occur, you'll recieve

[self failWithError:ASIRequestTimedOutError];
tt.Kilew
That doesnt work look at the sample code i posted please
Zen_silence