1

Some help needed, please:

I have an NSData ready with bytes (it's an RTF formatted text).

I'd like to be able to load the said NSData into a UIWebView without having to create a binary file first, so in other words without having to do this:

[UIWebView loadRequest:[NSURLRequest requestWithURL:[NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:@"test" ofType:@"rtf"]isDirectory:NO]]]; }

I need to call this many times, so I'd like to avoid saving NSData into a binary file every time. Thank you.

3
  • Your NSData is just an HTML string? Commented Jun 8, 2012 at 1:11
  • it's RTF formatted data. Commented Jun 8, 2012 at 1:17
  • Well, I mean, a format that a WebView can accept directly with no modification...I've never tried an RTF file but you can try the post in my answer. Commented Jun 8, 2012 at 1:18

2 Answers 2

3

Use loadData:MIMEType:textEncodingName:baseURL:

[webView loadData:data MIMEType:@"application/rtf" textEncoding:NSUTF8StringEncoding baseURL:someURL];

You generally need to provide some kind of URL as the baseURL, but it doesn't deeply matter what you use if there are no relative links. (See loadHTMLString baseURL:nil not working in iOS5 for more discussion on that.)

Sign up to request clarification or add additional context in comments.

1 Comment

Oh! Excellent! I didn't know about this method. You win ^^;
2

I'm going to assume that your NSData just contains HTML string data. In that case you can do the following:

//Use whatever encoding, I assume UTF8
NSString *html = [[NSString alloc] initWithData:yourData encoding:NSUTF8StringEncoding];
[yourWebView loadHTMLString:html baseURL:pathToYourHtml];
[html release]; //Not needed in ARC

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.