2014-07-24 26 views
2

我在iOS中遇到了很大的問題。 由於某些原因,我必須使用異步,因爲我想通過SSL錯誤。 因此,我使用NSURLconnection並從didReceiveData中的Web服務器獲取響應數據。 ,當我只發佈一個網址到服務器時,它工作得很好。iOS NSURLConnection didReceivedData不止一個任務

但我的問題是:如果我需要在同一時間發佈2或3不同的URL到服務器! 然後我收到了didReceiveData中的響應數據,我認爲它會混淆! 我怎麼知道哪個回覆的數據屬於哪個帖子? 有沒有人可以幫助我?請..謝謝。

+1

使用每個委託方法的'NSURLConnection'參數來知道響應是用於哪個連接。 – rmaddy

+0

rmaddy,你能給我示例代碼給每個委託的NSURLConnection參數,我需要寫不同的didReceiveData? – user3286613

回答

2

爲此,您必須檢查connection代表方法NSURLConnection。 &使用兩個不同的resposeData。

例子: 這裏兩個連接使用connSend

NSURL *url = // Your URL 
NSURLRequest *requestObj = [NSURLRequest requestWithURL:url]; 
connSend=[[NSURLConnection alloc] initWithRequest:requestObj delegate:self]; 

connSend & connRecieve

如果和其他連接

NSURL *url = // Your URL 
NSURLRequest *requestObj = [NSURLRequest requestWithURL:url]; 
connRecieve =[[NSURLConnection alloc] initWithRequest:requestObj delegate:self] 




- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response { 
    if (connection==connSend) { 
     responseSend = [[NSMutableData alloc]init]; 
     [responseSend setLength:0]; 
    } 
    else{ 
     responseData = [[NSMutableData alloc]init]; 
     [responseData setLength:0]; 
    } 
} 

- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data { 
    if (connection==connSend) { 
     [responseSend appendData:data]; 
    } 
    else{ 
     [responseData appendData:data]; 
    } 
} 

-(void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error { 
    if (connSend==connection) { 
     NSLog(@"Error in sending"); 
    } 
    else{ 
     NSLog(@"Error in receiving"); 
    } 
} 

- (void)connectionDidFinishLoading:(NSURLConnection *)connection{ 

    if (connection==connSend) { 
    // Connection send. 
    } 
    else{ 
    // Connection recive 
    } 

} 

它更好的,你可以得到不同的服務器性反應的創建不同類,

+0

connSend寫在: NSURLConnection * conn = [[NSURLConnection alloc] initWithRequest:request delegate:connSend]; 委託人:self =>委託:connSend right?謝謝 – user3286613

+0

使NSURLConnection成爲全球。 –

+0

NSURLConnection * connSend1 = [[NSURLConnection alloc] initWithRequest:request delegate:self]; NSURLConnection * connSend2 = [[NSURLConnection alloc] initWithRequest:request delegate:self]; 對不對?謝謝 – user3286613