1

Am developing an app which load a HTML page into uiwebview - that html file contains many fields i need to fetch 3 fields and pass it to web services

Here is my HTML page :

<form name="frmPostBack" id="frmPostBack" action="failure" method="post"        style="display:none;">
<input type="hidden" name="myId" value="123654">
<input type="hidden" name="first name" value="xxxxx">
<input type="hidden" name="status" value="failure">

i need to capture myId,first name and status values which already present in it.

here my sample code :

- (void)webViewDidFinishLoad:(UIWebView *)webView {
NSCachedURLResponse *resp = [[NSURLCache sharedURLCache]cachedResponseForRequest:webpage.request];
NSString *yourHTMLSourceCodeString = [webView stringByEvaluatingJavaScriptFromString:@"document.documentElement.innerHTML"];
NSLog(@"%@",[(NSHTTPURLResponse*)resp.response allHeaderFields]);
NSLog(@"%@",yourHTMLSourceCodeString);
NSString* value = [webView stringByEvaluatingJavaScriptFromString:@"document.getElementById('myId').value"];

Edited Code : NSString* value = [webpage stringByEvaluatingJavaScriptFromString:@"document.getElementById('myId').value"];

NSRange r;
while ((r = [value rangeOfString:@"<form name=\"frmPostBack\" id=\"frmPostBack\" action=\"failure\" method=\"post\"        style=\"display:none;\"> <input type=\"hidden\" name=\"myId\" value=\"" options:NSRegularExpressionSearch]).location != NSNotFound)   
value = [value stringByReplacingCharactersInRange:r withString:@""];
NSArray *items = [value componentsSeparatedByString:@" "];
NSString *myId = [items objectAtIndex:0];
NSLog(@"valkue==%@",value);
NSLog(@"myId=%@",myId);

my O/P :
valkue==
myId=

Anand ios
  • 57
  • 1
  • 12

1 Answers1

1

this is some irregular method but works , try this

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


NSCachedURLResponse *resp = [[NSURLCache sharedURLCache]cachedResponseForRequest:webpage.request];
NSString *yourHTMLSourceCodeString = [webView stringByEvaluatingJavaScriptFromString:@"document.documentElement.innerHTML"];
NSLog(@"%@",[(NSHTTPURLResponse*)resp.response allHeaderFields]);
NSLog(@"%@",yourHTMLSourceCodeString);
NSString* value = [webView stringByEvaluatingJavaScriptFromString:@"document.getElementById('myId').value"];

NSRange r;
while ((r = [value rangeOfString:@"<form name=\"frmPostBack\" id=\"frmPostBack\" action=\"failure\" method=\"post\"        style=\"display:none;\"> <input type=\"hidden\" name=\"myId\" value=\"" options:NSRegularExpressionSearch]).location != NSNotFound)

    value = [value stringByReplacingCharactersInRange:r withString:@""];

value=[value stringByReplacingOccurrencesOfString:@"><input type=\"hidden\" name=\"status\" value=\"failure\">" withString:@""];

value=[value stringByReplacingOccurrencesOfString:@"name=\"first name\"" withString:@""];

value=[value stringByReplacingOccurrencesOfString:@"><input type=\"hidden\" " withString:@""];

value=[value stringByReplacingOccurrencesOfString:@"\"" withString:@""];

NSArray *items = [value componentsSeparatedByString:@" "];

NSString *myId = [items objectAtIndex:0]; // pass this value to server
NSString *name = [items objectAtIndex:1];

name=[name stringByReplacingOccurrencesOfString:@"value=" withString:@""]; // pass this value to server


NSLog(@"valkue==%@",value);
NSLog(@"myId=%@",myId);
NSLog(@"name=%@",name);

your o/p is

valkue==123654 value=xxxxx
myId=123654
name=xxxxx
Anbu.Karthik
  • 82,064
  • 23
  • 174
  • 143
  • okie where does value comes from ? i mean value = [value stringByReplacingCharactersInRange:r withString:@""]; – Anand ios Nov 07 '14 at 10:43
  • okie bro i try this code if any problem i comment here, thanks for the code – Anand ios Nov 07 '14 at 10:48
  • iam getting error msg in nslog *** WebKit discarded an uncaught exception in the webView:didFinishLoadForFrame: delegate: *** -[__NSArrayI objectAtIndex:]: index 1 beyond bounds [0 .. 0] – Anand ios Nov 07 '14 at 10:57