2016-12-04 116 views
1

我想构建一个简单的WebView,它显示一个网页 - 页面需要所有页面的http认证(用于测试目的)。swift 3 - WKWebView中的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,但没有人叫,我已经从我未通过身份验证服务器遇到错误。

有人可以帮忙吗?

非常感谢!

大卫

+0

固定变种#1加 “_”: FUNC web视图(_ web视图: WKWebView,didReceive挑战:URLAuthenticationChallenge,completionHandler:@escaping(URLSession.AuthChallengeDisposition,URLCredential?) - > Void){ let user =“user” let password =“pass” 令凭证= URLCredential(用户:用户名,密码:密码,持久性:URLCredential.Persistence.forSession) challenge.sender。使用(证书号为:挑战)? completionHandler(URLSession.AuthChallengeDisposition.useCredential,证书) } – David

+0

你的意思是使用WKUIDelegate吗?我使用WKNavigationDelegate扩展了我的视图控制器类(设置webView.navigationDelegate = self而不是webView.uiDelegate = self),并实现了以下功能:func webView(_ webView:WKWebView,didReceive challenge:URLAuthenticationChallenge,completionHandler:@escaping(URLSession.AuthChallengeDisposition ,URLCredential?) - > Void){ – CScott

+0

是的,我分享了已经在 – David

回答

1

固定变种#1加 “_”:

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

它可以通过删除(或注释掉)这一行。 challenge.sender?.use(credential, for: 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) 

}

0

我已与管理认证WKWebView一个UIViewController,您可以安装使用CocoaPod