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

来源:互联网 发布:苹果软件怎么下载 编辑:程序博客网 时间:2024/06/03 11:19

在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

0 0