iOS

Auto Fill Username & Password Fields in UIWebView

If you use UIWebView and would like to automatically fill in the username and password (or any other text input fields), you could do so by executing Javascript on the webview.

I first learnt about this from stackoverflow, and has edited slightly.

The technique is to implement UIWebViewDelegate and execute some Javascript in webViewDidFinishLoad.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
- (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];
    }
}

Comments