uiwebview snippets

How to load a local file into UIWebView

Tagged iphone, uiwebview  Languages objectivec

First create index.html. Next add it to the Resources folder by right-clicking and selecting Add existing files from the menu.

Now you can display the file in a UIWebView with the following code:

[self.webView loadRequest:[NSURLRequest requestWithURL:[NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:@"index" ofType:@"html"]isDirectory:NO]]];

Via http://www.iphonedevsdk.com/forum/iphone-sdk-development/1784-how-can-i-access-local-html-file-using-uiwebview.html

How to load a local file into a UIWebView

Tagged file, iphone, uiwebview  Languages objectivec

First you'll need to setup a UIWebView in for example Interface Builder. Then in the controller you add this incredible piece of code:

NSString *path = [[NSBundle mainBundle] pathForResource:@"about" ofType:@"html" inDirectory:@"html"];
NSLog(@"%@", path);
NSURL *url = [NSURL fileURLWithPath:path];
NSURLRequest *request = [NSURLRequest requestWithURL:url];
[webView loadRequest:request];

This beautiful piece of code will look for a file named about.html in a folder called html. Now go create that folder and the file with your favorite tools.

Next drag that folder under your application in Groups & Files in XCode. Now to make XCode happy you have to make sure you select Create Folder References for any added folders.

That's it. If you get this error it means the file couldn't be found, and that you didn't import the files correctly:

Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '*** -[NSURL initFileURLWithPath:]: nil string parameter'