0

I have user authentication in my iPhone application. User login works in a way that application send username/password to web service for user check. The problem I have now is I don't know how to setup 'timeout' (30 seconds) and stop requesting data from web service?

NSString *soapMsg = 
    [NSString stringWithFormat:
     @"<?xml version=\"1.0\" encoding=\"utf-8\"?>...", username, password
     ];

    NSURL *url = [NSURL URLWithString: @"http://...Service.asmx"];      

    NSMutableURLRequest *req = [NSMutableURLRequest requestWithURL:url];    
    NSString *msgLength = [NSString stringWithFormat:@"%d", [soapMsg length]];

    [req addValue:@"text/xml; charset=utf-8" forHTTPHeaderField:@"Content-Type"];   
    [req addValue:@"http://.../LoginUser" forHTTPHeaderField:@"SOAPAction"];    
    [req addValue:msgLength forHTTPHeaderField:@"Content-Length"];   
    [req setHTTPMethod:@"POST"];
    [req setHTTPBody: [soapMsg dataUsingEncoding:NSUTF8StringEncoding]];

    conn = [[NSURLConnection alloc] initWithRequest:req delegate:self];

    if (conn) 
    {
        webData = [[NSMutableData data] retain];
    }    
1110
  • 7,829
  • 55
  • 176
  • 334

3 Answers3

4
[req setTimeoutInterval:urInterval];
setTimeoutInterval:

Sets the receiver’s timeout interval, in seconds.

- (void)setTimeoutInterval:(NSTimeInterval)timeoutInterval

Parameters

timeoutInterval

The timeout interval, in seconds. If during a connection attempt

the request remains idle for longer than the timeout interval, the request is considered to have timed out. The default timeout interval is 60 seconds.

visakh7
  • 26,380
  • 8
  • 55
  • 69
  • I added '[req setTimeoutInterval:30];'. And now if request is time out where is the handler for this? Is it '-(void) connection:(NSURLConnection *) connection didFailWithError:(NSError *) error '? – 1110 Jul 22 '11 at 07:26
0

You can use the cancel method of the NSURLConnection

/*! 
    @method cancel
    @abstract Cancels an asynchronous load of a URL.
    @discussion Once this method is called, the asynchronous load is
    stopped, and the NSURLConnectionDelegate associated with the
    receiver will no longer receive any asynchronous callbacks for
    this NSURLConnection.
*/
- (void)cancel;
Praveen S
  • 10,355
  • 2
  • 43
  • 69
0

Maybe ASIHTTP might help you?

Also, if I understood you correctly, a mostly similar issue on stackoverflow.

Community
  • 1
  • 1
Christopher Will
  • 2,991
  • 3
  • 29
  • 46