Experts, I'm a Beginner in IOS 9 / XCODE 7 / Swift 2 Development Kit
I'm trying to create an ios app that simply route to Web Application in HTTPS protocol. Below is my code so far in ViewController.swift
import UIKit
class ViewController: UIViewController {
@IBOutlet var myWebView: UIWebView!
/**
* Function to Display the Web Application initial URL
*/
func loadAppURL(){
let siteAddress = "https://domain:8443/path/to/page"
let url = NSURL (string: siteAddress)
let urlRequest = NSURLRequest(URL: url!)
myWebView.loadRequest(urlRequest)
}
override func viewDidLoad() {
super.viewDidLoad()
loadAppURL()
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
While Building my App it shows the below Error Message
2015-10-01 01:05:13.879 Web Page Tester[2947:31838] NSURLSession/NSURLConnection HTTP load failed (kCFStreamErrorDomainSSL, -9807)
and if i try building my app instead of "https://domain:8443/path/to/page" with "http://www.apple.com" its works fine.
I can access my web application in Safari and it asks for accepting the security risks. and i accept it and i can access my Application.
Guide me to fix my issues, Thanks in advance.
In Swift 3.
Step 1. Add NSURLConnectionDelegate
to your viewcontroller, to overwrite methods.
class ViewController: UIViewController, NSURLConnectionDelegate {
Step 2. Override viewDidLoad
override func viewDidLoad() {
super.viewDidLoad()
let siteAddress = "https://mysiteaddress"
let url = URL(string: siteAddress)
let urlRequest = URLRequest(url: url!)
let urlConnection:NSURLConnection = NSURLConnection(request: urlRequest, delegate: self)!
webView.loadRequest(urlRequest)
}
Step 3 Overwrite canAuthenticateAgainstProtectionSpace
and didReceive challenge
func connection(_ connection: NSURLConnection, canAuthenticateAgainstProtectionSpace protectionSpace: URLProtectionSpace) -> Bool {
print("\ncanAuthenticateAgainstProtectionSpace method Returning True\n")
return true
}
func connection(_ connection: NSURLConnection, didReceive challenge: URLAuthenticationChallenge) {
print("did autherntcationchallenge = \(challenge.protectionSpace.authenticationMethod)")
if challenge.protectionSpace.authenticationMethod == NSURLAuthenticationMethodServerTrust {
print("\nsend credential Server Trust\n")
let credential = URLCredential(trust: challenge.protectionSpace.serverTrust!)
challenge.sender!.use(credential, for: challenge)
}else if challenge.protectionSpace.authenticationMethod == NSURLAuthenticationMethodHTTPBasic{
print("send credential HTTP Basic")
let defaultCredentials: URLCredential = URLCredential(user: "user", password: "password", persistence:URLCredential.Persistence.forSession)
challenge.sender!.use(defaultCredentials, for: challenge)
}else if challenge.protectionSpace.authenticationMethod == NSURLAuthenticationMethodNTLM{
print("\nsend credential NTLM\n")
} else{
challenge.sender!.performDefaultHandling!(for: challenge)
}
}
Thanks Navas Basheer for the original solution! Saved me a ton of time
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With