2016-04-29 73 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

相關問題