iOS9----HTTPS 进行网络请求的解决方案

最近不知道使用 HTTPS 的猴子们有没有遇到这种错误呢?

CFNetwork SSLHandshake failed (-9801)
NSURLSession/NSURLConnection HTTP load failed (kCFStreamErrorDomainSSL, -9802)

后面的数字可以忽略掉,他们可以算作是同一种错误.不说没用的,现在就说解决办法,先来看看苹果的官方文档是怎么说的.

NSURLSession allows you to customize HTTPS server trust evaluation by implementing the -URLSession:didReceiveChallenge:completionHandler: delegate method. To customize HTTPS server trust evaluation, look for a challenge whose protection space has an authentication method of NSURLAuthenticationMethodServerTrust. For those challenges, resolve them as described below. For other challenges, the ones that you don't care about, call the completion handler block with the NSURLSessionAuthChallengePerformDefaultHandling disposition and a NULL credential.

When dealing with the NSURLAuthenticationMethodServerTrust authentication challenge, you can get the trust object from the challenge's protection space by calling the -serverTrust method. After using the trust object to do your own custom HTTPS server trust evaluation, you must resolve the challenge in one of two ways:

    If you want to deny the connection, call the completion handler block with the NSURLSessionAuthChallengeCancelAuthenticationChallenge disposition and a NULL credential.

    If you want to allow the connection, create a credential from your trust object (using +[NSURLCredential credentialForTrust:]) and call the completion handler block with that credential and the NSURLSessionAuthChallengeUseCredential disposition.
    Important: The system ignores the trust result of the trust object you use to create the credential; any valid trust object will allow the connection to succeed.

上面的意思就是说,NSURLSession 运行自定义服务器信任评价,通过实施 URLSessionDelegate 的代理方法
-URLSession:didReceiveChallenge:completionHandler:可以自定义服务器信任评价,中间还说了什么自己翻译吧.
关于实现自定义服务器信任评价有两中选择,一是不允许链接,我自己的理解,另一种就是NSURLCredential的对象,然后在上述的代理方法中回调代理方法中的 block 函数,然后就完事了,代码实现如下:

- (void)URLSession:(NSURLSession *)session didReceiveChallenge:(NSURLAuthenticationChallenge *)challenge completionHandler:(void (^)(NSURLSessionAuthChallengeDisposition, NSURLCredential * _Nullable))completionHandler
{
    SecTrustRef tmpSecTrustRef = challenge.protectionSpace.serverTrust;
    NSURLCredential *credential = [NSURLCredential credentialForTrust:tmpSecTrustRef];
    completionHandler(NSURLSessionAuthChallengeUseCredential, credential);
}

最后,还有一点需要做的是别忘记配置你的info.plist 文件,没有配置,只实现代码是没有用的,经过验证得到的结果.关于配置的部分如下截图:
这里写图片描述

现在,可以很随意的访问了.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值