在iPhone上的UIWebView的文档目录中显示本地HTML文件 [英] Display Local HTML file from documents directory in a UIWebView on iPhone

查看:103
本文介绍了在iPhone上的UIWebView的文档目录中显示本地HTML文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在我的应用程序中显示来自Documents目录的HTML文件?
下面是我正在使用的代码,没有任何内容显示在我的 UIWebView中

  NSArray * paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask,YES); 
NSString * documentsDirectory = [paths objectAtIndex:0];
NSString * filePath = [NSString stringWithFormat:@%@ /%@,documentsDirectory,@Notes.html];

NSURL * url = [NSURL URLWithString:filePath];
NSURLRequest * request = [NSURLRequest requestWithURL:url];
[notesListWeb loadRequest:request];

为什么这不起作用?

我发现了其他答案,但它们不是来自文档目录。



您的帮助非常感谢。

解决方案

用文件路径创建 NSURL 对象的正确方法是:

  NSURL * url = [NSURL fileURLWithPath:filePath]; 

您还应该更改如何创建 filePath

  NSString * filePath = [documentsDirectory stringByAppendingPathComponent:@Notes.html]; 

请注意,文件名在真实iOS设备上区分大小写。该文件是否真正命名为Notes.html和notes.html?


How can I display a HTML file from the Documents directory in my app? Below is the code that I'm using and nothing is showing up in my UIWebView

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentsDirectory = [paths objectAtIndex:0];
    NSString *filePath = [NSString stringWithFormat:@"%@/%@", documentsDirectory, @"Notes.html"];

    NSURL *url = [NSURL URLWithString:filePath];
    NSURLRequest *request = [NSURLRequest requestWithURL:url];
    [notesListWeb loadRequest:request];

Why is this not working out?

I've found other answers but they aren't from the documents directory.

Your help is greatly appreciated.

解决方案

The proper way to create an NSURL object with a file path is:

NSURL *url = [NSURL fileURLWithPath:filePath];

You should also change how you create your filePath:

NSString *filePath = [documentsDirectory stringByAppendingPathComponent:@"Notes.html"];

Also keep in mind that filenames are case sensitive on a real iOS device. Is the file really named Notes.html and notes.html?

这篇关于在iPhone上的UIWebView的文档目录中显示本地HTML文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆