我是斯威夫特和NSURLConnection & NSURLSession的新手。我有这个代码,来加载一个网页,这是可行的。但是我得到了这样的警告: NSURLConnection在iOS 9.0中被否决了,我必须使用NSURLSession。
这是我的密码:
var authenticated:Bool = false
var urlConnection:NSURLConnection!
func webView(webView: UIWebView, shouldStartLoadWithRequest request: NSURLRequest, navigationType: UIWebViewNavigationType) -> Bool {
if !authenticated {
self.authenticated = false
self.urlConnection = NSURLConnection(request: request, delegate: self)!
urlConnection.start()
return false
}
return true
}
// We use this method is to accept an untrusted site which unfortunately we need to do, as our PVM servers are self signed.
func connection(connection: NSURLConnection, canAuthenticateAgainstProtectionSpace protectionSpace: NSURLProtectionSpace) -> Bool {
return (protectionSpace.authenticationMethod == NSURLAuthenticationMethodServerTrust)
}
func connection(connection: NSURLConnection, didReceiveAuthenticationChallenge challenge: NSURLAuthenticationChallenge) {
if challenge.previousFailureCount == 0 {
self.authenticated = true
let credential: NSURLCredential = NSURLCredential(forTrust: challenge.protectionSpace.serverTrust!
)
challenge.sender!.useCredential(credential, forAuthenticationChallenge: challenge)
}
else {
challenge.sender!.cancelAuthenticationChallenge(challenge)
}
}
func connection(connection: NSURLConnection, didReceiveResponse response: NSURLResponse) {
// remake a webview call now that authentication has passed ok.
self.authenticated = true
web.loadRequest(request)
// Cancel the URL connection otherwise we double up (webview + url connection, same url = no good!)
urlConnection.cancel()
}这个很管用。现在我想将它“转换”为NSURLSession,但我似乎无法管理。有人能帮我吗?我敢肯定,对于一个能很好地编码的人来说,这并不难。
我曾多次尝试更改为NSURLSession,但每次都会出现以下错误: NSURLSession/NSURLConnection失败(kCFStreamErrorDomainSSL,-9813)。用NSURLConnection解决了这个问题。
这是我使用NSURLSession时所做的尝试之一:
var authenticated:Bool = false
var urlSession:NSURLSession!
var urlSessionConfig:NSURLSessionConfiguration!
func webView(webView: UIWebView, shouldStartLoadWithRequest request: NSURLRequest, navigationType: UIWebViewNavigationType) -> Bool {
if !authenticated {
self.authenticated = false
self.urlSessionConfig = NSURLSessionConfiguration.defaultSessionConfiguration()
self.urlSession = NSURLSession(configuration: self.urlSessionConfig, delegate: self, delegateQueue:NSOperationQueue.mainQueue())
let task = self.urlSession.dataTaskWithRequest(request){
(myData, myResponse, myError) -> Void in
if(myError == nil){
if(self.authenticated){
self.web.loadRequest(request)
}
}
}
task.resume()
return false
}
return true
}
func URLSession(session: NSURLSession, didReceiveChallenge challenge: NSURLAuthenticationChallenge, completionHandler: (NSURLSessionAuthChallengeDisposition, NSURLCredential?) -> Void) {
if(challenge.protectionSpace.authenticationMethod == NSURLAuthenticationMethodServerTrust){
if(challenge.protectionSpace.host == "mydomain.org"){
self.authenticated = true
let credential = NSURLCredential(forTrust: challenge.protectionSpace.serverTrust!)
completionHandler(NSURLSessionAuthChallengeDisposition.UseCredential,credential);
}
}
}发布于 2016-05-01 06:05:39
没有可以使用NSURLSession。考虑到NSURLConnection的使用范围,很有可能,考虑到它的使用范围,它将一直处于半支持状态,直到时间结束。
说到这里,我只会说一次,所以仔细听。在任何情况下,都不要按编写的方式公开发布这两个版本的代码。只要你检查正确,自我签名的证书就可以了。这段代码并没有做到这一点,这使得它们的并不比HTTP更好。
本文档解释了如何通过添加对特定证书的信任来正确实现修改后的TLS链验证:
顺便说一句,在其他保护空间或其他主机名中,您没有遇到任何挑战。如果您从未调用完成处理程序,那么请求任何其他类型身份验证的任务将永远停留在模糊状态,等待您决定如何处理身份验证请求。这可能不是您想要的,虽然我怀疑它会导致您的问题,除非您是在代理后面。您的NSURLConnection代码接受了单个保护空间内的挑战,因此它可能没有遇到这个问题(很多)。
尽管如此,我不明白为什么这个错误代码会失败,除非证书除了自我签名之外还有其他问题。哦,方法声明中缺少一个下划线。我觉得这不再重要了,但我不确定。确保您的委托方法实际上正在被调用。
您还可以尝试将CFNETWORK_DIAGNOSTICS环境变量设置为1(或更多),看看这是否提供了进一步的洞察力。
https://stackoverflow.com/questions/36935686
复制相似问题