The web view first loads a url to a login page. When the user logs in for the first time and selects the checkbox to remember the username, save the username to core data. When the user logs in again, autofill username text field for the username stored in core data.
How would this be done in Swift?
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
}