在UIWebView iOS中自动填写并提交HTML表单和视图

时间:2017-03-27 12:22:30

标签: javascript ios objective-c uiwebview ios10

- (void)completeUserFieldsForWebView:(UIWebView *)webView withUsername:(NSString *)username {

    NSString *loadUsernameJS =
    [NSString stringWithFormat:@"var inputFields = document.querySelectorAll(\"input[type='text']\"); \
     for (var i = inputFields.length >>> 0; i--;) { inputFields[i].value = '%@';}", username];
    NSString *loadText =
    [NSString stringWithFormat:@"var inputFields = document.querySelectorAll(\"input[type='text']\"); \
     for (var i = inputFields.length >>> 0; i--;) { inputFields[i].value = '%@';}", username];

     NSLog(@"loadUsernameJS%@",loadUsernameJS);
     NSLog(@"loadText%@",loadText);

    [webView stringByEvaluatingJavaScriptFromString:loadUsernameJS];
    [webView stringByEvaluatingJavaScriptFromString:loadText];

    NSLog(@"%@",[webView stringByEvaluatingJavaScriptFromString:loadUsernameJS]);
    NSLog(@"%@",[webView stringByEvaluatingJavaScriptFromString:loadText]);

}
- (void)completePasswordFieldsForWebView:(UIWebView *)webView withPassword:(NSString *)password {

    NSString *loadPasswordJS =
    [NSString stringWithFormat:@"var passFields = document.querySelectorAll(\"input[type='password']\"); \
     for (var i = passFields.length>>> 0; i--;) { passFields[i].value ='%@';}", password];
    [webView stringByEvaluatingJavaScriptFromString: loadPasswordJS];

}
- (void)clickOnSubmitButtonForWebView:(UIWebView *)webView {

    NSString *performSubmitJS = @"var passFields = document.querySelectorAll(\"input[type='submit']\"); \
    passFields[0].click()";
    [webView stringByEvaluatingJavaScriptFromString:performSubmitJS];

}

这是正常工作,除了当用户名有斜杠b(即,\ b)时我遇到的问题,例如test \ btest它只将用户名设置为testtest。因此,每当我收到错误的用户名时,请帮助我。因为我没有太多的javascript知识。我之所以认为这只是因为\ b。但是用户名可能包含\ b。那么在这种情况下如何处理这种情况。

0 个答案:

没有答案