Avba
Avba

Reputation: 15286

Can not load file using [[NSBundle mainBundle] pathForResource ofType inDirectory]

I have a directory with a website I need to add to my project. I will load it using a uiwebview.

I've tried several ways of adding it, all unsuccessful. This is the last way I tried:

Copied the whole website directory to my project directory (the actual physical folder). After that, dragged the directory into my xcode project, and ticked "Create folder references for any added folders" Then I looked at "build phases -> copy bundle resources ->..." and I see that it is there

When loading my webpage:

-(void)loadUrl
{
NSString *str = [[NSBundle mainBundle] pathForResource:@"index" ofType:@"html" inDirectory:@"websiteDir"]; // THIS IS RETURNING NIL!
    NSURL *url = [NSURL URLWithString:str];
    NSURLRequest *request = [NSURLRequest requestWithURL:url];
    [self.webView loadRequest:request];

}

What am i doing wrong?

Upvotes: 0

Views: 2491

Answers (1)

Daij-Djan
Daij-Djan

Reputation: 50119

this is wrong:

you get str as path BUT then call URLWithString... but str is no URLString but a path

EITHER use url = [NSURL fileUrlWithPath:str]

OR use the method - (NSURL *)URLForResource:(NSString *)name withExtension:(NSString *)extension subdirectory:(NSString *)subpath


sample

 -(void)loadUrl {
    NSURL *url = [[NSBundle mainBundle] URLForResource:@"index" withExtension:@"html" subdirectory:@"websiteDir"];
    NSURLRequest *request = [NSURLRequest requestWithURL:url];
    [self.webView loadRequest:request];
}

Upvotes: 1

Related Questions