Objective-c/iOS - Open local html file with Safari

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

Answers


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

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

Need Your Help

Why does developing a firefox extension involve installing an sdk?

google-chrome firefox google-chrome-extension firefox-addon firefox-addon-sdk

I'm currently trying to port a Chrome extension into a Firefox add-on, but the development process is incredibly different, and the part in particular that I find confusing is why I should have to

How to manage Thread Local Storage (TLS) when using TPL?

.net asynchronous threadpool task-parallel-library thread-local-storage

I want to store logging context information in TLS so that I can set a value at the entry point, and have that value available in all resulting stacks. This work well, but I also using TPL and the