iosnsurlconnectiondelegate

iOS NSURLConnection didReceivedData more than one task


I have big problem in iOS. For some reasons,I must use asynchronous,Because I want to pass SSL error. So,I use NSURLconnection and get response data from Web server in didReceiveData. and it work very well when I just post one url to server.

But my question is : if I need to post 2 or 3 different url to server in the same time! then I receive response data in didReceiveData, I think it will be confusion! How can I know which response's data is belong to which post task?? Have anyone can help me? Please.. thanks.


Solution

  • For that you have to check your connection in Delegate methods of NSURLConnection. & use two different resposeData.

    Example: here two connections connSend & connRecieve

    If using connSend

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

    And For other Connection

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

    Its better you can create different class for getting different server responce,