我正在尝试从UIWebView连接到使用Swift 3通过https使用自签名证书的服务器.我已经在我的iOS设备上安装了证书,我可以使用iOS上的Safari连接到服务器,所以问题出在应用程序.
我发现的最佳方法是使用Objective C描述的:
UIWebView查看自签名网站(没有私人API,不是NSURLConnection) - 有可能吗?
我试着把它翻译成Swift 3如下:
override func viewDidLoad() {
super.viewDidLoad()
webView.delegate = self
// other stuff
}
func webView(_ webView: UIWebView, shouldStartLoadWith request: URLRequest, navigationType: UIWebViewNavigationType) -> Bool {
print("HTTPSSETUP should start load with request: authenticated \(authenticated)")
if(!authenticated) {
print("HTTPSSETUP not yet authenticated - trying")
globalRequest = request
let conn: NSURLConnection = NSURLConnection(request: request, delegate: self)!
conn.start()
return false
}
// some other stuff
return true
}
func connection(_ connection: NSURLConnection, willSendRequestFor challenge: URLAuthenticationChallenge) {
print("HTTPSSETUP …Run Code Online (Sandbox Code Playgroud)