1

我对此有点困惑。我有这个方法。

-(BOOL)verifyAuth: (NSString*)username forPassword:(NSString*)password
{
    NSURLSession *session = [NSURLSession sharedSession];
    NSURLCredential *creds = [NSURLCredential credentialWithUser:username password:password persistence:NSURLCredentialPersistenceForSession];

    [session dataTaskWithURL:MALAuthVerifyURL completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) 
    {

         NSLog(@"%@",data);
    }];
    return YES;
}

如何将凭据传递给 NSURLSession 对象?苹果的文档是这样说的:

“在你创建了 NSURLCredential 对象之后:

对于 NSURLSession,使用提供的完成处理程序块将对象传递给身份验证质询的发送者。”

但我不确定这实际上意味着什么。我找不到这种用法的任何例子。

谢谢。

4

2 回答 2

1

https://developer.apple.com/library/ios/documentation/cocoa/Conceptual/URLLoadingSystem/Articles/AuthenticationChallenges.html#//apple_ref/doc/uid/TP40009507-SW1

HTTP 基本身份验证 (NSURLAuthenticationMethodHTTPBasic) 需要用户名和密码。提示用户提供必要的信息并使用 credentialWithUser:password:persistence: 创建一个 NSURLCredential 对象。

创建 NSURLCredential 对象后:

  • 对于 NSURLSession,使用提供的完成处理程序块将对象传递给身份验证质询的发送者。
  • 对于 NSURLConnection 和 NSURLDownload,使用 useCredential:forAuthenticationChallenge: 将对象传递给身份验证质询的发送者。

也许这有帮助。

于 2014-04-02T08:45:00.713 回答
1

您需要实现另一个委托方法,-URLSession:task:didReceiveChallenge:challenge:completionHandler:.

于 2013-11-19T06:09:05.950 回答