【发布时间】:2019-03-28 09:58:39
【问题描述】:
我想使用 URLSession 绕过 HTTPS 服务器请求。我有一个客观的 c 代码,它运行良好。但是当我将此代码转换为 swift 并且它不起作用时。请检查并帮助我。我的代码做错了什么?
// Objective C Code
- (void)URLSession:(NSURLSession *)session task:(NSURLSessionTask *)task didReceiveChallenge:(NSURLAuthenticationChallenge *)challenge completionHandler:(void (^)(NSURLSessionAuthChallengeDisposition disposition, NSURLCredential *credential))completionHandler
{
completionHandler(NSURLSessionAuthChallengeUseCredential, [NSURLCredential credentialForTrust:challenge.protectionSpace.serverTrust]);
}
// Swift Code
func urlSession(_ session: URLSession, task: URLSessionTask, didReceive challenge: URLAuthenticationChallenge, completionHandler: @escaping (URLSession.AuthChallengeDisposition, URLCredential?) -> Void) {
if let aTrust = challenge.protectionSpace.serverTrust {
completionHandler(URLSession.AuthChallengeDisposition.useCredential, URLCredential(trust: aTrust))
}
}
【问题讨论】:
标签: ios swift https server nsurlsession