Search code examples
javascriptobjective-ciosuiwebview

extract text from UIWebView


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!


Solution

  • It turns out document.documentElement.innerText is the way to go. Gets me the plain text right away.