Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I load a local HTML file into the UIWebView?

How can we load our own html file into the UIWebView?

like image 705
RAMAN RANA Avatar asked Sep 11 '25 02:09

RAMAN RANA


2 Answers

The following code will load an HTML file named index.html in your project folder:

[WebView loadRequest:[NSURLRequest requestWithURL:[NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:@"index" ofType:@"html"]isDirectory:NO]]];
like image 171
Cody Gray Avatar answered Sep 12 '25 15:09

Cody Gray


Cody Gray is right but there's also this way :

// Load the html as a string from the file system
NSString *path = [[NSBundle mainBundle] pathForResource:@"index" ofType:@"html"];
NSString *html = [[NSString alloc] initWithContentsOfFile:path encoding:NSUTF8StringEncoding error:nil];

// Tell the web view to load it
[WebView loadHTMLString:html baseURL:[[NSBundle mainBundle] bundleURL]];

This is useful if you need to edit the html before you load it.

like image 20
deanWombourne Avatar answered Sep 12 '25 15:09

deanWombourne