ios – 自签名证书的Swift SSL错误
发布时间:2020-12-14 19:34:02 所属栏目:百科 来源:网络整理
导读:此代码尝试访问并且无法访问在浏览器中工作的SSL URL: let path = "https://localhost:8443/greeting"let request = NSMutableURLRequest(URL: NSURL(string: path)!)let session = NSURLSession.sharedSession()let task = session.dataTaskWithRequest(re
此代码尝试访问并且无法访问在浏览器中工作的SSL URL:
let path = "https://localhost:8443/greeting" let request = NSMutableURLRequest(URL: NSURL(string: path)!) let session = NSURLSession.sharedSession() let task = session.dataTaskWithRequest(request,completionHandler: {data,response,error -> Void in let json:JSON = JSON(data: data!) if let c = json["content"].string { print(c) } }) task.resume() 失败并出现错误:
允许应用程序接受此证书需要什么? 有问题的证书是自签名的.在SO上阅读一些解决方案但没有成功. 运行Xcode 7.2 解决方法
@Ashish Kakkad是当场的.这有效:
class Blah: NSURLSessionDelegate { func rest() { let path = "https://localhost:8443/greeting" let request = NSMutableURLRequest(URL: NSURL(string: path)!) let configuration = NSURLSessionConfiguration.defaultSessionConfiguration() let session = NSURLSession(configuration: configuration,delegate: self,delegateQueue:NSOperationQueue.mainQueue()) let task = session.dataTaskWithRequest(request,error -> Void in let json:JSON = JSON(data: data!) if let c = json["content"].string { print(c) } }) task.resume() } func URLSession(session: NSURLSession,task: NSURLSessionTask,didReceiveChallenge challenge: NSURLAuthenticationChallenge,completionHandler: (NSURLSessionAuthChallengeDisposition,NSURLCredential?) -> Void) { completionHandler(NSURLSessionAuthChallengeDisposition.UseCredential,NSURLCredential(forTrust: challenge.protectionSpace.serverTrust!)) } 在Info.plist文件中使用它: <key>NSAppTransportSecurity</key> <dict> <key>NSExceptionDomains</key> <dict> <key>localhost</key> <dict> <key>NSExceptionAllowsInsecureHTTPLoads</key> <true/> </dict> </dict> </dict> (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
相关内容