Hizmetimi sınamak için kullandığım Sanal Makinede kendim imzalı bir sertifikam var. UIWebView to view self signed websites (No private api, not NSURLConnection) - is it possible? içinde bulunan cevapları kullanarak hızlı 2.0 kodu işleyebildi. Xcode 7, NSURLConnection
'un kullanımdan kaldırıldığını ve bunun yerine NSURLSession
kullanmam gerektiğini söyledi. Bu kodu taşıma girişimlerimin hiçbiri başarılı olmadı ve diğer yanıtlarda açıklanan normal dönüşüm senaryolarının hiçbiri geçerli değil.NSURLSession'a dönüştürülebilen kendi imzalı sertifikaları işlemek için bu özel NSURLConnection kullanımı mıdır?
Kimlik doğrulama sorunumun delege yöntemleriyle işlenmesi için yeni bir NSURLSession
oluşturuyorsam, diğer yükler hala sharedSession
üzerinde gerçekleşir ve bu nedenle başarısız olur.
var authRequest : NSURLRequest? = nil
var authenticated = false
var trustedDomains = [:] // set up as necessary
func webView(webView: UIWebView, shouldStartLoadWithRequest request: NSURLRequest, navigationType: UIWebViewNavigationType) -> Bool {
if !authenticated {
authRequest = request
let urlConnection: NSURLConnection = NSURLConnection(request: request, delegate: self)!
urlConnection.start()
return false
}
else if isWebContent(request.URL!) { // write your method for this
return true
}
return processData(request) // write your method for this
}
func connection(connection: NSURLConnection, willSendRequestForAuthenticationChallenge challenge: NSURLAuthenticationChallenge) {
if challenge.protectionSpace.authenticationMethod == NSURLAuthenticationMethodServerTrust {
let challengeHost = challenge.protectionSpace.host
if let _ = trustedDomains[challengeHost] {
challenge.sender!.useCredential(NSURLCredential(forTrust: challenge.protectionSpace.serverTrust!), forAuthenticationChallenge: challenge)
}
}
challenge.sender!.continueWithoutCredentialForAuthenticationChallenge(challenge)
}
func connection(connection: NSURLConnection, didReceiveResponse response: NSURLResponse) {
authenticated = true
connection.cancel()
webview!.loadRequest(authRequest!)
}