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