Authentication with WKWebView in Swift

前端 未结 3 1977
长发绾君心
长发绾君心 2020-12-06 09:53

In my iOS app, I would like to use a WKWebView to wrap an external URL in the application. This URL requires basic authentication (it needs user and password credential, lik

相关标签:
3条回答
  • 2020-12-06 10:26

    i am late to party but still this can be useful for someone.

    To support auth challenge in WKWebview Swift 4, complete code as below

    func webView(_ webView: WKWebView, didReceive challenge: URLAuthenticationChallenge, completionHandler: @escaping (URLSession.AuthChallengeDisposition, URLCredential?) -> Void) {
            guard let hostname = webView.url?.host else {
                return
            }
    
            let authenticationMethod = challenge.protectionSpace.authenticationMethod
            if authenticationMethod == NSURLAuthenticationMethodDefault || authenticationMethod == NSURLAuthenticationMethodHTTPBasic || authenticationMethod == NSURLAuthenticationMethodHTTPDigest {
                let av = UIAlertController(title: webView.title, message: String(format: "AUTH_CHALLENGE_REQUIRE_PASSWORD".localized, hostname), preferredStyle: .alert)
                av.addTextField(configurationHandler: { (textField) in
                    textField.placeholder = "USER_ID".localized
                })
                av.addTextField(configurationHandler: { (textField) in
                    textField.placeholder = "PASSWORD".localized
                    textField.isSecureTextEntry = true
                })
    
                av.addAction(UIAlertAction(title: "BUTTON_OK".localized, style: .default, handler: { (action) in
                    guard let userId = av.textFields?.first?.text else{
                        return
                    }
                    guard let password = av.textFields?.last?.text else {
                        return
                    }
                    let credential = URLCredential(user: userId, password: password, persistence: .none)
                    completionHandler(.useCredential,credential)
                }))
                av.addAction(UIAlertAction(title: "BUTTON_CANCEL".localized, style: .cancel, handler: { _ in
                    completionHandler(.cancelAuthenticationChallenge, nil);
                }))
                self.parentViewController?.present(av, animated: true, completion: nil)
            }else if authenticationMethod == NSURLAuthenticationMethodServerTrust{
                // needs this handling on iOS 9
                completionHandler(.performDefaultHandling, nil);
            }else{
                completionHandler(.cancelAuthenticationChallenge, nil);
            }
        }
    
    0 讨论(0)
  • 2020-12-06 10:36

    Add the following line:

    completionHandler(NSURLSessionAuthChallengeDisposition.UseCredential, credential)
    

    at the end of didReceiveAuthenticationChallenge solved the problem.

    0 讨论(0)
  • 2020-12-06 10:38

    In a simpler way:

    Though this answer looks redundant but posting as this may help other naive developers (like me).

       func webView(_ webView: WKWebView, didReceive challenge: URLAuthenticationChallenge, completionHandler: @escaping (URLSession.AuthChallengeDisposition, URLCredential?) -> Void) {
            guard (webView.url?.host) != nil else {
                return
            }
            let authenticationMethod = challenge.protectionSpace.authenticationMethod
            if authenticationMethod == NSURLAuthenticationMethodDefault || authenticationMethod == NSURLAuthenticationMethodHTTPBasic || authenticationMethod == NSURLAuthenticationMethodHTTPDigest {
                let credential = URLCredential(user: userName, password: password, persistence: .none)
                completionHandler(.useCredential, credential)
            } else if authenticationMethod == NSURLAuthenticationMethodServerTrust {
                completionHandler(.performDefaultHandling, nil)
            } else {
                completionHandler(.cancelAuthenticationChallenge, nil)
            }
        }
    
    0 讨论(0)
提交回复
热议问题