在 Safari 中打开生成的 html 文件 [英] Open Generated html file in Safari
问题描述
有什么方法可以在 Safari 中打开应用程序文档目录中保存的 .html 文件.我有一个 Template.html 文件,我会根据用户的选择更改该文件并将其保存到 temp.html 文件中,如何在 safari 中打开它?
Is there any way i can open a saved .html file in Documents directory of the application, in Safari. I have a Template.html file that i change depending on user choices and saving it to a temp.html file, how to open it in safari?
推荐答案
很遗憾,由于沙盒限制,您无法通过 [[UIApplication sharedApplication] openURL:] 在 iOS 的 Safari-App 中打开 File-URL.
Unfortunately you can't open File-URLs in iOS' Safari-App via [[UIApplication sharedApplication] openURL:] due to sandbox restrictments.
您有两个选择:
a) 将该文件上传到服务器,然后使用 [[UIApplication sharedApplication] openURL:@"http://..."] 在 Safari-App 中打开它;
a) Upload that file to a server and then open it in the Safari-App with [[UIApplication sharedApplication] openURL:@"http://..."];
b) 在您的应用中嵌入一个 Safari-WebView 来显示 html 文件,如下所示:
b) Embed a Safari-WebView in your app which displays the html-file, like so:
UIWebView *webView = [[UIWebView alloc] initWithFrame:...];
[self.view addSubView:webView];
NSURL *fileURL = [NSURL fileURLWithPath:...];
NSURLRequest *request = [NSURLRequest requestWithURL:fileURL];
[webView loadRequest:request];
self 在这里是一个 UIViewController.
self is a UIViewController here.
编辑
要添加具有适当逻辑的共享按钮,您可以将包含 UIWebView 的 ViewController 放入 UINavigationController 中,并将 UIBarButtonSystemItemAction 类型的 UIBarButtonItem 添加到它的 NavigationBar.如果 BarButtonItem 被点击,您会显示一个 a UIActivityViewController,像这样:
To add a Share-Button with proper logic, you can put the ViewController, that contains the UIWebView, inside a UINavigationController and add a UIBarButtonItem of type UIBarButtonSystemItemAction to it's NavigationBar. If the BarButtonItem gets tapped, you show a a UIActivityViewController, like so:
- (void)viewDidLoad {
[super viewDidLoad];
UIBarButtonItem *shareButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAction target:self action:@selector(shareButtonTapped:)];
self.navigationItem.rightBarButtonItem = shareButton;
}
- (void)shareButtonTapped:(id)sender {
UIActivityViewController *activityViewController = [[UIActivityViewController alloc] initWithActivityItems:... applicationActivities:nil];
[self.navigationController presentViewController:activityViewController animated:YES completion:nil];
}
有关使用 UIActivityViewController 的更多信息,请参阅 NSHipster
For more Infos on using UIActivityViewController, see NSHipster
这篇关于在 Safari 中打开生成的 html 文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!