iOSアプリのWKWebViewに基本認証機能を追加しようとしています。基本的な認証ポップアップが正しい方法で表示されます。
しかし、私は正しい情報を入力した後、もう一度ポップアップが表示され、情報を再び入力するとアプリケーションが破損します。コンソールに'NSInternalInconsistencyException', reason: 'Completion handler passed to -[SFSafariViewController.WebViewViewController webView:didReceiveAuthenticationChallenge:completionHandler:] was not called'
と表示されます。
2番目のポップアップの背後にはウェブサイトのコンテンツが既に表示されているので、基本的な認証ポップアップが間違った方法で呼び出されると思います。
どこで修正する必要がありますか?第二のポップアップが現れたときに、私は別のコンソール警告ログを見つけ
WKWebView基本認証ポップアップが2回表示され、アプリがクラッシュする
import UIKit
import WebKit
class WebViewViewController: UIViewController, WKUIDelegate, WKNavigationDelegate {
@IBOutlet weak var webView: WKWebView!
var startUrl = "http://basic-auth-page.com"
var pageTitle: String?
var requestUrl: String?
override func loadView() {
super.loadView()
webView.uiDelegate = self
webView.navigationDelegate = self
}
override func viewDidLoad() {
super.viewDidLoad()
webView.allowsBackForwardNavigationGestures = true
if let url = URL(string: startUrl) {
let request = URLRequest(url: url)
webView.load(request)
}
}
// open "target_blank" link
func webView(_ webView: WKWebView, createWebViewWith configuration: WKWebViewConfiguration, for navigationAction: WKNavigationAction, windowFeatures: WKWindowFeatures) -> WKWebView? {
if navigationAction.targetFrame == nil {
webView.load(navigationAction.request)
}
return nil
}
// for basic auth
func webView(_ webView: WKWebView, didReceive didReceiveAuthenticationChallenge: URLAuthenticationChallenge, completionHandler: @escaping(URLSession.AuthChallengeDisposition, URLCredential?) -> Void){
let alertController = UIAlertController(title: "Authentication Required", message: "This is an alert.", preferredStyle: .alert)
weak var usernameTextField: UITextField!
alertController.addTextField { textField in
textField.placeholder = "Username"
usernameTextField = textField
}
weak var passwordTextField: UITextField!
alertController.addTextField { textField in
textField.placeholder = "Password"
textField.isSecureTextEntry = true
passwordTextField = textField
}
alertController.addAction(UIAlertAction(title: "Cancel", style: .cancel, handler: { action in
completionHandler(.cancelAuthenticationChallenge, nil)
}))
alertController.addAction(UIAlertAction(title: "Log In", style: .default, handler: { action in
let credential = URLCredential(user: usernameTextField.text!, password: passwordTextField.text!, persistence: URLCredential.Persistence.forSession)
completionHandler(.useCredential, credential)
}))
self.present(alertController, animated: true, completion: nil)
}
@IBAction func prevBtn(_ sender: UIBarButtonItem) {
if webView.canGoBack {
webView.goBack()
}
}
@IBAction func nextBtn(_ sender: UIBarButtonItem) {
if webView.canGoForward {
webView.goForward()
}
}
@IBAction func refreshBtn(_ sender: UIBarButtonItem) {
webView.reloadFromOrigin()
}
@IBAction func doneBtn(_ sender: UIBarButtonItem) {
let initialBoard = storyboard!.instantiateViewController(withIdentifier: "initialBoard")
self.present(initialBoard, animated: true, completion: nil)
}
func webView(_ webView: WKWebView, didFinish navigation: WKNavigation!) {
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}
}
===編集===:
は、ここに私のコードです。
Warning: Attempt to present <UIAlertController: 0x7fa46d88a600> on <SFSafariViewController.WebViewViewController: 0x7fa46f10b350> which is already presenting <UIAlertController: 0x7fa46e819000>
おかげで、まだエラーが発生しています。おそらく基本認証の問題ではなく、UIAlertControllerの処理です。 Nowポップアップは通常の(=基本認証機能なし)ページにも表示されます – Udomomo