Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Authentication with WKWebView in Swift

Tags:

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).

enter image description here

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?

like image 846
giograno Avatar asked May 12 '16 05:05

giograno


People also ask

What is WKWebView in Swift?

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.

What is the difference between UIWebView and WKWebView?

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.

Is WKWebView the same as Safari?

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.

How do I load a URL in WKWebView swift 5?

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.


1 Answers

Add the following line:

completionHandler(NSURLSessionAuthChallengeDisposition.UseCredential, credential) 

at the end of didReceiveAuthenticationChallenge solved the problem.

like image 173
giograno Avatar answered Sep 18 '22 19:09

giograno