开发者

Auto fill and submit HTML form and view in UIWebView iOS

I'm trying to automatically fill and submit an HTML form on the iPhone and iPad, but then view the resulting page in a UIWebView.

Example: There's a website to add money to a meal plan for my university, and it requires you to put in your student number first, press submit, put in your password, press submit, then it reveals your balance after that.

I am trying to make an app that lets you save your information in it, press GO and then in the background it will fill in the saved student number, hit submit, wait for the resulting page to be loaded, then input the password, press submit, then finally display th开发者_如何学Pythone resulting page with the balance.

I've tried multiple times to submit forms, but can't get them to display in a UIWebView after.

Does anyone have any ideas or solutions?

Appreciate all the help! Sorry if it's a silly question.


You can do it with JavaScript. Use the -stringByEvaluatingJavaScriptFromString: method of UIWebView to enter the text and click the button.


For those who need the code I hope this 3 methods can help :

This method will fill the username field:

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

    NSString *loadUsernameJS =
    [NSString stringWithFormat:@"var inputFields = document.querySelectorAll(\"input[type='email']\"); \
     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];
    [webView stringByEvaluatingJavaScriptFromString: loadUsernameJS];
    [webView stringByEvaluatingJavaScriptFromString: loadText];

}

This one will fill the password field:

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

}

To submit your forms :

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

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

} 
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜