NSURLCredentialStorage默认凭证不会自动使用

我从NSURLConnection切换到NSURLSession为我的应用程序通信,而我在它,试图从委派authentication转移到利用NSURLCredentialStorage。 我已经移动了代码,但是我得到了-URLSession:任务:在代理上调用了didReceiveChallenge,尽pipe在启动应用程序时在sharedCredentialStorage上设置了defaultCredentials。

保护空间是相同的(根据下面logging的消息设置凭证时所创build的凭证以及由NSURLAuthenticationChallenge传递的凭证):

Register credentials for: <NSURLProtectionSpace: 0x162227c0>: Host:192.168.1.99, Server:https, Auth-Scheme:NSURLAuthenticationMethodDefault, Realm:192.168.1.99, Port:23650, Proxy:NO, Proxy-Type:(null) Unexpected authentication challenge: <NSURLProtectionSpace: 0x1680ee40>: Host:192.168.1.99, Server:https, Auth-Scheme:NSURLAuthenticationMethodDefault, Realm:192.168.1.99, Port:23650, Proxy:NO, Proxy-Type:(null) 

并在didReceiveChallenge:(NSURLAuthenticationChallenge *)挑战委托方法:

 po [[NSURLCredentialStorage sharedCredentialStorage] defaultCredentialForProtectionSpace:[challenge protectionSpace]] 

结果是

 <NSURLCredential: 0x1680ff00>: thecorrectusername 

https://stackoverflow.com/a/501869/563905表明,当服务器响应一个401的挑战,NSURLConnection(这是一个NSURLSession的问题?)首先检查授权的标题(没有任何设置)和然后咨询NSURLCredentialStorage获取保护空间的凭证。

我只是不明白为什么我会得到被调用的didReceiveChallenge委托? 当我没有设置委托方法NSURLSession只需重新发送请求没有任何凭据…我很难过…

编辑:我已经添加了手动凭证处理didReceiveChallenge:方法,并且它正在触发每个请求,尽pipe只使用一个NSURLSession。

我只是有同样的问题,我的URLSession不会使用存储的凭据。 然后我阅读NSURLSession的参考文档。 基本上它说的是,如果你正在实现一个自定义的委托,那么当代理方法被调用时你必须自己处理所有的东西。 换句话说,保存凭证是战斗的一半。 每当服务器要求身份validation时,您都将收到挑战,因此在didReceiveChallenge方法中,您现在必须手动提取要使用的凭据并将其传递给完成处理程序。 让我知道,如果这是有道理的。