iOS8 系統(tǒng)上WKWebView https 證書不驗證問題

來這里,了解很多問題,發(fā)掘更多。
?????? 傳送門->Have-a-problem??????

??????:以下內(nèi)容來自于傳送門鏈接??????

詳細過程:

WKWebView在iOS8上加載https(無效證書)出錯,通過各種資料查詢是WKWebView沒有走證書處理的方法(下面會給出),更何況證書不符合apple的要求。

Log輸出:

NSURLConnection/CFURLConnection HTTP load failed (kCFStreamErrorDomainSSL, -9814)

https關(guān)于服務(wù)器支持要求:

  • 服務(wù)器必須支持傳輸層安全(TLS)協(xié)議1.2以上版本
  • 證書必須使用SHA256或更高的哈希算法簽名
  • 必須使用2048位以上RSA密鑰或256位以上ECC算法等

?? 檢測你的地址是否支持ATS->??????傳送門??????

我目前的處理

  • iOS8及以下系統(tǒng) 使用UIWebView
  • iOS9及以上系統(tǒng) 使用WKWebView

需要注意

iOS8及以下系統(tǒng),使用UIWebView的時候,任然需要忽略證書(其實這個取決于你們服務(wù)端愿不愿去支持ATS),如果不愿意或者說你跳的第三方根本就無法修改,那還是老老實實的忽略吧,不管那么多了,直接忽略??????

放大招(示例代碼)

控件及變量

// UIWebView 
var webViewIOS8: UIWebView?
var requestIOS8: URLRequest?
// WKWebView
var webViewIOS9: WKWebView?
// Other
var authed: Bool = false
var urlConnection: NSURLConnection?

根據(jù)系統(tǒng)版本選擇控件

if #available(iOS 9.0, *) {
    let request: URLRequest = URLRequest.init(url: URL.init(string: "鏈接地址")!)
    view.addSubview(webViewIOS9)
    webViewIOS9.load(request)
}else{
    requestIOS8 = URLRequest.init(url: URL.init(string: "鏈接地址")!)
    view.addSubview(webViewIOS8)
    webViewIOS8.loadRequest(requestIOS8!)
}

iOS8及以上忽略證書

func webView(_ webView: UIWebView, shouldStartLoadWith request: URLRequest, navigationType: UIWebViewNavigationType) -> Bool {
    // 重新賦值requestIOS8
    requestIOS8 = request
    // 判斷是否https
    if (requestIOS8?.url?.absoluteString.hasPrefix("https"))! && !authenticated{
        authenticated = false
        // 使用NSURLConnection
        urlConnection = NSURLConnection.init(request: request, delegate: self)
        // 開始
        urlConnection?.start()
        return false
    }
    return true
}

func webViewDidFinishLoad(_ webView: UIWebView) {
    // 控制使用NSURLConnection
    authenticated = (requestIOS8?.url?.absoluteString.hasPrefix("https"))!
}

func webView(_ webView: UIWebView, didFailLoadWithError error: Error) {
    // 錯誤打印    
}

// MARK: NSURLConnectionDelegate
// 我對這部分的理解就是不去驗證證書的真?zhèn)?,直接信任服?wù)端
func connection(_ connection: NSURLConnection, didReceive challenge: URLAuthenticationChallenge) {
    // 創(chuàng)建憑據(jù)對象
    let cred = URLCredential(trust: challenge.protectionSpace.serverTrust!)
    // 告訴服務(wù)器信任證書
    challenge.sender?.use(cred, for: challenge)
}

func connection(_ connection: NSURLConnection, didReceive response: URLResponse) {
    // 控制使用NSURLConnection
    authenticated = true
    // 重新加載
    webViewIOS8.loadRequest(requestIOS8!)
    // 取消
    urlConnection?.cancel()
}

func connection(_ connection: NSURLConnection, canAuthenticateAgainstProtectionSpace protectionSpace: URLProtectionSpace) -> Bool {
    return true
}

iOS9及以上忽略證書

func webView(_ webView: WKWebView, didReceive challenge: URLAuthenticationChallenge, completionHandler: @escaping (URLSession.AuthChallengeDisposition, URLCredential?) -> Void) {
    // 創(chuàng)建憑據(jù)對象
    let cred = URLCredential(trust: challenge.protectionSpace.serverTrust!)
    // 告訴服務(wù)器信任證書
    completionHandler(.useCredential, cred)
}

注意

以上內(nèi)容為個人整理,如果有問題有出入或者你有更好的解決方法,還請賜教哦,感謝。
我的郵箱 coderjianfeng@foxmail.com ?????? github傳送門

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容