Finding and auto-filling HTML login forms in a UIWebView using JavaScript

前端 未结 4 1340
自闭症患者
自闭症患者 2020-12-24 09:30

i have a UIWebView which acts like an internet browser and loads the HTML of the webpages that it is at.

in the webViewController, the method webViewDidFinishLoad, w

4条回答
  •  清酒与你
    2020-12-24 09:55

    I had some problems with finding the fields, so I adapted the code to target the fields more specific:

       - (void)webViewDidFinishLoad:(UIWebView*)theWebView
        {
    
        NSString *savedUsername = @"username";
        NSString *savedPassword = @"pass";
    
          if (savedUsername.length != 0 && savedPassword.length != 0) {
    
              NSString *loadUsernameJS = [NSString stringWithFormat:@"var inputFields = document.getElementsByTagName('input'); \
                                    for (var i = inputFields.length >>> 0; i--;) { if(inputFields[i].name == 'username') inputFields[i].value = '%@';}", savedUsername];
    
              NSString *loadPasswordJS = [NSString stringWithFormat:@" \
                                    for (var i = inputFields.length >>> 0; i--;) { if(inputFields[i].name == 'password') inputFields[i].value = '%@';}", savedPassword];
    
        //autofill the form
           [theWebView stringByEvaluatingJavaScriptFromString: loadUsernameJS];
           [theWebView stringByEvaluatingJavaScriptFromString: loadPasswordJS];
          }
    
    
        return [super webViewDidFinishLoad:theWebView];
      }      
    

提交回复
热议问题