2016-04-29 71 views
0

我是新来的Swift和NSURLConnection & NSURLSession。我有这个代码,加载一个网页,这个工程。但是我得到了这个警告,NSURLConnection在iOS 9.0中被弃用,我必须使用NSURLSession。NSURLConnection 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的HTTP加载失败(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); 

     } 
    } 
} 

回答

0

你不使用NSURLSession。可能性是,NSURLConnection将处于半支持状态,直到时间结束,因为它的使用范围很广。

就这样说,我只会说一次,所以仔细听。在任何情况下,不要公开发布任何版本的此代码。自签名证书是可以的,只要你正确地检查它们。此代码没有这样做,这使得它们没有比HTTP更好。

  • 如果数据必须保密,可以使用真实证书或添加代码来正确验证自签名证书。
  • 如果保密并不重要,只需使用HTTP即可。

本文档介绍了如何正确通过增加信任特定证书实现修改TLS链验证:

https://developer.apple.com/library/mac/documentation/NetworkingInternet/Conceptual/NetworkingTopics/Articles/OverridingSSLChainValidationCorrectly.html

顺便说一句,你没有做任何事情,在其他保护空间或挑战其他主机名称。如果你永远不会调用完成处理程序,那么任何请求任何其他类型的认证的任务都会永远停留在一边,等待你决定如何处理认证请求。这可能不是你想要的,尽管我怀疑它是否会导致你的问题,除非你在代理之后。您的NSURLConnection代码接受单个保护空间内的挑战,因此它可能不会遇到这个问题(尽可能多)。

这样说的话,我不明白为什么这个错误代码会失败,除非除了自签名之外还有其他问题。哦,方法声明中缺少下划线。我不认为这很重要,但我不确定。确保你的委托方法实际上被调用。

您也可以尝试将CFNETWORK_DIAGNOSTICS环境变量设置为1(或更多),然后查看它是否提供了进一步的见解。

+0

感谢您的帮助!我已经知道代码的这个目的是一个真正的_no_ _go_,但是这个_app_只会被3到4个人使用。我只是一个试图自学Swift的实习生,唯一我知道的是,我必须展示的这个网页在SSL(或类似的东西)方面存在一些问题。这个网页是由某人在这里建立的,所以它不会做奇怪的事情。它只允许用户登录并点击一个按钮。所以我无法将其从HTTPS更改为HTTP ...我将尝试阅读并理解您在评论中链接的文档。但非常感谢,非常感谢! – Iman

相关问题