我正在尝试将基本身份验证功能添加到我的iOS应用程序的WKWebView中。基本的auth弹出窗口以正确的方式显示
但是在输入正确的信息后,弹出窗口再次出现,当我再次输入信息时,应用程序会崩溃。控制台说'NSInternalInconsistencyException', reason: 'Completion handler passed to -[SFSafariViewController.WebViewViewController webView:didReceiveAuthenticationChallenge:completionHandler:] was not called'
在第二个弹出窗口后面,网站内容已经显示出来,所以我认为基本的auth弹出窗口是以错误的方式调用的。
我应该在哪里修理?
这是我的代码:
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>
答案 0 :(得分:0)
尝试将您的网络视图回调替换为:
func webView(_ webView: WKWebView, didReceive challenge: URLAuthenticationChallenge, completionHandler: @escaping (URLSession.AuthChallengeDisposition, URLCredential?) -> Void) {
<#code#>
}