Reputation: 2383
I'm trying to make an iOS app that allows the user to create an unlimited amount of HTML document and save them into the documents folder of the app. This is sort of like a combo question but how can I display a file that is in the documents dictionary.
So my two question are:
How to display HTML file that are in the document dictionary
And
How to allow the user to created unlimited folders and html files
Sorry for the double question...
Upvotes: 0
Views: 1647
Reputation: 7226
First, you need to know the path to your documents directory:
NSArray* paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsPath = [paths objectAtIndex:0];
You can create documents and directories there up to the limits of the device storage. Be aware, however, that only files in the root will show up in iTunes Document Sharing.
So, if you have a string htmlContent
and a string htmlFilename
, you can save it. Do pay attention to the encoding you're using; the encoding in the HTML header and meta tags (if present) should match the encoding used here.
NSString *htmlPath = [documentsPath stringByAppendingPathComponent:htmlFilename];
[htmlContent writeToFile:htmlPath atomically:NO encoding:NSUTF8StringEncoding error:nil];
You can display HTML in a UIWebView
. Assuming you have one called webView
, you can display any NSURL
object in it:
NSURL *stackOverflowAddress = [NSURL URLWithString:@"http://www.stackoverflow.com"];
[[self webView] loadRequest:[NSURLRequest requestWithURL:stackOverflowAddress]];
This can include the file you just wrote:
NSURL *htmlURL = [NSURL fileURLWithPath:htmlPath];
[[self webView] loadRequest:[NSURLRequest requestWithURL:htmlURL]];
Or, you can put a string into it directly:
[[self webView] loadHTMLString:htmlContent baseURL:nil];
If you use a UIWebView
for a lot of your functionality, take the time to read the UIWebViewDelegate Protocol Reference.
It is also possible to tell your application to open URLs elsewhere. This will usually result in Safari opening. However, because Safari does not have access to your app's document directory, it cannot display documents while they are saved locally.
[[UIApplication sharedApplication] openURL:stackoverflowAddress];
Upvotes: 4
Reputation: 43330
I'm trying to make an iOS app that allows the user to create an unlimited amount of HTML documents and save them into the documents folder.
HTML files are simply text (as most code-related files are), so of you wish to get it's contents as a string, use NSString's +stringWithContentsOfFile
.
How to allow the user to created unlimited folders and html files
As for writing, so long as you write your string with the path extension .html, it will be a perfectly valid HTML file.
EDIT, the path to the documents directory can be found with
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0]; // Get documents directory
How to display HTML file that are in the document dictionary
HTML files may be displayed in UIWebViews see this related question for an example.
Upvotes: 0