3

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

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

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.

Hope you can help me

1
  • if you iOS device has internet connectivity, put it on the web and open it Commented Jun 14, 2012 at 14:57

2 Answers 2

8

You cannot open a document with resides in your app bundle through Safari (please, see iOS Security Model).

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];
Sign up to request clarification or add additional context in comments.

Comments

2

i dont think you can achieve that, since both UIApplication openURL: and UIDocumentInteractionController will not open local files inside your bundle

Do the following instead, in your viewDidLoad

UIWebView *webView = [[UIWebView alloc] initWithFrame:self.view.bounds];
[self addSubview:webView];
NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL fileURLWithPath: documentPath]];
[webView loadRequest:request];

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.