我发现很多方法可以在objective-c中自动填充用户名和密码,但我无法弄清楚如何使其与Swift一起使用。什么是Swift相当于这段代码,这对我想要完成的工作有用吗?感谢
- (void)webViewDidFinishLoad:(UIWebView *)webView {
// Auto fill the username and password text fields, assuming the HTML has
// <input type="text" name="username"> and
// <input type="text" name="password">
NSString *savedUsername = [[NSUserDefaults standardUserDefaults] objectForKey:@"USERNAME"];
NSString *savedPassword = [[NSUserDefaults standardUserDefaults] objectForKey:@"PASSWORD"];
if (savedUsername.length != 0 && savedPassword.length != 0) {
// Create js strings
NSString *loadUsernameJS = [NSString stringWithFormat:@"var inputFields = document.querySelectorAll(\"input[name='username']\"); \
for (var i = inputFields.length >>> 0; i--;) { inputFields[i].value = '%@';}", savedUsername];
NSString *loadPasswordJS = [NSString stringWithFormat:@"var inputFields = document.querySelectorAll(\"input[name='password']\"); \
for (var i = inputFields.length >>> 0; i--;) { inputFields[i].value = '%@';}", savedPassword];
// Runs the JS
[self.webview stringByEvaluatingJavaScriptFromString: loadUsernameJS];
[self.webview stringByEvaluatingJavaScriptFromString: loadPasswordJS];
}
我有这个但是由于某些原因它没有自动填充,即使我已经测试过并且值正确存储并且正在被正确抓取:
func webViewDidFinishLoad(_ : UIWebView){
let savedUsername = NSUserDefaults.standardUserDefaults().stringForKey("USERNAME")
let savedPassword = NSUserDefaults.standardUserDefaults().stringForKey("PASSWORD")
if savedUsername == nil || savedPassword == nil {return}
if ( countElements(savedUsername!) != 0 && countElements(savedPassword!) != 0) {
let loadUsernameJS = "var inputFields = document.querySelectorAll(\"input[name='username']\"); \\ for (var i = inputFields.length >>> 0; i--;) { inputFields[i].value = \'\(savedUsername)\';}"
let loadPasswordJS = "var inputFields = document.querySelectorAll(\"input[type='password']\"); \\ for (var i = inputFields.length >>> 0; i--;) { inputFields[i].value = \'\(savedPassword)\';}"
self.webView.stringByEvaluatingJavaScriptFromString(loadUsernameJS)
self.webView.stringByEvaluatingJavaScriptFromString(loadPasswordJS)
}
}
答案 0 :(得分:6)
以下是您提供的代码的简单快速转换。 SStringByEvaluatingJavaScriptFromString已经使用不同的javascript进行了测试,并且工作正常。
func webViewDidFinishLoad(webView: UIWebView!) {
let savedUsername = NSUserDefaults.standardUserDefaults().stringForKey("USERNAME")
let savedPassword = NSUserDefaults.standardUserDefaults().stringForKey("PASSWORD")
if savedUsername == nil || savedPassword == nil {return}
if ( countElements(savedUsername!) != 0 && countElements(savedPassword!) != 0) {
let loadUsernameJS = "var inputFields = document.querySelectorAll(\"input[name='username']\"); \\ for (var i = inputFields.length >>> 0; i--;) { inputFields[i].value = \'\(savedUsername)\';}"
let loadPasswordJS = "var inputFields = document.querySelectorAll(\"input[name='password']\"); \\ for (var i = inputFields.length >>> 0; i--;) { inputFields[i].value = \'\(savedPassword)\';}"
self.webview.stringByEvaluatingJavaScriptFromString(loadUsernameJS)
self.webview.stringByEvaluatingJavaScriptFromString(loadPasswordJS)
}
}