简体   繁体   中英

Why is my UIWebView showing lots of HTML instead of the local file?

I have a "download manager" integrated into my iPhone App which allows the user to save a webpage online to their documents folder on their device to view offline and locally. This works perfectly, except the files show with lots of HTML code rather than what they should look like.

Here's the UITableView with files: http://cl.ly/3D2g0o3C3a302J243D3v Here's the UIWebView with the HTML in it instead of the usual webpage: http://cl.ly/2s1h3H3D1c1t3y1J2G0W

I'm loading my UIWebView like this (looks all fine?):

NSURL *url = [[NSURL alloc] initFileURLWithPath:documentsDirectory]; 
NSURLRequest *req = [[NSURLRequest alloc] initWithURL:url];
[webView loadRequest:req];

Why is this happening and what can I do to rectify this?

Use this line of code my friend,

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

and here is a code that i use before:

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *appSettingsPath = [documentsDirectory stringByAppendingPathComponent:@"index.html"];

now, you have the file path in your document directory. then:

[webview loadHTMLString:SiteString baseURL:[NSURL fileURLWithPath:appSettingsPath]];

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM