为什么我的UIWebView显示大量的HTML而不是本地文件



我在我的iPhone应用程序中集成了一个"下载管理器",它允许用户在线将网页保存到他们设备上的文档文件夹中,以便离线和本地查看。这是完美的,除了文件显示了大量的HTML代码,而不是他们应该是什么样子。

这是UITableView with files: http://cl.ly/3D2g0o3C3a302J243D3v这是UIWebView与HTML在它而不是通常的网页:http://cl.ly/2s1h3H3D1c1t3y1J2G0W

我像这样加载我的UIWebView(看起来都很好?):

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

为什么会发生这种情况,我能做些什么来纠正这种情况?

使用这行代码,我的朋友,

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

这是我之前使用的代码:

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

现在,您在文档目录中有了文件路径。然后:

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

最新更新