Skip to content

Instantly share code, notes, and snippets.

@codeswimmer
Created January 2, 2013 20:01
Show Gist options
  • Save codeswimmer/4437454 to your computer and use it in GitHub Desktop.
Save codeswimmer/4437454 to your computer and use it in GitHub Desktop.
iOS: How To Access Local File Using NSURL
Use [[NSBundle mainBundle] resourceURL] to gain a URL string of your local resource.
For example you have index.html in you bundle resource directory. To create an NSURL of that local html file to display on your UIWebView. Simply do this.
NSString *resourceURLString = [[NSBundle mainBundle] resourceURL];
resourceURLString will return something like this: file:/../AppName.app/
NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:@"%@index.html",resourceURLString]];
Your URL string would look something like this: file:/../AppName.app/Index.html
You can then load that local html file to your UIWebView.
NSURLRequest *request = [NSURLRequest requestWithURL:url];
[webView loadRequest:request];
NOTE: If you want to use path to access local files. You can use this one below.
NSString *urlAddress = [[NSBundle mainBundle] pathForResource:@"index" ofType:@"html"]; //you can also use PDF files
NSURL *url = [NSURL fileURLWithPath:urlAddress];
NSURLRequest *requestObj = [NSURLRequest requestWithURL:url];
[webView loadRequest:requestObj];
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment