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

在IOS中,使用自签名证书或者没有达到苹果要求的证书时,需要在INFO.LIST中加入字段
在XCODE中使用sorce code的方式直接编辑加入如下内容

    <key>NSAppTransportSecurity</key>
    <dict>
        <key>NSAllowsArbitraryLoads</key>
        <true/>
    </dict>

加了以上内容后,如果使用自签名的证书,会提示如下内容
The certificate for this server is invalid. You might be connecting to a server that is pretending to be “xxxxx.com.cn” which could put your confidential information at risk.
NSURLSession需要设置代理NSURLSessionDelegate
控制器要实现如下方法

@interface ViewController ()<NSURLSessionDelegate>

@end

@implementation ViewController
- (void)URLSession:(NSURLSession *)session didReceiveChallenge:(nonnull NSURLAuthenticationChallenge *)challenge completionHandler:(nonnull void (^)(NSURLSessionAuthChallengeDisposition, NSURLCredential * _Nullable))completionHandler{
    NSLog(@"%@",challenge.protectionSpace.host);
    NSURLCredential *urlCre = [NSURLCredential credentialForTrust:challenge.protectionSpace.serverTrust];
    completionHandler(NSURLSessionAuthChallengeUseCredential,urlCre);
}
- (void)viewDidLoad {
    [super viewDidLoad];
    NSURLSession *ses = [NSURLSession sessionWithConfiguration:nil delegate:self delegateQueue:[NSOperationQueue currentQueue]] ;
}

最后就可以正常访问HTTPS网址了。

搜了很多文章
http://xinpure.com/nsurlsessionnsurlconnection-http-load-failed-kcfstreamerrordomainssl-9802/
http://stackoverflow.com/questions/30739473/nsurlsession-nsurlconnection-http-load-failed-on-ios-9/30748166#30748166
不能解决问题
最后看了http://stackoverflow.com/questions/23241872/nsurlconnection-cfurlconnection-http-load-failed-kcfstreamerrordomainssl-9813的回答得到了解决方法。
苹果的NSURLSession的编程指南
https://developer.apple.com/library/ios/documentation/Cocoa/Conceptual/URLLoadingSystem/URLLoadingSystem.html#//apple_ref/doc/uid/10000165i

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值