gpt4 book ai didi

javascript - 如何访问在 Swift 3 中输入到 WKWebView 的用户名和密码

转载 作者:行者123 更新时间:2023-12-05 07:41:56 26 4
gpt4 key购买 nike

我正在尝试使用 Swift 3 中的 WKWebView 构建一个 iOS 应用程序。WebView 从其 URL 加载一个网站。我需要能够访问用户输入 WebView 的用户名和密码,以便在用户单击提交按钮时将其保存到 UserDefaults。有没有办法做到这一点?谢谢!!

最佳答案

xcode 12.4 的功能

extension ViewController: WKNavigationDelegate{

func webView(_ webView: WKWebView, didStartProvisionalNavigation navigation: WKNavigation!) {


let lurl:String = webView.url?.absoluteString ?? ""

if (lurl.contains(pagweb+"index.php")) {
let defaults = UserDefaults.standard
var user:String = ""
var pass:String = ""
var rememberme: Bool = false

wv.evaluateJavaScript("document.getElementById('user').value;") { (res, error) in
if res != nil {
user = res as! String
}
}
wv.evaluateJavaScript("document.getElementById('pass').value;"){ (res, error) in
if res != nil {
pass = res as! String
}
}
wv.evaluateJavaScript("document.getElementById('rememberme').checked;"){ (res, error) in
if res != nil {
rememberme = res as! Bool
if rememberme {
defaults.set(user, forKey: "USERNAME")
defaults.set(pass, forKey: "PASSWORD")
defaults.set(true, forKey: "rememberme")
}else if pass.count > 7 && user.count > 0{
defaults.set("", forKey: "USERNAME")
defaults.set("", forKey: "PASSWORD")
defaults.set(false, forKey: "rememberme")
}
}
}

}
}



func webView(_ webView: WKWebView, didFinish navigation: WKNavigation!) {

// user - pass - rememberme
let savedUsername = UserDefaults.standard.string(forKey: "USERNAME")
let savedPassword = UserDefaults.standard.string(forKey: "PASSWORD")
let savedrememberme = UserDefaults.standard.bool(forKey: "rememberme")

if savedrememberme {
wv.evaluateJavaScript("document.getElementById('user').value = '"+savedUsername!+"';")
wv.evaluateJavaScript("document.getElementById('pass').value = '"+savedPassword!+"';")
wv.evaluateJavaScript("document.getElementById('rememberme').checked = true;")
}else{
wv.evaluateJavaScript("document.getElementById('user').value = '';")
wv.evaluateJavaScript("document.getElementById('pass').value = '';")
wv.evaluateJavaScript("document.getElementById('rememberme').checked = false;")
}
}

}

关于javascript - 如何访问在 Swift 3 中输入到 WKWebView 的用户名和密码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44945162/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com