Telegraph icon indicating copy to clipboard operation
Telegraph copied to clipboard

NSURLErrorDomain (code = -1202)

Open jxian725 opened this issue 3 years ago • 0 comments

Hi everyone, I am new to Telegraph. I got the error NSURLErrorDomain, code = -1202 when trying to run on HTTPS. I am using the ca.der and localhost.p12 provided in the sample. I tried with HTTP and it works. Not sure what is the problem with HTTPS, Any idea?

override func viewDidLoad() {
        super.viewDidLoad()
        view.addSubview(webView)
        loadCertificates()
        if let identity = identity, let caCertificate = caCertificate {
            server = Server(identity: identity, caCertificates: [caCertificate])
        } else {
            server = Server()
        }
        guard let url = URL(string: "https://localhost:3231/index.html") else {
            return
        }
        webView.uiDelegate = self
        let path = Bundle.main.url(forResource: "public", withExtension: nil)!
        server.serveDirectory(path)
        try! server.start(port: 3231, interface: "localhost")
        webView.load(URLRequest(url: url))
    }

private func loadCertificates() {
        if let identityURL = Bundle.main.url(forResource: "localhost", withExtension: "p12") {
            identity = CertificateIdentity(p12URL: identityURL, passphrase: "test")
        }
        if let caCertificateURL = Bundle.main.url(forResource: "ca", withExtension: "der") {
            caCertificate = Certificate(derURL: caCertificateURL)
        }
        if let caCertificate = caCertificate {
            tlsPolicy = TLSPolicy(commonName: "localhost", certificates: [caCertificate])
        }
    }

func webView(_ webView: WKWebView, didReceive challenge: URLAuthenticationChallenge, completionHandler: @escaping(URLSession.AuthChallengeDisposition, URLCredential?) -> Void) {
        if let serverTrust = challenge.protectionSpace.serverTrust {
            let credential = URLCredential(trust: serverTrust)
            completionHandler(.useCredential, credential)
        }else{
            completionHandler(.useCredential, nil)
        }
    }

plist.info

<key>NSAppTransportSecurity</key>
	<dict>
		<key>NSAllowsArbitraryLoads</key>
		<true/>
		<key>NSExceptionDomains</key>
		<dict>
			<key>localhost</key>
			<dict>
				<key>NSExceptionAllowsInsecureHTTPLoads</key>
				<true/>
				<key>NSIncludesSubdomains</key>
				<true/>
			</dict>
		</dict>
	</dict>

jxian725 avatar Jan 12 '22 12:01 jxian725