Untrusted SSL certificate open in webView IOS
Posted By : Nitin Bhatt | 30-Mar-2016
Here I will explain in detail with some steps :
Open XCode , Select Single View Controller (File->New->Single View Controller->Next)
Enter Product Name and Organization Identifier, make sure Swift is selected as Language and iPhone is selected as Device. If you want to use database then check the Use Core data checkbox otherwise remain it unchecked.
Open Main.stroyboard , then drag and drop webView on view controller after that create the outlet of webView and set webView delegate.
Write this code in OpenUntrustedUrlInWebView.swift.
import UIKit
class OpenUntrustedUrlInWebView: UIViewController,UIWebViewDelegate,NSURLConnectionDelegate{
@IBOutlet weak var webView: UIWebView!
override func viewDidLoad() {
super.viewDidLoad()
let siteAddress = "https://192.168.2.99:8443/"
let url = NSURL (string: siteAddress)
let urlRequest = NSMutableURLRequest(URL: url!)
let urlConnection:NSURLConnection = NSURLConnection(request: urlRequest, delegate: self)!
webView.loadRequest(urlRequest)
webView.delegate = self
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
override func viewWillAppear(animated: Bool) {
}
func connection(connection: NSURLConnection, canAuthenticateAgainstProtectionSpace protectionSpace: NSURLProtectionSpace) -> Bool{
return true
}
func connection(connection: NSURLConnection, didReceiveAuthenticationChallenge challenge: NSURLAuthenticationChallenge){
if challenge.protectionSpace.authenticationMethod == NSURLAuthenticationMethodServerTrust {
let credential = NSURLCredential(forTrust: challenge.protectionSpace.serverTrust!)
challenge.sender!.useCredential(credential, forAuthenticationChallenge: challenge)
}else if challenge.protectionSpace.authenticationMethod == NSURLAuthenticationMethodHTTPBasic{
let defaultCredentials: NSURLCredential = NSURLCredential(user: "username", password: "password", persistence:NSURLCredentialPersistence.ForSession)
challenge.sender!.useCredential(defaultCredentials, forAuthenticationChallenge: challenge)
}else if challenge.protectionSpace.authenticationMethod == NSURLAuthenticationMethodNTLM{
} else{
challenge.sender!.performDefaultHandlingForAuthenticationChallenge!(challenge)
}
}
}
THANKS
Cookies are important to the proper functioning of a site. To improve your experience, we use cookies to remember log-in details and provide secure log-in, collect statistics to optimize site functionality, and deliver content tailored to your interests. Click Agree and Proceed to accept cookies and go directly to the site or click on View Cookie Settings to see detailed descriptions of the types of cookies and choose whether to accept certain cookies while on the site.
About Author
Nitin Bhatt
Nitin is an Assistant Project Manager specializing in iOS Application Development. He is an avid reader.