Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

UIWebView load HTML file from local file system

How can I load a HTML from the local file system?

I have seen many questions like this but they are all referring to a file that is within a bundle. I would like to load from a path such as/Users/Abs/Documents/ - I think the ios simulator has access to this as I can save files to my local /Users/Abs/Documents/ directory.

But I don't know how to load a HTML file to the UIWebView.

How can I edit the below to achieve this:

[web loadRequest:[NSURLRequest requestWithURL:[NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:@"index.html" ofType @"html"]isDirectory:NO]]];
like image 427
Abs Avatar asked Dec 01 '25 08:12

Abs


2 Answers

use below code it will work

NSString *filePath=[[NSBundle mainBundle] pathForResource:@"index" ofType:@"html"];
 [webView loadData:[NSData dataWithContentsOfFile:filePath] MIMEType:@"text/html" textEncodingName:@"UTF-8" baseURL:nil];
like image 102
Narayana Avatar answered Dec 04 '25 00:12

Narayana


NSURL *url = [NSURL URLWithString:@"/Users/Abs/Documents/index.html"];
NSURLRequest *req = [NSURLRequest requestWithURL:url];
[web loadRequest:req];

Note that this is only useful when working in the simulator, because apps on a device are sandboxed and can only get to files in their bundle.

like image 38
Daan van Hasselt Avatar answered Dec 04 '25 01:12

Daan van Hasselt



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!