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]);
[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.
}
I normally check like this:
- (void)requestFailed:(ASIHTTPRequest *)request
{
NSString *message = NULL;
NSError *error = [request error];
switch ([error code])
{
case ASIRequestTimedOutErrorType:
message = kAlertMsgTimeoutError;
break;
case ASIConnectionFailureErrorType:
message = kAlertMsgConnectionFailError;
break;
case ASIAuthenticationErrorType:
message = kAlertMsgAuthFailError;
break;
case ASITooMuchRedirectionErrorType:
message = kAlertMsgTooManyRedirect;
break;
case ASIRequestCancelledErrorType:
message = kAlertMsgReqCancelledError;
break;
case ASIUnableToCreateRequestErrorType:
message = kAlertMsgUnableCreateReqError;
break;
case ASIInternalErrorWhileBuildingRequestType:
message = kAlertMsgUnableBuildReqError;
break;
case ASIInternalErrorWhileApplyingCredentialsType:
message = kAlertMsgUnableApplyCredError;
break;
case ASIFileManagementError:
message = kAlertMsgFileManageError;
break;
case ASIUnhandledExceptionError:
message = kAlertMsgUnhandledExcepError;
break;
case ASICompressionError:
message = kAlertMsgCompressionError;
break;
default:
message = kAlertMsgGenericError;
break;
}
if (NULL != message)
{
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:kApplicationTitle
message:message
delegate:self
cancelButtonTitle:kAlertDefaultCancel
otherButtonTitles:nil];
[alert show];
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With