我是新來的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);
}
}
}
感謝您的幫助!我已經知道代碼的這個目的是一個真正的_no_ _go_,但是這個_app_只會被3到4個人使用。我只是一個試圖自學Swift的實習生,唯一我知道的是,我必須展示的這個網頁在SSL(或類似的東西)方面存在一些問題。這個網頁是由某人在這裏建立的,所以它不會做奇怪的事情。它只允許用戶登錄並點擊一個按鈕。所以我無法將其從HTTPS更改爲HTTP ...我將嘗試閱讀並理解您在評論中鏈接的文檔。但非常感謝,非常感謝! – Iman