使用AFNNetworking访问网络,在开发调试阶段,可能会碰到后端证书不受信任导致访问出错.在不改变AFNNetworking源码的前提下解决方法如下:
问题描述,细心可以发现报错信息里面已经提示是证书问题了
Error Domain=NSURLErrorDomain Code=-1202 "The certificate for this server is invalid. You might be connecting to a server that is pretending to be “10.0.82.221” which could put your confidential information at risk." UserInfo={NSURLErrorFailingURLPeerTrustErrorKey=<SecTrustRef: 0x600000117460>, NSLocalizedRecoverySuggestion=Would you like to connect to the server anyway?, _kCFStreamErrorDomainKey=3, _kCFStreamErrorCodeKey=-9843, NSErrorPeerCertificateChainKey=(
"<cert(0x7f964e077600) s: app.jtxxshopping.com i: Symantec Basic DV SSL CA - G1>",
"<cert(0x7f964e075000) s: Symantec Basic DV SSL CA - G1 i: VeriSign Class 3 Public Primary Certification Authority - G5>"
), NSUnderlyingError=0x608000441bc0 {Error Domain=kCFErrorDomainCFNetwork Code=-1202 "(null)" UserInfo={_kCFStreamPropertySSLClientCertificateState=0, kCFStreamPropertySSLPeerTrust=<SecTrustRef: 0x600000117460>, _kCFNetworkCFStreamSSLErrorOriginalValue=-9843, _kCFStreamErrorDomainKey=3, _kCFStreamErrorCodeKey=-9843, kCFStreamPropertySSLPeerCertificates=(
"<cert(0x7f964e077600) s: app.jtxxshopping.com i: Symantec Basic DV SSL CA - G1>",
"<cert(0x7f964e075000) s: Symantec Basic DV SSL CA - G1 i: VeriSign Class 3 Public Primary Certification Authority - G5>"
)}}, NSLocalizedDescription=The certificate for this server is invalid. You might be connecting to a server that is pretending to be “10.0.82.221” which could put your confidential information at risk., NSErrorFailingURLKey=https://10.0.82.221/jtshopping/rest/qiniu/getToken, NSErrorFailingURLStringKey=https://10.0.82.221/jtshopping/rest/qiniu/getToken, NSErrorClientCertificateStateKey=0}
解决方法
// 1.初始化单例类
AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
// 2.设置非校验证书模式
manager.securityPolicy = [AFSecurityPolicy policyWithPinningMode:AFSSLPinningModeNone];
manager.securityPolicy.allowInvalidCertificates = YES;
[manager.securityPolicy setValidatesDomainName:NO];
备注:如有不足还请告知.
网友评论