2

I want to parse RTF file in Objective c.Is its possible to parse it? if is possible then how can I use it into IOS.I want all the text and formatting. I have tried in so many way to get text and formatting for this purpose I have used UIWEBVIEW but its not return any text string

NSURL *imgPath = [[NSBundle mainBundle] URLForResource:@"test" withExtension:@"rtf"];
NSData *data = [NSData dataWithContentsOfURL:imgPath];
[webView loadData:data MIMEType:@"text/rtf" textEncodingName:@"utf-8" baseURL:[NSURL URLWithString:@"/"]];
 NSString *html = [webView stringByEvaluatingJavaScriptFromString: @"document.body.innerHTML"];

Here html return nil value. Thanks

Shinning River
  • 813
  • 1
  • 10
  • 23
  • Maybe these will be of help: http://stackoverflow.com/questions/9501240/ios-objective-c-display-rtf-document and http://stackoverflow.com/questions/10686381/how-to-read-rtf-file-from-url-in-iphone-app – Jens Bergvall Jun 17 '13 at 13:10
  • @Da_smokes Yes I am doing in the same way but the issue is I want extract text and formatting from RTF – Shinning River Jun 17 '13 at 13:21
  • if test.rtf is in your app (the path is in the mainBundle) why are you using a webview? try converting the data to an nsstring with [[NSString alloc]initWithData:data encoding:NSUTF8StringEncoding] – gg13 Jun 17 '13 at 20:02
  • @gg13 Yes I can get the text by using [[NSString alloc]initWithData:data encoding:NSUTF8StringEncoding] but the issue is when I am going to edit the RTF then how to edit it – Shinning River Jul 12 '13 at 08:04
  • ...well you'll have the contents of the RTF that way and you can edit it as you please – gg13 Jul 15 '13 at 21:42

1 Answers1

1

Ok, this question is very old but in case someone stumbles across this problem:

The html is nil as the webView is not finished loading. Fetch the html in the UIWebViewDelegate:

- (void)webViewDidFinishLoad:(UIWebView *)webView
{
    NSString *html = [webView stringByEvaluatingJavaScriptFromString: @"document.body.innerHTML"];

    NSLog(@"Html: %@",html);

}

You will get the desired html.

SHN
  • 785
  • 7
  • 23