views:

52

answers:

1

How do I format an NSURL (what path, or path generator function) so that I can display a downloaded local file (NOT part of the application NSBundle) for use offline? The idea is to snag an HTML file, clean it up and stash it in the iFoo application specific storage area, then later use UIWebView to display it.

Next, I go off to RTFM about working with files on iFoo (iPhone / iPad) in general, but any fast-start tips are appreciated.

+1  A: 

You could read your local file in a string upon completing its download and then use UIWebView's loadHTMLString:baseURL: to load the HTML from that string. Use relative paths and set the correct baseURL:

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); 
NSString *yourFolderPath = [[paths objectAtIndex:0] stringByAppendingPathComponent: @"yourFolder"];
NSString *filePath = [yourFolderPath stringByAppendingPathComponent: @"yourFile.htm"];
NSString *fileContents = [NSString stringWithContentsOfFile: filePath];
NSURL *baseURL = [NSURL fileURLWithPath: yourFolderPath];
[yourWebView loadHTMLString: fileContents baseURL: baseURL];
luvieere
Thanks, that is very interesting. However, I would prefer to put an HTML file and any associated CSS (etc.) into a directory together (or whatever iFoo uses in place of a dir/folder). I guess the real problem remains stashing the file(s), then providing a pathname to that stash. I'm too ignorant, unfortunately. (still, this is a cool trick, and does let you give a context/folder/dir for relative paths out of the text).
Roboprog
@Roboprog I've edited my answer to address your issues. You should use relative file paths for your css and images within your html file, so that setting the baseURL of UIWebView's loadHTMLString allows the files to be searched within the specific folder pointed to by it.
luvieere
Thanks again for the update. It looks like NSDocumentDirectory (presumably this is nested under NSHomeDirectory as "./Documents") can be used cobble up a NSURL to do the job. Now to generate some fake content and try it out!
Roboprog