Wkwebview Auto Fill Login Form Swift 2

Autofill username into web view

When the user firsts signs in you need to listen to

func webView(_ webView: UIWebView, shouldStartLoadWith request: URLRequest, navigationType: UIWebViewNavigationType) -> Bool

Here you can get the post data, which should contain the username, password, and remember username values. Check if the remember username is checked and then save the username to core data.

When the user logs in any other time, use this webViewDidFinishLoad(_ webView: UIWebView) to fill in the username text field.

You can do this by using the webView.stringByEvaluatingJavaScript(from: String)

You'll need to use some javascript and it should be something like this.

document.getElementByID('id of the username text field').value = username

So for your website, your delegate methods should look like this.

func webViewDidFinishLoad(_ webView: UIWebView) {

if let url = webView.request?.url {

if url.absoluteString == "https://retail.onlinesbi.com/retail/login.htm" {

// Autofill the username and password here.

webView.stringByEvaluatingJavaScript(from: "document.getElementById('username').value = \"username\"")

webView.stringByEvaluatingJavaScript(from: "document.getElementById('label2').value = \"password\"")
}
}
}

func webView(_ webView: UIWebView, shouldStartLoadWith request: URLRequest, navigationType: UIWebViewNavigationType) -> Bool {

if let url = webView.request?.url {

if url.absoluteString == "https://retail.onlinesbi.com/retail/login.htm" {

// Get the username and password here.

let username = webView.stringByEvaluatingJavaScript(from: "document.getElementById('username').value")

let password = webView.stringByEvaluatingJavaScript(from: "document.getElementById('label2').value")
}
}
return true
}

How to use autoFill with keychain access in WKWebView

Almost sure this has been answered elsewhere, but Enabling Password AutoFill on an HTML Input Element from Apple's docs is what you need.

TL;DR:

<input id="user-text-field" type="email" autocomplete="username"/>
<input id="password-text-field" type="password" autocomplete="current-password"/>


Related Topics



Leave a reply



Submit