2016-12-04 18 views
1

Webページを表示する簡単なWebViewを構築しようとしています。このページには、テスト目的ですべてのページでhttp認証が必要です。私はwillSendRequestForAuthenticationChallengeとdidReceiveAuthenticationChallengeを見つけたが、それらのどれも呼ばれていないと私は私が認証されなかったサーバーからのエラーを持っている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) 
    } 

} 

は、ここに私のコードです。

誰かが助けてもらえますか?

ありがとうございました! "_" を追加することにより、

デビッド

+0

追加することにより、固定変種#1 "_": FUNCのWebViewの(_のWebView: WKWebView、didReceive challenge:URLAuthenticationChallenge、completionHandler:@エスケープ(URLSession.AuthChallengeDisposition、URLCredential?) - >無効){ let user = "user" let password = "pass" LET資格= URLCredential(ユーザー:ユーザー、パスワード:パスワード、持続性:URLCredential.Persistence.forSession) challenge.sender .USE(の資格、:挑戦)? completionHandler(URLSession.AuthChallengeDisposition.useCredential、資格) } – David

+0

WKUIDelegateを使用することを意味しましたか?私は、(代わりにwebView.uiDelegate =自己のwebView.navigationDelegate =自己設定)WKNavigationDelegateと私のビューコントローラクラスを拡張し、次の関数implimented:FUNCのWebViewの(_のWebView:WKWebView、didReceive挑戦: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