Pergunta

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.

Foi útil?

Solução

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

Outras dicas

I have a wrapper class that makes this very easy and keeps your code clean: https://github.com/ricardocontrerasrobles/EasyNetwork

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top