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