0

I need to load load self-signed ssl web page on ios using WKWebView I have follwing code, but the page not loading when I open the application. I have tried with non-http url and works fine.

class ShowCloudWebViewController: UIViewController  , WKUIDelegate, NSURLConnectionDelegate{

    @IBOutlet weak var webViewParent: UIView!
    var webView: WKWebView!
    
    @IBOutlet weak var progressView: UIProgressView!
    override func loadView() {
    super.loadView()
    let webConfiguration = WKWebViewConfiguration()
    webView = WKWebView(frame: .zero, configuration: webConfiguration)  
    webView.uiDelegate = self
    view = webView

    
}
override func viewDidLoad() {
    super.viewDidLoad()
    
  
    
    var myURL = NSURL(string: "https://mysite.ddns.net:8076/index.php")
    let myRequest = NSURLRequest(url: myURL! as URL)
    let urlConnection:NSURLConnection = NSURLConnection(request: myRequest as URLRequest, delegate: self)!
    webView.load(myRequest as URLRequest)
}
    
   
    
    override func observeValue(forKeyPath keyPath: String?, of object: Any?, change: [NSKeyValueChangeKey : Any]?, context: UnsafeMutableRawPointer?) {
        if keyPath == "estimatedProgress" {
            progressView.progress = Float(webView.estimatedProgress)
        }
    }
    
    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)
        }

    }
    
    deinit {
       // webView!.removeObserver(self, forKeyPath: "estimatedProgress")
    }
}
CodeDezk
  • 1,230
  • 1
  • 12
  • 40

1 Answers1

0

Add this to your plist file

<key>NSAppTransportSecurity</key>
<dict>
    <key>NSAllowsArbitraryLoads</key>
    <true/>
</dict>

Source from this solution

jdanvz
  • 44
  • 1
  • 8