3

I have a html form which when it loads is already populated with data. It has Save and Apply buttons on.

I want to change 1 item on the form and POST it from my iOS app. The problem is that when I send the POST with my changed Form Data info for the item i'm changing, I also need to submit all the other current settings from the form or they get overwritten with blanks. (I have no control over editing the page).
I could parse the page beforehand to grab the displayed data from the form then send it with the POST, however the page can appear different for each user so this will be very difficult.

In a web browser when I hit the Save button after loading the page, I can see all the POST headers and form data, so I'm thinking I can do this in my app to grab the current data then amend the 1 item I wish to change, then rePOST it.

So my question is, if I read a page with AFNetworking, can I then simulate pressing the Save button on that page, so I can then read the response but this time with the POST and header data?

Or any alternative thought on how to achieve this?

Cœur
  • 37,241
  • 25
  • 195
  • 267
Darren
  • 10,182
  • 20
  • 95
  • 162

1 Answers1

5

This might work by accessing the Document Object Model... check the syntax...

NSString *formValue = [webView stringByEvaluatingJavaScriptFromString
                        :@"document.forms[0].elements[0].value"];

//...change value...

NSString *jScript = [NSString stringWithFormat
                     :@"document.forms[0].elements[0].value='%@';", formValue ];

[webView stringByEvaluatingJavaScriptFromString: jScript ];

[webView stringByEvaluatingJavaScriptFromString: @"document.forms[0].submit();"];
kineticfocus
  • 492
  • 4
  • 16
  • I've not tried this yet but I see you have webView. I'm not using a webView, I want it to happen in the background so I can read the resulting headers. – Darren Nov 02 '12 at 16:50
  • 6
    As the creator of AFNetworking, let me be the first to say that you definitely shouldn't be using AFNetworking for this. Listen to @kineticfocus--take advantage of the Javascript interactions in a web view (even if the web view is not actually displayed to the user, you can still interact with it programmatically) – mattt Nov 05 '12 at 07:17