3

我有一个 HTML 文件保存在这样的临时目录中:

NSFileManager *fileManager = [NSFileManager defaultManager];
NSString *documentDirectory = NSTemporaryDirectory();
NSString *documentPath = [documentDirectory stringByAppendingPathComponent:@"mydocument.html"];
[fileManager createFileAtPath:documentPath contents:myHTMLDocumentData attributes:nil];

该文档是在我的临时文件中创建的。之后,我想在 Safari 中打开此文档,但它不起作用:

NSURL *url = [NSURL fileURLWithPath:documentPath];
[[UIApplication sharedApplication] openURL:url];

屏幕上什么都没有发生,没有错误......但是,如果我将“url”替换为@“http://google.fr”,Safari 将使用 google.fr 启动,我可以通过键入 url 访问我的临时文件“文件://localhost ..../myHtmlDocument.html”在 Safari 中。

希望你能帮我

4

2 回答 2

8

您无法通过 Safari 打开包含驻留在应用程序包中的文档(请参阅iOS 安全模型)。

您需要的是使用 ;UIWebView在您的应用程序中显示您的文档内容– loadHTMLString:baseURL:;例如:

UIWebView* webView = [[[UIWebView alloc] initWithFrame:rect] autorelease];
[webView loadHTMLString:myHTMLSource baseURL:nil];
[self.view addSubview:webView];
于 2012-06-14T14:46:57.780 回答
2

我认为您无法做到这一点,因为两者UIApplication openURL:UIDocumentInteractionController都不会在您的捆绑包中打开本地文件

请改为执行以下操作,在您的viewDidLoad

UIWebView *webView = [[UIWebView alloc] initWithFrame:self.view.bounds];
[self addSubview:webView];
NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL fileURLWithPath: documentPath]];
[webView loadRequest:request];
于 2012-06-14T14:47:17.983 回答