我有一个 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“file://localhost..../”来访问我的临时文件Safari 中的 myHtmlDocument.html”。
希望你能帮助我
最佳答案
您无法通过 Safari 打开应用程序包中驻留的文档(请参阅 iOS Security Model)。
您需要使用 UIWebView
在您的应用程序中使用 – loadHTMLString:baseURL:
显示您的文档内容;例如:
UIWebView* webView = [[[UIWebView alloc] initWithFrame:rect] autorelease];
[webView loadHTMLString:myHTMLSource baseURL:nil];
[self.view addSubview:webView];
关于Objective-c/iOS - 使用 Safari 打开本地 html 文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11035564/