Autofill Username and Password UIWebView Swift

2020-02-29 06:25发布

I've found many ways to autofill a username and password in objective-c, but I can't figure out how to make it work with Swift. What would be the Swift equivalent of this code and would this work for what I am trying to accomplish? Thanks

- (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];
}

I have this but it doesn't autofill for some reason even though I have tested and the values are stored correctly and are being grabbed correctly:

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)
    }
}

1条回答
smile是对你的礼貌
2楼-- · 2020-02-29 07:02

Here is a simple swift conversion of your provided code. SStringByEvaluatingJavaScriptFromString has been tested using different javascript and works correctly.

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)
    }
}
查看更多
登录 后发表回答