How to cancel web service call - IOS
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 servi开发者_如何学JAVAce?
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];
}
[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.
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;
Maybe ASIHTTP might help you?
Also, if I understood you correctly, a mostly similar issue on stackoverflow.
精彩评论