Objective-c / iOS - 用Safari打开本地html文件 [英] Objective-c/iOS - Open local html file with Safari

查看:1149
本文介绍了Objective-c / iOS - 用Safari打开本地html文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have an HTML file save in a temporary directory like that :

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

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

The document is created in my temporary file. After it, I want to open this document in Safari but it doesn't work :

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

在屏幕上没有发生任何事情,没有错误...
然而,如果我替换url by @http://google.fr,Safari是用google.fr启动的,我可以通过在Safari中输入urlfile://localhost..../myHtmlDocument.html来访问我的临时文件。

Nothing happen at screen, no error... However, if I replace "url" by @"http://google.fr", Safari is launched with google.fr and I can access to my temporary file by typing the url "file://localhost..../myHtmlDocument.html" in Safari.

希望您能帮助我。

推荐答案

通过Safari驻留在您的应用程序捆绑包中(请参阅 iOS安全模型)。

You cannot open a document with resides in your app bundle through Safari (please, see iOS Security Model).

您需要的是使用 UIWebView 使用 - loadHTMLString:baseURL:在应用程式中显示文件内容;例如:

What you need is using an UIWebView to display your document content inside your app using – loadHTMLString:baseURL:; e.g.:

UIWebView* webView = [[[UIWebView alloc] initWithFrame:rect] autorelease];
[webView loadHTMLString:myHTMLSource baseURL:nil];
[self.view addSubview:webView];

这篇关于Objective-c / iOS - 用Safari打开本地html文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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