2011-07-22 82 views
0

我在我的iPhone應用程序中有用戶身份驗證。用戶登錄以應用程序將用戶名/密碼發送到Web服務進行用戶檢查的方式工作。我現在的問題是我不知道如何設置「超時」(30秒)並停止從Web服務請求數據?如何取消網絡服務呼叫 - IOS

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]; 
    }  

回答

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.

+0

我添加 '[REQ setTimeoutInterval:30];'。現在如果請求超時,這個處理程序在哪裏?它是' - (void)連接:(NSURLConnection *)連接didFailWithError:(NSError *)錯誤'? – 1110

0

您可以使用取消的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; 
0

方法也許ASIHTTP可以幫助你?

另外,如果我正確地理解了你,在stackoverflow上主要是similar issue