I have https url with self-sign certication in UIWEBView ,but it does not work.
Error is NSURLConnection/CFURLConnection HTTP load failed (kCFStreamErrorDomainSSL, -9813)...
how can I to allow any certication in uiwebview by swift
Do you have any idea that fix this problem?
i find a way to do this,but i think this is not a good way.
step 1------use NSURLConnection in override func viewDidLoad(){} section request = NSURLRequest(URL:url)
let urlConnection:NSURLConnection = NSURLConnection(request: request, delegate: self)!
step 2------ use the NSURLConnection delegate to
func connection(connection: NSURLConnection, didFailWithError error: NSError){
println("didFailWithError")
}
func connection(connection: NSURLConnection, canAuthenticateAgainstProtectionSpace protectionSpace: NSURLProtectionSpace) -> Bool{
println("canAuthenticateAgainstProtectionSpace")
//return [protectionSpace.authenticationMethodisEqualToString:NSURLAuthenticationMethodServerTrust];
return true
}
func connection(connection: NSURLConnection, didReceiveAuthenticationChallenge challenge: NSURLAuthenticationChallenge){
println("did autherntcationchallenge = \(challenge.protectionSpace.authenticationMethod)")
//if challenge.protectionSpace.authenticationMethod == NSURLAuthenticationMethodServerTrust && challenge.protectionSpace.host == "myDomain.com" {
if challenge.protectionSpace.authenticationMethod == NSURLAuthenticationMethodServerTrust {
println("send credential Server Trust")
let credential = NSURLCredential(forTrust: challenge.protectionSpace.serverTrust)
challenge.sender.useCredential(credential, forAuthenticationChallenge: challenge)
}else if challenge.protectionSpace.authenticationMethod == NSURLAuthenticationMethodHTTPBasic{
println("send credential HTTP Basic")
var defaultCredentials: NSURLCredential = NSURLCredential(user: "username", password: "password", persistence:NSURLCredentialPersistence.ForSession)
challenge.sender.useCredential(defaultCredentials, forAuthenticationChallenge: challenge)
}else if challenge.protectionSpace.authenticationMethod == NSURLAuthenticationMethodNTLM{
println("send credential NTLM")
if challenge.previousFailureCount > 0 {
//如果连续两次验证未通过,则终止, 还需要返回自定义出错界面
//handle bad credentials scenario
}
var defaultCredentials: NSURLCredential = NSURLCredential(user: "username", password: "password", persistence:NSURLCredentialPersistence.ForSession)
challenge.sender.useCredential(defaultCredentials, forAuthenticationChallenge: challenge)
}
} else{
challenge.sender.performDefaultHandlingForAuthenticationChallenge!(challenge)
}
//challenge.sender.performDefaultHandlingForAuthenticationChallenge!(challenge)
//challenge.sender.continueWithoutCredentialForAuthenticationChallenge(challenge)
}
/*
func connection(connection: NSURLConnection, willSendRequestForAuthenticationChallenge challenge: NSURLAuthenticationChallenge) {
}*/
func connection(connection: NSURLConnection, didCancelAuthenticationChallenge challenge: NSURLAuthenticationChallenge){
println("didCancelAuthenticationChallenge")
}
/*
- (void)connection: (NSURLConnection *)connection willSendRequestForAuthenticationChallenge: (NSURLAuthenticationChallenge *)challenge
{
[challenge.sender useCredential:[NSURLCredential credentialForTrust:challenge.protectionSpace.serverTrust] forAuthenticationChallenge:challenge];
}*/
func connection(connection: NSURLConnection, didReceiveResponse response: NSURLResponse){
println("-----received response");
// remake a webview call now that authentication has passed ok.
//_authenticated =YES;
//[_webloadRequest:_request];
webView.loadRequest(request)
// Cancel the URL connection otherwise we double up (webview + url connection, same url = no good!)
//[_urlConnectioncancel];
}
it can work
sorry for my poor english skill