当用户第一次登录时,你需要听
func webView(_ webView: UIWebView, shouldStartLoadWith request: URLRequest, navigationType: UIWebViewNavigationType) -> Bool
在这里,您可以获取帖子数据,其中应包含用户名,密码和记住用户名值 . 检查是否选中了记住用户名,然后将用户名保存到核心数据 .
当用户在任何其他时间登录时,请使用此 webViewDidFinishLoad(_ webView: UIWebView) 填写用户名文本字段 .
您可以使用 webView.stringByEvaluatingJavaScript(from: String) 执行此操作
你需要使用一些javascript,它应该是这样的 .
document.getElementByID('id of the username text field').value = username
因此,对于您的网站,您的委托方法应该如下所示 .
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
}