iOS NSURLConnection didReceivedData不止一个任务

问题描述:

我在iOS中遇到了很大的问题。 由于某些原因,我必须使用异步,因为我想通过SSL错误。 因此,我使用NSURLconnection并从didReceiveData中的Web服务器获取响应数据。 ,当我只发布一个网址到服务器时,它工作得很好。iOS NSURLConnection didReceivedData不止一个任务

但我的问题是:如果我需要在同一时间发布2或3不同的URL到服务器! 然后我收到了didReceiveData中的响应数据,我认为它会混淆! 我怎么知道哪个回复的数据属于哪个帖子? 有没有人可以帮助我?请..谢谢。

+1

使用每个委托方法的'NSURLConnection'参数来知道响应是用于哪个连接。 – rmaddy

+0

rmaddy,你能给我示例代码给每个委托的NSURLConnection参数,我需要写不同的didReceiveData? – user3286613

为此,您必须检查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