私はSwiftとNSURLConnectionを初めて利用しました。& NSURLSessionです。私はこのコードを持っており、ウェブページを読み込むとこれが動作します。しかし、NSURLConnectionはiOS 9.0では非推奨となっています。NSURLSessionを使用する必要があります。NSURLSessionへのNSURLConnection
これは私のコードです:
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人だけで使用されます。私は自分でスイフトを学ぼうとしているインターンだけです。私が知っている唯一のことは、このウェブページにSSL(またはそのようなもの)に関するいくつかの問題があることです。このウェブページはここの誰かによって作られたので、奇妙なことはしません。ユーザーはログインしてボタンをクリックするだけです。だから私はHTTPSからHTTPに変更することはできません...私はあなたのコメントにリンクしている文書を読んで理解しようとします。しかし、ありがとう、それは非常に感謝しています! – Iman