Apple Event: May 7th at 7 am PT

Looks like no one’s replied in a while. To start the conversation again, simply ask a new question.

open an html file locally

Hi,
I have started developing with iPhone less than one month ago. Im still following tutorials and guide books and in one of them I learnt how to open in Safari an URL with openURL.

I was just wondering if it is possible to open a local HTML file with openURL, I mean; I have a simple,static HTML file, added to my Resources folder in Xcode. Then, I try to open it using openURL but it seems that either it is not possible or I am not referencing the file properly. I have used these two options:

NSString *filePath = [[NSBundle mainBundle] pathForResource:@"test" ofType:@"html"];
NSURL *url = [NSURL fileURLWithPath:filePath];


and


- (NSString *)dataFilePath:(NSString *)fileName
{
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
return [documentsDirectory stringByAppendingString:fileName];
}
filePath = [self dataFilePath:@"test.html"];
NSURL *url = [NSURL fileURLWithPath:filePath];

With both options I get a reference to my .app file, but I want the .html file in it.
Is it possible to do what Im trying?

Thank you and regards.

Mac Mini, iPhone OS 3.1.2

Posted on Oct 21, 2009 7:47 AM

Reply
7 replies

Oct 21, 2009 7:57 PM in response to fspace

using html for things like help is great. Here is how I do it:

NSString *appDirectory = [[NSBundle mainBundle] resourcePath];
NSString *htmlPath = [appDirectory stringByAppendingPathComponent:@"terms.html"];
[termsPage loadRequest:[NSURLRequest requestWithURL: [NSURL fileURLWithPath: htmlPath]]];

we even implemented multi level html references by using links that pointed at sub html files, but you have to add the html files as a "Resource" before you can refer to them. Don't point them at a www URL or they'll never get back to your app. The local html files will automatically be carried along with your Build when you deploy your app on the store. ... Flash Gordon

Oct 22, 2009 11:52 AM in response to fspace

file:// appears to be a valid URL scheme on the simulator, but it is not on Safari Mobile. My guess is that Safari Mobile does not have access rights to the bundle's files.
Thus:

NSURL *fileURL = [NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:@"readme" ofType:@"html"]];
NSLog(@"URL is: %@",fileURL);
[[UIApplication sharedApplication] openURL:fileURL];

works on the simulator, but gets this:
Thu Oct 22 08:21:58 unknown com.apple.debugserver-43[104] <Warning>: 1 [0068/1603]: error: ::read ( 7, 0x28091c, 1024 ) => -1 err = Bad file descriptor (0x00000009)

on the real device.
I can't find it specifically in the Doc, but my guess is that this behavior is intentional.

Oct 22, 2009 12:38 PM in response to fspace

As I mentioned in my earlier reply, I do not think this is possible. All applications run in literally a "chroot jail" and are deliberately kept totally isolated from each other. There may be some level of interaction between applications distributed by Apple, since they have more direct access to the hardware, but I doubt that any of them will support accessing files within a third party application.

open an html file locally

Welcome to Apple Support Community
A forum where Apple customers help each other with their products. Get started with your Apple ID.