问题描述
在我的iOS应用程序中,我想使用WKWebView在应用程序中包装外部URL。此URL需要基本身份验证(需要用户和密码凭据,如下面的屏幕截图所示)。
In my iOS app, I would like to use a WKWebView to wrap an external URL in the application. This URL requires basic authentication (it needs user and password credential, like in the following screenshot).
经过一番调查,我正在尝试使用 didReceiveAuthenticationChallenge
方法,以启用自动登录,所以我不是了解它的工作原理。
After some investigation, I'm trying to use didReceiveAuthenticationChallenge
method, in order to enable an automatic login, so I'm not understanding how it works.
这是我的代码。
import UIKit
import WebKit
class WebViewController: UIViewController, WKNavigationDelegate {
var webView: WKWebView?
private var request : NSURLRequest {
let baseUrl = "https://unimol.esse3.cineca.it/auth/Logon.do"
let URL = NSURL(string: baseUrl)!
return NSURLRequest(URL: URL)
}
/* Start the network activity indicator when the web view is loading */
func webView(webView: WKWebView,
didStartProvisionalNavigation navigation: WKNavigation){
UIApplication.sharedApplication().networkActivityIndicatorVisible = true
}
/* Stop the network activity indicator when the loading finishes */
func webView(webView: WKWebView,
didFinishNavigation navigation: WKNavigation){
UIApplication.sharedApplication().networkActivityIndicatorVisible = false
}
func webView(webView: WKWebView,
decidePolicyForNavigationResponse navigationResponse: WKNavigationResponse,
decisionHandler: ((WKNavigationResponsePolicy) -> Void)){
decisionHandler(.Allow)
}
func webView(webView: WKWebView, didReceiveAuthenticationChallenge challenge: NSURLAuthenticationChallenge, completionHandler: (NSURLSessionAuthChallengeDisposition, NSURLCredential?) -> Void) {
if challenge.protectionSpace.host == "https://unimol.esse3.cineca.it/auth/Logon.do" {
let user = "*******"
let password = "******"
let credential = NSURLCredential(user: user, password: password, persistence: NSURLCredentialPersistence.ForSession)
challenge.sender?.useCredential(credential, forAuthenticationChallenge: challenge)
}
}
override func viewDidLoad() {
/* Create our preferences on how the web page should be loaded */
let preferences = WKPreferences()
preferences.javaScriptEnabled = false
/* Create a configuration for our preferences */
let configuration = WKWebViewConfiguration()
configuration.preferences = preferences
/* Now instantiate the web view */
webView = WKWebView(frame: view.bounds, configuration: configuration)
if let theWebView = webView {
/* Load a web page into our web view */
let urlRequest = self.request
theWebView.loadRequest(urlRequest)
theWebView.navigationDelegate = self
view.addSubview(theWebView)
}
}
}
我正面临这个例外:
由于未捕获的异常'NSInternalInconsistencyException'而终止应用程序,原因:'完成处理程序已通过to - [MyUnimol.WebViewController webView:didReceiveAuthenticationChallenge:completionHandler:]未被称为'
如果我删除 didReceiveAuthenticationChallenge
方法,我能够到达网址,但遗憾地给了我错误的凭据。
If I delete the didReceiveAuthenticationChallenge
method, I'm able to reach the URL but is gives me, obliviously, wrong credentials.
任何人都可以解释我的错误吗?
Anyone could explain me what I'm doing wrong please?
推荐答案
添加以下行:
completionHandler(NSURLSessionAuthChallengeDisposition.UseCredential, credential)
在 didReceiveAuthenticationChallenge
结束时解决了问题。
这篇关于在Swift中使用WKWebView进行身份验证的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!