作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在开发一个将 HTML 页面加载到 uiwebview 的应用程序 - 该 html 文件包含许多字段,我需要获取 3 个字段并将其传递给 Web 服务
这是我的 HTML 页面:
<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">
我需要捕获其中已经存在的 myId、名字和状态值。
这里是我的示例代码:
- (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"];
编辑代码: NSString* value = [网页 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);
我的O/P:
值==
我的Id=
最佳答案
这是一些不规则但有效的方法,试试这个
- (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);
你的名字是
valkue==123654 value=xxxxx
myId=123654
name=xxxxx
关于ios - 如何在 ios 中提取隐藏的输入 uiwebview?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26796197/
我是一名优秀的程序员,十分优秀!