iOS: trigger NSURLErrorServerCertificateUntrusted instead of NSURLErrorCancelled when failing the AuthenticationChallenge

左心房为你撑大大i 提交于 2020-01-06 07:59:44

问题


I'm handling the server trust authentication challenge in a custom way (comparing the public key from the server trust with a hashed local version) through the urlSessionDelegate, similarly to how it is done here: How do I accept a self-signed SSL certificate using iOS 7's NSURLSession and its family of delegate methods for development purposes?

As described in the documentation, I need to cancel the challenge if I want to fail the server trust authentication myself. When doing this, my data task will complete with the NSURLErrorCancelled. I have some error handling classes which look at the resulting NSURLError returned by the datatask, and it would be a lot cleaner for me if I got NSURLErrorServerCertificateUntrusted instead.

Is there a way to achieve that? Or will those server errors only be forwarded when the authentication is handled automatically, without the delegate methods?


回答1:


Would the normal handling of the certificate in question fail with that error?

If so, you could use NSURLSessionAuthChallengePerformDefaultHandling in that case.

If not, I think the only way would involve mucking around with the challenge sender, which is highly discouraged, though it might not be catastrophic if you call that and afterwards call the proper completion handler. There's a good chance you'd get two callbacks, though. I've never attempted this.

My advice would be to file a bug with Apple asking for a proper API to pass an NSError along with cancellation.



来源:https://stackoverflow.com/questions/56225350/ios-trigger-nsurlerrorservercertificateuntrusted-instead-of-nsurlerrorcancelled

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!