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).
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.
This is my code.
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) } } }
I'm facing with this exception:
Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Completion handler passed to -[MyUnimol.WebViewController webView:didReceiveAuthenticationChallenge:completionHandler:] was not called'
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?
A WKWebView object is a platform-native view that you use to incorporate web content seamlessly into your app's UI. A web view supports a full web-browsing experience, and presents HTML, CSS, and JavaScript content alongside your app's native views.
Difference Between UIWebview and WKWebView UIWebview is a part of UIKit, so it is available to your apps as standard. You don't need to import anything, it will we there by default. But WKWebView is run in a separate process to your app,. You need to import Webkit to use WKWebView in your app.
WKWebView - This view allows developers to embed web content in your app. You can think of WKWebView as a stripped-down version of Safari. It is responsible to load a URL request and display the web content. WKWebView has the benefit of the Nitro JavaScript engine and offers more features.
Loading local content WKWebView can load any HTML stored in your app bundle using its loadFileURL() method. You should provide this with a URL to some HTML file that you know is in your bundle, along with another URL that stores any other files you want to allow the web view to read. That url.
Add the following line:
completionHandler(NSURLSessionAuthChallengeDisposition.UseCredential, credential)
at the end of didReceiveAuthenticationChallenge
solved the problem.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With