自从我使用iOS 9升级现有项目以来,我一直收到错误:
发生SSL错误,无法与服务器建立安全连接.
我正在开发一款iPhone应用程序.在开发期间,我需要连接到使用自签名SSL证书的服务器.我很确定- (void)URLSession:(NSURLSession *)session didReceiveChallenge:(NSURLAuthenticationChallenge *)challenge completionHandler:(void (^)(NSURLSessionAuthChallengeDisposition, NSURLCredential *))completionHandler我有机会编写一些异常代码来实现这一点.但是,我找不到任何资源告诉我如何做到这一点.我可以在日志中看到以下错误:
NSURLConnection/CFURLConnection HTTP load failed (kCFStreamErrorDomainSSL, -9813)
Run Code Online (Sandbox Code Playgroud)
除此之外,当我NSLog(@"error = %@", error);从上面的委托方法中得到:
错误域= NSURLErrorDomain代码= -1202"此服务器的证书无效.您可能正在连接到假装为"api.mydevelopmenturl.com"的服务器,这可能会使您的机密信息面临风险." UserInfo = 0x10cbdbcf0 {NSUnderlyingError = 0x112ec9730"此服务器的证书无效.您可能正在连接到假装为"api.mydevelopmenturl.com"的服务器,这可能会使您的机密信息面临风险.",NSErrorFailingURLStringKey = https: //api.mydevelopmenturl.com/posts,NSErrorFailingURLKey = https://api.mydevelopmenturl.com/posts,NSLocalizedRecoverySuggestion =您是否还要连接到服务器?,NSURLErrorFailingURLPeerTrustErrorKey =,NSLocalizedDescription =此服务器的证书无效.您可能正在连接到假装为"api.mydevelopmenturl.com"的服务器,这可能会使您的机密信息面临风险.}
有关如何解决此问题的任何想法?请在我阅读概念文档时发布代码,但我不理解它们.这是一个超越我的例子:https://developer.apple.com/library/content/technotes/tn2232/_index.html
如何使用 Swift 在 iOS 上修复此问题?当我发出服务器请求时,出现以下错误:
\n\n\n\n该服务器的证书无效。您可能正在连接到冒充 \xe2\x80\x9c...\xe2\x80\x9d 的服务器,这可能会使您的机密信息面临风险。
\n
我之前尝试过所有以前的答案,但没有一个对我有用。我也许知道问题所在,而且我猜错是误导了。我正在尝试向https是但没有SSL的服务器发出api请求。API也只能通过VPN运作。因此,我禁用了SSL验证,除了真正的iOS设备以外,它在其他任何地方(在模拟器,邮递员,android,mac中)都能正常运行。
到目前为止,我得到的是:
在我看来,该错误从字面上似乎是我的代码或某些逻辑而必须在iOS上完成才能在安全性上实际在实际设备上运行(如果存在)。
因此,在这里,我将分享使它到目前为止在模拟器上都能运行的实现(在此之前,我在模拟器上遇到相同的错误)。
我已将URLSessionDelegate实现到路由器类,并允许在info.plist中进行任意加载。因此,所有这些都是正确的网址,请求等。
委托不会在真实设备上被调用。
func urlSession(_ session: URLSession, didReceive challenge: URLAuthenticationChallenge, completionHandler: @escaping (URLSession.AuthChallengeDisposition, URLCredential?) -> Void) {
completionHandler(.useCredential, URLCredential(trust: challenge.protectionSpace.serverTrust!))
}
Run Code Online (Sandbox Code Playgroud)
提出要求之前:
let config = URLSessionConfiguration.default
let session = URLSession(configuration: config, delegate: self, delegateQueue: .main)
Run Code Online (Sandbox Code Playgroud)
信息plist文件内容
<key>NSAppTransportSecurity</key>
<dict>
<key>NSExceptionDomains</key>
<dict>
<key>*my.domain*.com</key>
<dict>
<key>NSIncludesSubdomains</key>
<true/>
<key>NSExceptionRequiresForwardSecrecy</key>
<false/>
<key>NSTemporaryExceptionMinimumTLSVersion</key>
<string>TLSv1.0</string>
</dict>
</dict>
</dict>
Run Code Online (Sandbox Code Playgroud)
控制台错误:
[] nw_proxy_resolver_create_parsed_array PAC evaluation error:
NSURLErrorDomain: -1003
2018-06-26 20:12:08.646042+0530 MyApp[806:161960] TIC TCP Conn Failed [1:0x1c416f000]: 12:8 Err(-65554)
2018-06-26 20:12:08.646740+0530 MyApp[806:161964] Task <DCE45907-5758-4CC0-91A1-9EFD53FFDA0A>.<1> HTTP …Run Code Online (Sandbox Code Playgroud) 我目前在以下职位上工作。这是代码:
SecCertificateRef certs = NULL;
SecPolicyRef policy = NULL;
NSString *publicKeyString = @"MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQCeJ8N8fuGShAJnniDg4yuRrxrG61ZF2T24eXSEH87jCJmLbc+MV70AgP/LC8btzSU4FFP56lBmDcmW+Prupf5gO1RXhjPIlET73t5Ny1I3ze+xaShAA9qB0c9dNb26NxVd95wCHNmQhon9qBFmTVZb0CdgscxYcDuLOGskDnATrwIDAQAB";
NSData *publicKeyStringData = [[NSData alloc] initWithBase64EncodedString:publicKeyString options:0];
certs = SecCertificateCreateWithData(kCFAllocatorDefault, (__bridge CFDataRef) publicKeyStringData);
Run Code Online (Sandbox Code Playgroud)
根据帖子,如果certs变量为NULL,则数据格式不正确。我检查了上面的公钥,它确实是base64,所以我看不出为什么certs会是NULL?