如何在WKWebView中轻松处理ssl证书

时间:2018-04-10 07:14:51

标签: ios ssl swift4 wkwebview

我正在开发一款只展示网站的移动应用。 这不是一个大项目,它只是为了展示我瞄准的网站如何在这样的应用程序中呈现。

我已经设法在Android中使用WebView对象,但我必须处理ssl certificats和Timeout异常。

既然这个应用程序有效,我必须在IOS上做同样的事情......但我对Apple品牌一无所知。 我已经“学习”了这个主题并设法在我的应用程序中显示一个网站,但现在我遇到了与android相同的问题:处理ssl certificat(也许是TimeoutException)。

我在这里关注了几个问题,尤其是this one,但应用程序会永远加载页面。 (适用于https://www.google.com/ 我得到的唯一消息就是这个:

  

2018-04-09 17:32:13.252805 + 0200 myproject [64445:1704413] [MC]延迟加载NSBundle MobileCoreServices.framework

     

2018-04-09 17:32:13.254114 + 0200 myproject [64445:1704413] [MC]已加载MobileCoreServices.framework

我正在使用Xcode,它似乎是Swift的4.0.3版本

这是我的代码:

import UIKit
import WebKit

class ViewController: UIViewController, WKUIDelegate {

@IBOutlet var webView: WKWebView!

override func loadView() {
    let webConfiguration = WKWebViewConfiguration()

    webView = WKWebView(frame: .zero, configuration:webConfiguration)
    webView.uiDelegate=self
    view=webView
}

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

override func viewDidLoad() {
    super.viewDidLoad()
    // Do any additional setup after loading the view, typically from a nib.

    print("Chargement des données ...")
    let url = URL(string:"https://myWebSite.fr/")
    //let url = URL(string:"https://www.google.com")

    let request = URLRequest(url: url!)
    webView.load(request)

}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
}

override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?){
    self.view.endEditing(true)
}

}

希望它不会被视为我错过的另一个问题的副本。 提前谢谢。

0 个答案:

没有答案