tags:

views:

184

answers:

3

I have HTML files that I want to load locally. I have included the files in the resources folder in XCode. I am not sure what the syntax is to load them.

This is the code I use to connect to say google.

NSString *urlAddress=@"http\\someurl";
NSURL *url = [NSURL URLWithString:urlAddress];
NSURLRequest *requestObj = [NSURLRequest requestWithURL:url];
[webHelpView loadRequest:requestObj];

Could someone provide an example where a HTML file is loaded locally.

Thanks in advance.

+1  A: 

Can't you just do

NSURL *url = [NSURL URLWithString:@"file:///path/to/file"];

?

phunehehe
A: 

Sure, here is some code from an app that loads a locallly stored html page into a web view:

- (void) viewDidLoad
{
    // Load the content into the view
    NSString* path = [NSString stringWithFormat: @"%@/index.html",
        [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex: 0]];
    [webView_ loadRequest: [NSURLRequest requestWithURL: [NSURL fileURLWithPath: path]]];
}
St3fan
Mmmmmm...not working. Although I am not sure where I should specify where the HTML file should go?? In XCode, I choose copy in build process for the file.
jp chance
If the document is part of your app bundle then you will need to use `[[NSBundle mainBundle] pathForResource...` to get a path to it.
St3fan
I am using this: NSString *path = [[NSBundle mainBundle] pathForResource:@"SettingsHelp.html" ofType:@"html"];Still it is not finding the resource. Mmmm....
jp chance
I don't want to be rude, but really, read the documentation and re-read your code. Hint, you got .html in there twice.
St3fan
+1  A: 

This is the only code that worked for me.

NSString *path = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:@"SettingsHelp.html"];        
NSURL *url = [NSURL fileURLWithPath:path isDirectory:NO];
NSURLRequest *request = [NSURLRequest requestWithURL:url];

Thanks all for the help.

jp chance