Strategy to keep track of stored files in the documents directory? - iphone

In my app, the user can save his input to disk. This is done with NSKeyedArchiver. Currently I simply name my files with a timestamp. But of course, the user may want to load one of them back in to keep on editing them.
What would be the most reliable / safe strategy to keep track of those files? I need to present the user a list of those files, so that he can choose one to open.
Currently I think of making an archived NSMutableArray which simply stores the file names, but I feel that this strategy is not really good. We all know when we save files sometimes something goes wrong, and this seems very likely to get currupted easily, or not? How would you do it?

If you've got all your files in one directory, and nothing else there, I would use -[NSFileManager contentsOfDirectoryAtPath:error:] to fetch an array of filenames in there. That's a little more robust than tracking these manually.

Related

How to manage the iCloud Document of MAC App

The documents provided by APPLE did not make it clear to me. This may be my English level limit. I hope you can help me to get a better understanding of it.
I use the FileManager class to manage the files of icloud, including the methods of copyItem, moveItem, and removeItem. I directly deal with the files in the icloudDocument's address as an ordinary file. After matching with NSMetadataQuery, I compare the modification time and creation time of files, so I managed files, but I found it very bad, because I need to judge whether files need updating, uploading or deleting.
I later found that the FileManager class has Managing iCloud-Based Items, which includes the evictUbiquitousItem, startDownloadingUbiquitousItem, and other methods. It seems to be easy to manage, but it doesn't make me clear.
My problems are now:
which is the final file when the local file is not consistent with the icloud file? (there is no convenient method, automatic synchronization)
what should I do if the icloud does not exist, but when the local file exists? (delete it directly? Or is it necessary to judge?)
how do you know which files are downloaded to the local?

coredata as front end and text files as backend

How does this sound conceptually.
I wanted to store some text and add tags to it thats easily retrivable. Coredata is the obvious solution, but i also needed that data across the cloud, not just iCloud like dropbox. So I thot i'd use Coredata + textFiles.
obvious approach
use coredata with two entities. One for text and one for tags. Works awesome BUT not ready for syncing.
1. using icloud coredata combo will hurt me badly, its still unreliable, and i cant afford to keep stabilising it.
2. i dont just have ios devices, need it on my computer too.
the solution
Add text in a simple textview.
Add tags to the text as well using some kinda delimiters.
Save the document as a text file (that includes tags) and give the document some unique name
Put that file in dropbox or icloud or whatever as a document But Also, parse it locally in the iOS app so that the text in file is separated into text and tags each of which enters its entity in coredata.
advantage for the solution
I can use the text in a useful way locally (in iphone) and if needed will get those text files from the cloud.
problem with the solution
Data in the cloud (as textfiles) is only so useful. But nonetheless, its there, i can live with this.
SYNCING: how do i make sure that each file is synced appropriately. Im not sure I should use UIManagedDocument? I'm already using coredata locally, dont know how i'd complicate things if i use UIManagedDocument.
My question is, im confused about the syncing and saving part, what should i do to keep it neat and clean.
While writing this question i feel like I screwed up the whole idea.

Valid file types for iCloud?

I find many similar question but i didn't get solution for this.
Is it possible to upload some file like image, document, zip file to upload on iCloud programmatically?
See table 4-1 in the documentation:
How do you manage the data? Manage files and directores using the
NSFileManager class. Open, close, read, and write files using standard
file system routines.
So if you can create a file, you can store it in iCloud. But remember there's a finite, relatively small amount of space available.
iCloud can handle all kinds of files. So if you want rot use an obscure file format or invent your own, go for it. It only can to be converted into a byte stream/NSData, but then again, what isn't?
Check this tutorial walkthrough app. It shows how to create, modify and delete files for iCloud.
http://github.com/lichtschlag/iCloudPlayground

Saving, reading, exchanging own file format?

any recommendations for saving four strings into an own file format that is read by the application and can be shared?
In my app, you will be able to enter some text in some boxes and the app shows a view with an background image and those strings. Now, I am already able to save this as a picture, but I actually want to save it to an own file format so that you can save different files that can be modified afterwards as well or even exchanged via email and opened from another iphone with the app.
Now, I wrote the code for accessing the documents folder of the app, as well as saving and deleting. Thing is, i dont know how to store those strings in a file (perhaps in a xml?) and read them easily afterwards from my application.
For the exchanging part, I found that link which would be a nice feature indeed: http://iosdevelopertips.com/cocoa/launching-your-own-application-via-a-custom-url-scheme.html
Parsing xml seems not that difficult (never done it before): http://ipad.about.com/od/iPad-App-Dev/a/How-To-Parse-Xml-Files-In-Xcode-Objective-C.htm
If it's only a small bit of infomation then the easiest way to store your data in a file would be using a plist - there's a good tutorial here - http://www.icodeblog.com/2009/02/14/loading-data-from-plist-files/
In addition to the plist, you could also do the following approaches:
1) simplest - open a file in your documents directory, write the 4 strings (using whatever delimiter/end of string marker is useful - carriage return?) and overwrite them each time through. (i.e. it's your file, you can format it how you like)
2) mildly harder - use Apple's NSArchive to pack and unpack the strings.
3) possible overkill - store them directly in a SQLite database
4) major overkill - store them in CoreData.
Of course, the "overkill" options also provide you with extra features which may be of use if your app functionality extends beyond what you've outlined.
By sharing, I would think that simple copy and paste might be enough, but there's also sending it via email, or tripping another app's URL scheme to make it open it and sending the strings as part of the URL. You'd have to know that the other app would be able to interpret your strings as part of the URL, so you might have to write it yourself.
Okay guys I found that very nice method in the NSString documentation:
–writeToFile:atomically:encoding:error:
I think I am gonna separate my strings by /n and save them to a .txt. When I am gonna read the file afterwards, i am getting the string, divide it to substrings. That will do it, I guess.
One last question: Is it possible to replace the content of my file so that I won't need to create a new file every time i want to change something?
Thanks!

Prevent Save As Functionality

I need to prevent a document from being saved / saved as (say from ms word). I've looked around and I havn't quite found a satifying answer. I've considered EFS... but I don't think it prevents the user from saving the document as... (though it prevents access to the original source file). Any ideas outthere?
Run it on completely locked-down system with read-only disk, no network and no removable drives. Access to the computer must be phisically restricted as well.
This should prevent Save As from working, but still won't prevent document from being copied (someone may take photo of the screen and OCR it).
It usually isn't worth it to disable Save As, because you need access to the original in order to open it in the first place. There are always ways to copy a file.
I had the same issue come up when someone asked me to disable Save As for a PDF.
There is no way take an arbitrary document and modify it in such a way that it cannot be saved. You could modify a program such as Word not to allow Save or Save As but that would still allow someone to open and save the document with a different program or on a different machine.