Search code examples
cocoa-touchios6http-status-code-401nsurlconnectiondelegatesendasynchronousrequest

NSURLConnection status code for Asynchronous request iOS 6


Currently using sendAsynchronous from NSURLConnection to make post and get requests, but can't get the status code in the response. Most post suggest the use of NSURLConnection and its delegate methods, which I understand are also Asynchronous.

I don't understand how the delegates send information back to the calling method (the method that eventually needs the data). The sendAsynchronous method has a call back that I am using right now.

I'm new to this, thank you for your help.


Solution

  • As you are using sendAsynchronousRequest:queue:completionHandle method, I will try to answer in that context:

    [NSURLConnection sendAsynchronousRequest:request 
                                       queue:queue 
                           completionHandler:
        ^(NSURLResponse *response, NSData *data, NSError *error) {
    
        // This will get the NSURLResponse into NSHTTPURLResponse format
        NSHTTPURLResponse* httpResponse = (NSHTTPURLResponse*)response;
    
        // This will Fetch the status code from NSHTTPURLResponse object
        int responseStatusCode = [httpResponse statusCode];
    
        //Just to make sure, it works or not
        NSLog(@"Status Code :: %d", responseStatusCode);
    }];