में WKWebView के साथ प्रमाणीकरण मेरे आईओएस ऐप में, मैं एप्लिकेशन में बाहरी यूआरएल लपेटने के लिए WKWebView का उपयोग करना चाहता हूं। इस यूआरएल को बुनियादी प्रमाणीकरण की आवश्यकता है (इसे उपयोगकर्ता और पासवर्ड प्रमाण पत्र की आवश्यकता है, जैसे कि निम्नलिखित स्क्रीनशॉट में)।स्विफ्ट
कुछ जांच के बाद, मैं didReceiveAuthenticationChallenge
विधि का उपयोग करने, ताकि एक स्वचालित प्रवेश सक्षम करने में कोशिश कर रहा हूँ, तो मैं समझ नहीं कर रहा हूँ कि यह कैसे काम करता है।
यह मेरा कोड है।
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)
}
}
}
मैं इस अपवाद के साथ सामना करना पड़ रहा हूँ:
Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Completion handler passed to -[MyUnimol.WebViewController webView:didReceiveAuthenticationChallenge:completionHandler:] was not called'
अगर मैं didReceiveAuthenticationChallenge
विधि हटाने के लिए, मैं यूआरएल तक पहुँचने में सक्षम हूँ लेकिन है मुझे, obliviously, गलत क्रेडेंशियल्स देता है।
कोई भी मुझे समझा सकता है कि मैं क्या गलत कर रहा हूं? didReceiveAuthenticationChallenge
के अंत में
completionHandler(NSURLSessionAuthChallengeDisposition.UseCredential, credential)
समस्या हल: