3

I am trying to submit a login form (which can be viewed here) in my app, but there is no PHP file whatsoever - so how can I code something similar to this, just without the PHP and just literally inputting text into the text fields in UIWebView and then submitting the data to the destination URL?

Thanks!

Community
  • 1
  • 1
pixelbitlabs
  • 1,934
  • 6
  • 36
  • 65

1 Answers1

3

It doesn't matter if the server is PHP or any other technology. You can use NSURLRequest to submit it.

Anyway, if for some reason you need to use the UIWebView approach, you'll need to fill the form and submit it using JavaScript with stringByEvaluatingJavaScriptFromString.

Add something like this in -[UIWebViewDelegate webViewDidFinishLoad:]: (adjust jQuery paths to your case)

- (void)webViewDidFinishLoad:(UIWebView *)webView 
{
    ...
    NSString *result;
    result = [webView stringByEvaluatingJavaScriptFromString:@"$('#username-box').val('the-user-name');"];
    result = [webView stringByEvaluatingJavaScriptFromString:@"$('#password-box').val('secret');"];
    result = [webView stringByEvaluatingJavaScriptFromString:@"$('#submit-button').click();"];
    ...
}
djromero
  • 19,551
  • 4
  • 71
  • 68
  • Great, thank you - very very appreciated! How can I use the contents of a UITextField to put into the username field and then another UITextField to put that text into the password field? I tried doing it but failed :( – pixelbitlabs Oct 24 '11 at 17:52
  • 1
    Once you get the UITextField value, I guess something like this will work: [webView stringByEvaluatingJavaScriptFromString:[NSString stringWithFormat:@"$('#username-box').val('%@');", fieldValue]]; – djromero Oct 25 '11 at 09:27
  • oops, doesn't work, I was testing it out with previous code. Nothing happens :( – pixelbitlabs Oct 25 '11 at 12:30
  • do you have another solution please? :-) – pixelbitlabs Oct 25 '11 at 15:02
  • Nop. The solution I proposed works. Maybe you can ask a different question, with details about the problem you have, and we'll see if we can answer it. – djromero Oct 25 '11 at 15:07
  • Amazing solution! Thanks! :) – Amit Mar 17 '13 at 23:38