Objective-c/iOS - 使用 Safari 打开本地 html 文件

标签 objective-c ios cocoa-touch safari

我有一个 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/

相关文章:

iphone - 读取 iPhone 上安装的证书

ios - 关闭 View Controller 后约束崩溃

ios - 如何让 UITextField 根据所使用的 iPhone 适当调整其大小?

objective-c - 静默短信检测IOS

ios - 方法未被调用

ios - 更改了子字符串的颜色,但字体设置已重置

objective-c - 为什么我不必释放这些对象?

iphone - ABPersonViewController 不保存更改

objective-c - Cocoa 中的 keyUp/keyDown 问题

objective-c - 如何以编程方式创建多个窗口?