2012-04-03 118 views
5

我使用新的iOS 5方法發出異步url請求:sendAsynchronousRequest:queue:completionHandler :.這使用塊來處理響應,但是沒有調用NSURLConnectionDelegate委託方法?我看不到在此實例中設置NSUrlConnection類的委託的方法?使用sendAsynchronousRequest處理401響應:queue:completionHandler:

[NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse *response, NSData *data, NSError *error) { 

    NSHTTPURLResponse *httpResponse = (NSHTTPURLResponse *)response; 

    NSLog(@"HTTP response code: %i", httpResponse.statusCode); 

    if (error != nil) { 
     NSLog(@"There was error with the synchronous request: %@", error.description);    
    } 
}]; 

我之所以需要委託方法是因爲我的電話的人得到一個401響應,iOS系統處理以不同的方式 - 推遲認證委託方法。當401響應返回時 - httpResponse.statusCode只是「0」。

有沒有辦法阻止iOS試圖處理401不同?特別是,我想我需要使用continueWithoutCredentialForAuthenticationChallenge:委託方法 - 但在這種情況下沒有委託被調用。

謝謝!

+0

使用與代表,像initWithRequest的連接方式之一:代表: – 2012-04-03 04:30:01

+0

看一看這個答案: http://stackoverflow.com/questions/12828060/authentication-with-nsurlconnection-sendasynchronousrequest-with-完成手 – 2013-07-29 12:34:23

回答

1

您需要使用NSURLConnection實例併爲其使用委託。 sendAsynchronousRequest:queue:completionHandler:是一個相同的快捷方式,但具有沒有委託的默認行爲。

0
NSMutableURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:urlString]]; 

NSURLConnection *connection = [[NSURLConnection alloc] nitWithRequest:request delegate:self]; 

使用上面的方法會觸發委託方法。