admin管理员组文章数量:1397016
i have a UIWebView loaded with a simple rtf file. (containing one line "THIS IS A TEST")
UIWebView* webview = [[UIWebView alloc] initWithFrame:CGRectMake(5, 50, 310, 400)];
[[self view] addSubview:webview];
[webview loadRequest:[NSURLRequest requestWithURL:[NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:@"test" ofType:@"rtf"]isDirectory:NO]]];
NSString *html = [webview stringByEvaluatingJavaScriptFromString: @"all"];
NSLog (@"html:%@", html.debugDescription);
The "THIS IS A TEXT" line appears inside the UIWebView correctly. Does anyone know if it's somehow possible to extract that line of text (or more) to an NSString or some other accessible container ?
I know there is:
NSString *html = [webview stringByEvaluatingJavaScriptFromString: @"document.documentElement.outerHTML"];
but that (obviously) doesn't work here. any ideas ? I'm basically trying to (fake) convert a rtf file to an NSString on iOS. Thank you!
i have a UIWebView loaded with a simple rtf file. (containing one line "THIS IS A TEST")
UIWebView* webview = [[UIWebView alloc] initWithFrame:CGRectMake(5, 50, 310, 400)];
[[self view] addSubview:webview];
[webview loadRequest:[NSURLRequest requestWithURL:[NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:@"test" ofType:@"rtf"]isDirectory:NO]]];
NSString *html = [webview stringByEvaluatingJavaScriptFromString: @"all"];
NSLog (@"html:%@", html.debugDescription);
The "THIS IS A TEXT" line appears inside the UIWebView correctly. Does anyone know if it's somehow possible to extract that line of text (or more) to an NSString or some other accessible container ?
I know there is:
NSString *html = [webview stringByEvaluatingJavaScriptFromString: @"document.documentElement.outerHTML"];
but that (obviously) doesn't work here. any ideas ? I'm basically trying to (fake) convert a rtf file to an NSString on iOS. Thank you!
Share Improve this question edited Jun 7, 2012 at 4:29 EarlGrey asked Jun 7, 2012 at 1:01 EarlGreyEarlGrey 2,5434 gold badges34 silver badges64 bronze badges 4- have you tried document.body.innerHTML? – J Max Commented Jun 7, 2012 at 1:06
- yes. and document.body.innerText . both e back blank. – EarlGrey Commented Jun 7, 2012 at 1:20
- Are you waiting till the page has loaded before calling stringByEvaluatingJavaScriptFromString:? – Piepants Commented Jun 7, 2012 at 2:12
- actually, you were right. I had to implement webViewDidFinishLoad delegate method and then I started to get some html results inside of it. sleep(int) was not the way to go. you should change this to an aswer. – EarlGrey Commented Jun 7, 2012 at 4:27
3 Answers
Reset to default 5It turns out document.documentElement.innerText
is the way to go. Gets me the plain text right away.
Instead of fetching html from UIWebView, and again apply extra efforts to convert html into plain text we can directly use below code to fetch text content from UIWebView:
NSString *plainText = [webView stringByEvaluatingJavaScriptFromString:@"document.body.textContent"];
It saves your extra efforts for converting html into plain text.
I've actually had to do something similar in the past. Maybe not the most elegant, but here is what worked for me:
I would load the text from the file in to an HTML page (you can do this with javascript from inside the UIWebView, or you can do it from Objective-C creating a javascript method to call using stringByEvaluatingJavaScriptFromString). Then you can run the method call you outlined above, which should work fine, or you can create a custom method in Javascript that would capture and return the text to the Objective-C code. Here, it is stored as a string and is ready to go.
本文标签: javascriptextract text from UIWebViewStack Overflow
版权声明:本文标题:javascript - extract text from UIWebView - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744106676a2591094.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论