1

I have an HTML page with some button in it, i want my Objective C code to programmatically trigger that button so that i can get next response (NEXT HTML page)..

i am trying get and post method but i don't know where to change the html content and post it back..

HTML part of button is:

<input name="xyz" type="button" value="abc" onclick="validate(this.name)" >

is there any way to find where to change the html page post it back..?

and how this link help me..

How to Click A Button Programmatically - Button in WebBrowser ( IE )

NOTE : HTML page is some URL content in INTERNET, i want the next response, ie html page after clicking some button.

Community
  • 1
  • 1
Raju
  • 441
  • 1
  • 6
  • 17

2 Answers2

7

You can use the following method to evaluate javascript with a UIWebView object:

stringByEvaluatingJavaScriptFromString:

So in your case: If you have a UIWebView object called webview:

NSString *jsStat = @"document.getElementsByName('xyz')[0].click()";

[webView stringByEvaluatingJavaScriptFromString:jsStat];

The method returns the result of the script;

Rakesh
  • 3,370
  • 2
  • 23
  • 41
  • My earlier answer was for cocoa webview based application. Updated the answer for ios. – Rakesh Mar 04 '13 at 13:53
  • open your web page in a normal browser (for debuggind) and run the script in console(say in safari) and make sure its getting executed properly. – Rakesh Mar 04 '13 at 13:58
  • You are getting null because the script is failing. For this to work you should have used the webview to load the html page and then use the method provided to you. – Rakesh Mar 04 '13 at 14:09
1

if you are making ios app using objective-c then you need to use UIWebView for this purpose, and load your html in the UIWebView and using its delegate methods check if button is clicked something like this

- (BOOL)webView:(UIWebView*)webView shouldStartLoadWithRequest:(NSURLRequest*)request navigationType:(UIWebViewNavigationType)navigationType {



    if (navigationType == UIWebViewNavigationTypeFormSubmitted) {
 // form is submitted or button is clicked , perform your actions here
 }
return YES;

}
nsgulliver
  • 12,655
  • 23
  • 43
  • 64
  • i have to post ( to server) the content of html page so that it can give the response.. – Raju Mar 04 '13 at 13:52
  • well this is exactly `UIWebViewNavigationTypeFormSubmitted`& `UIWebViewNavigationTypeLinkClicked` does, you can check if the button is clicked or link is clicked then you could do whatever you want to do – nsgulliver Mar 04 '13 at 13:58
  • if you want to play with javascript functions or values of html contents then or you want to call some method then you could use `[self.myWebView stringByEvaluatingJavaScriptFromString:[NSString stringWithFormat:@"callJSMethod('%d')", value]];` – nsgulliver Mar 04 '13 at 14:02