swift 3-WKWebView中的http身份验证

swift 3-WKWebView中的http身份验证,swift,swift3,wkwebview,http-authentication,Swift,Swift3,Wkwebview,Http Authentication,我正在尝试构建一个简单的WebView,它显示一个web页面——该页面需要对所有页面进行http身份验证(用于测试目的) 这是我的密码: class ViewController: UIViewController, WKUIDelegate { var webView: WKWebView! override func loadView() { let webConfiguration = WKWebViewConfiguration() w

我正在尝试构建一个简单的WebView,它显示一个web页面——该页面需要对所有页面进行http身份验证(用于测试目的)

这是我的密码:

class ViewController: UIViewController, WKUIDelegate {

    var webView: WKWebView!

    override func loadView() {
        let webConfiguration = WKWebViewConfiguration()
        webView = WKWebView(frame: .zero, configuration: webConfiguration)
        webView.uiDelegate = self
        view = webView
    }

    // #1 variant
    func webView(webView: WKWebView, willSendRequestForAuthenticationChallenge challenge:
        URLAuthenticationChallenge, completionHandler: (URLSession.AuthChallengeDisposition, URLCredential?) -> Void) {
        let user = "user"
        let password = "pass"
        let credential = URLCredential(user: user, password: password, persistence: URLCredential.Persistence.forSession)
        challenge.sender?.use(credential, for: challenge)
    }

    // #2 variant
    func webView(webView: WKWebView, didReceiveAuthenticationChallenge challenge: URLAuthenticationChallenge, completionHandler: (URLSession.AuthChallengeDisposition, URLCredential?) -> Void) {

            let user = "user"
            let password = "pass"
            let credential = URLCredential(user: user, password: password, persistence: URLCredential.Persistence.forSession)
            challenge.sender?.use(credential, for: challenge)

    }

    override func viewDidLoad() {
        super.viewDidLoad()

        let myURL = URL(string: "https://myurl.com")
        let myRequest = URLRequest(url: myURL!)
        webView.load(myRequest)
    }

}
我找到了willSendRequestForAuthenticationChallenge和didReceiveAuthenticationChallenge,但没有调用任何一个,并且我从服务器上得到了一个错误,即我没有经过身份验证

有人能帮忙吗

多谢各位

David

通过添加“#”修正了变体#1:

它通过删除(或注释掉)这一行来工作。
challenge.sender?使用(凭证,用于:challenge)
我也在其他iOS版本9.X、10.1、10.2和10.3中检查过它。 很好用

func webView(_ webView: WKWebView, didReceive challenge: URLAuthenticationChallenge, completionHandler: @escaping (URLSession.AuthChallengeDisposition, URLCredential?) -> Void) {
    let user = "user"
    let password = "pass"
    let credential = URLCredential(user: user, password: password, persistence: URLCredential.Persistence.forSession)
    completionHandler(URLSession.AuthChallengeDisposition.useCredential, credential)

}

通过添加“”:func webView(webView:WKWebView,didReceive challenge:urlAuthenticationChallengeChallenge,completionHandler:@escaping(URLSession.AuthChallengeDisposition,URLCredential?)->Void)修复了变量#1{let user=“user”let password=“pass”let credential=URLCredential(用户:用户,密码:密码,持久性:URLCredential.persistence.forSession)challenge.sender?.use(凭证,for:challenge)completionHandler(URLSession.AuthChallengeDisposition.UseCdential,credential)}您想使用WKUIDelegate吗?我用WKNavigationDelegate扩展了我的视图控制器类(设置webView.navigationDelegate=self而不是webView.uiDelegate=self)并实现以下功能:func-webView(uwebview:WKWebView,didReceive-challenge:URLAuthenticationChallenge,completionHandler:@escaping(URLSession.AuthChallengeDisposition,URLCredential?)->->Void){是的,我已在下面共享了答案测试,直到我将WKUIDelegate更改为WKNavigationDelegate,并将webView.uiDelegate=self更改为webView.navigationDelegate=self,我的完成处理程序才被调用
func webView(_ webView: WKWebView, didReceive challenge: URLAuthenticationChallenge, completionHandler: @escaping (URLSession.AuthChallengeDisposition, URLCredential?) -> Void) {
    let user = "user"
    let password = "pass"
    let credential = URLCredential(user: user, password: password, persistence: URLCredential.Persistence.forSession)
    completionHandler(URLSession.AuthChallengeDisposition.useCredential, credential)