swift 3 - http authentication in WKWebView

2019-07-15 05:07发布

I'm trying to build a simple WebView which shows a web page - the page requires http authentication for all pages (for testing purposes).

Here is my code:

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)
    }

}

I've found willSendRequestForAuthenticationChallenge and didReceiveAuthenticationChallenge, but none of them is called and I've got error from the server that I was not authenticated.

Could somebody help?

Thank you a lot!

David

2条回答
仙女界的扛把子
2楼-- · 2019-07-15 05:52

Fixed variant #1 by adding "_":

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)
        challenge.sender?.use(credential, for: challenge)
        completionHandler(URLSession.AuthChallengeDisposition.useCredential, credential)
}
查看更多
不美不萌又怎样
3楼-- · 2019-07-15 06:01

It works by removing (or comment out) this line. challenge.sender?.use(credential, for: challenge) I have checked it also in other iOS versions 9.X, 10.1, 10.2 and 10.3. It's working fine.

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)

}

查看更多
登录 后发表回答