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)
}
}
:
は、ここに私のコードです。
誰かが助けてもらえますか?
ありがとうございました! "_" を追加することにより、
デビッド
追加することにより、固定変種#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
WKUIDelegateを使用することを意味しましたか?私は、(代わりにwebView.uiDelegate =自己のwebView.navigationDelegate =自己設定)WKNavigationDelegateと私のビューコントローラクラスを拡張し、次の関数implimented:FUNCのWebViewの(_のWebView:WKWebView、didReceive挑戦:URLAuthenticationChallenge、completionHandler:@escaping(URLSession.AuthChallengeDispositionを、URLCredential?) - > Void){ – CScott
はい、既に以下の回答を共有しました – David