Open Generated html file in Safari

2019-09-16 14:52发布

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?

标签: ios xcode safari
1条回答
爷的心禁止访问
2楼-- · 2019-09-16 15:01

Unfortunately you can't open File-URLs in iOS' Safari-App via [[UIApplication sharedApplication] openURL:] due to sandbox restrictments.

You have 2 options:

a) Upload that file to a server and then open it in the Safari-App with [[UIApplication sharedApplication] openURL:@"http://..."];

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 is a UIViewController here.

EDIT

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

For more Infos on using UIActivityViewController, see NSHipster

查看更多
登录 后发表回答