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];