2016-10-02 11 views
1

IOS 10の自己割り当て証明書を使用してHTTPS URLをロードしようとして失敗しています。自己割り当て証明書を使用するIOS wkwebview

はこのスレッドで同様の問題を見つけましたが、解決策は、スウィフト/ SDK/IOSの新しいバージョンには適していません:Allow unverified ssl certificates in WKWebView

私はApp Storeのために有効な解決策を捜しているわけではない、単にする必要がありSSL証明書の検証を無視することができます。

import UIKit 
import WebKit 

class ViewController: UIViewController, WKNavigationDelegate { 

    var webView: WKWebView! 

    override func loadView() { 
     webView = WKWebView() 
     webView.navigationDelegate = self 

     view = webView 
    } 


    override func viewDidLoad() { 
     super.viewDidLoad() 
     // Do any additional setup after loading the view, typically from a nib. 
     let url = URL(string: "https://X.X.X.X:XXXX")! 
     //let url = URL(string: "https://google.com")! 
     webView.load(URLRequest(url: url)) 
     webView.allowsBackForwardNavigationGestures = true 
    } 

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


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


} 

答えて

1

あなたのデリゲートメソッドのシグネチャは非常ない試合を行います。ここでは

はViewController.swiftファイルです。試してみてください:

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

試してみましたが、https://google.comで正常に動作しますが、自己割り当てURLは使用できません。このエラーが発生しました: '致命的なエラー:オプション値をアンラッピングしている間に予期せずnilが見つかりました'。 –

+0

問題が見つかりました: "challenge.protectionSpace.serverTrust"はゼロで、動作するようにしました。ありがとう –

関連する問題