Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Timeout a ASIHTTP request

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]);
like image 377
Zen_silence Avatar asked Feb 21 '10 18:02

Zen_silence


2 Answers

[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.
}
like image 165
chrissr Avatar answered Nov 15 '22 09:11

chrissr


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];
    } 
}
like image 37
Oh Danny Boy Avatar answered Nov 15 '22 07:11

Oh Danny Boy