How can we load our own html file into the UIWebView?
-
http://stackoverflow.com/questions/7063276/how-to-load-local-html-file-into-uiwebview-iphone/24159166#24159166 – Govind Jun 11 '14 at 09:24
7 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]]];

- 239,200
- 50
- 490
- 574
-
Apple's documentation suggests that using this method to load local HTML is not secure: ```To help you avoid being vulnerable to security attacks, be sure to use this method to load local HTML files; don’t use loadRequest:.``` I've recorded their suggestion (`loadHTMLString:baseURL:`) in my answer below. – siege_Perilous May 26 '16 at 22:11
-
Yeah, it didn't say that 5 years ago when I posted this answer. Always good to read the documentation. I'd delete this, but it's the accepted answer and has obviously helped a lot of people. – Cody Gray - on strike Dec 02 '16 at 21:24
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.

- 7,579
- 2
- 37
- 63

- 38,189
- 13
- 98
- 110
-
You're right but I would pass `[NSURL fileURLWithPath:[[NSBundle mainBundle] bundlePath]]` instead of `nil` for `baseURL`. – Jilouc Jan 10 '11 at 10:43
-
Ah yes - I'd not remembered about loading other resources from the html. I've edited my answer, thanks! – deanWombourne Jan 10 '11 at 12:47
-
Swift
guard let path = NSBundle.mainBundle().pathForResource("index", ofType: "html") else {
return
}
let url = NSURL(fileURLWithPath: path)
self.webview.loadRequest(NSURLRequest(URL:url))

- 4,065
- 2
- 38
- 50
By using following code you can load an html in an WebView.here web is web view obj and inde
Web.delegate = self;
[Web loadRequest:[NSURLRequest requestWithURL:[NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:@"index" ofType:@"html"]isDirectory:NO]]];

- 1,139
- 12
- 18
Apple's documentation suggests using loadHTMLString:baseURL:
:
Use the loadHTMLString:baseURL: method to begin loading local HTML files or the loadRequest: method to begin loading web content. Use the stopLoading method to stop loading, and the loading property to find out if a web view is in the process of loading.
The loadHTMLString:baseURL:
documentation offers further reasoning:
To help you avoid being vulnerable to security attacks, be sure to use this method to load local HTML files; don’t use loadRequest:.
This one might help: https://stackoverflow.com/a/29741277

- 1
- 1

- 1,109
- 9
- 8
Swift Version 2.1
// load html to String with Encoding
let path = NSBundle.mainBundle().pathForResource("policy", ofType: "html")
do {
let fileHtml = try NSString(contentsOfFile: path!, encoding: NSUTF8StringEncoding)
webView.loadHTMLString(fileHtml as String, baseURL: nil)
}
catch {
}

- 7,073
- 7
- 61
- 71
NSString *htmlFile = [[NSBundle mainBundle] pathForResource:@"privacy-Policy" ofType:@"html" inDirectory:nil];
NSData *htmlData = [NSData dataWithContentsOfFile:htmlFile];
[webView loadData:htmlData MIMEType:@"text/html" textEncodingName:@"UTF-8" baseURL:[NSURL URLWithString:@""]];

- 7
- 7