1

当前使用来自NSURLConnection的sendAsynchronous发出post和get请求,但无法获得响应中的状态码。大多数帖子都建议使用NSURLConnection及其委托方法,我理解它们也是异步的。异步请求的NSURLConnection状态代码iOS 6

我不明白代理如何将信息发送回调用方法(最终需要数据的方法)。 sendAsynchronous方法有一个我现在正在使用的回调。

我是新来的,谢谢你的帮助。

回答

7

当您使用sendAsynchronousRequest:queue:completionHandle方法,我会尽量在这方面来回答:

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