How to manage the iCloud Document of MAC App - swift

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?

Related

Is It Possible To Set An Ics/iCal File So That The File Name Can't Be Modified? Or Even to Read Only?

This may be a foolish question but I would appreciate any pointers: I'm generating year-long .Ics files that contain unique information people are actually purchasing - Moon phases, tides, etc.
The files work fine but I would like to try and 'lock' them so that the usual suspects cannot simply, download,rename and distribute the unique content to their advantage.
Does anyone know if anything like this exists? I've tried locking the file as text and but it doesn't stick once it's resaved as ics.

Is iCloud only meant for UIDocument and CoreData(How to Take Back up of any folder with its data on iCloud)

I read the apple documentation and some other links and found there are examples of using iCloud with only either UIDocument or Core Data.
I am having a folder created in documents directory named "backUPFolder" and it contains some images and other files in it.
I want to ask , if it is possible to move this backUPFolder in iCloud with all the data exist in this folder as it is.
If yes it is possible please provide me some useful link or suggest an approach which I can follow.
My requirement is to take a back up of my data on iCloud.
Please please help me.
I am stuck here.
Any suggestions would be highly appreciated.
Thanks in advance!
I have also only seen the UIDocument and Core Data examples. What comes to mind is to transform your pics and docs into Core Data blobs and store them with core data anyway. This could also be very efficient.
Alternatively, you could check out the Dropbox APIs.
If your folder in the Documents or Library folder of your application, it will be backed up to iCloud automatically assuming the user has iCloud enabled (and that you haven't explicitly flagged the file as NSURLIsExcludedFromBackupKey).
Any manual interaction with iCloud as a developer is typically to share files explicitly between installations of your app. If your requirement is just to back up data, you don't need to do anything besides store the folder in Documents.

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

Where to store private important user data when the Documents directory is not an option?

My app is using iTunes file sharing which exposes everything in the Documents directory to the user, making it vulnerable to accidentally deletion or manipulation.
I spent hours in reading though these documents but it is a mess and I hope someone knows from experience. First, in one place they say that I should put these files in the Library directory.
In this technical Q & A Apple says that this is preserved. From my understanding this means that I can safely put important user data like sqlite3 database files in this directory. When the user updates to a new version, the content inside this directory will be preserved, it will survive and be available after the update:
applications can create their own
directories in
/Library/ and those
directories will be preserved in
backups and across updates
So /Library/ is preserved in backups and across updates.
For me with bad english this means: YES, the data will survive. It will not be lost when the user backs up. It will not be lost when the user updates. I looked up the word "preserved" in several dictionaries and I am sure it means "it will survive".
But then, there is this note in the iOS Application Programming Guide which tells something completely different! Here, they say about the Library directory:
<Application_Home>/Library/
You should not use this directory for user data files.
The contents of this directory (with the exception of the Caches
subdirectory) are backed up by iTunes.
Your application is generally
responsible for adding and removing
these files. It should also be able to
re-create these files as needed
because iTunes removes them during a
full restoration of the device.
"Should not use for user data files." (???)
But at the same time they admit it's backed up by iTunes. OK. So why shouldn't I put user data files into there, then?
/Library/Caches
Use this directory to write any application-specific support files
that you want to persist between
launches of the application or during
application updates. (...)
It should also be able to re-create
these files as needed because iTunes
removes them during a full restoration
of the device.
What?! I should put these files in Library/Caches. But this directory is not backed up by iTunes, as they said above. So this is only save for updates, but not for backup. And the data might be deleted anytime by the system.
Now this is completely confusing me. From what I understand I can choose between the evil and the devil: Data in /Library/ does not survive updates but is backed up by iTunes. Data in /Library/Caches does survive updates, but is not backed up by iTunes AND it might be deleted by the system (since it's a "Cache") anytime.
And on the other hand, the technical Q & A suggests putting this important user data in a custom subfolder in /Library/, for example /Library/PrivateDocuments.
In contrast to the iOS Application Programming Guide, the technical Q & A says: the entire /Library directory has always been preserved during updates and backups
So now, really, one of both documents MUST be wrong. But which one? What's the truth? Please, not speculation! I'm looking for answers from experience and I feel there is no way to figure this out except releasing an app and praying. Maybe someone wants to share his/her experience what really worked.
I've seen Library/Preferences (where NSUserDefaults are stored) be kept across restores, so I think most of Library is kept. The cache directories are probably excluded, though.
In general, just use the APIs to fetch paths and trust that iTunes will preserve them unless they're meant to represent temporary folders. That means you should use a subdirectory of your NSApplicationSupportDirectory named for your application:
NSArray * urls = [[NSFileManager defaultManager] URLsForDirectory:NSApplicationSupportDirectory inDomains:NSUserDomainMask];
NSAssert([urls count], #"Can't get app support directory");
NSURL * url = [urls objectAtIndex:0];
url = [url URLByAppendingPathComponent:#"MyAppName"];
In practice, this will end up being "Library/Application Support/MyAppName" in your sandbox, but you should use the API anyway to ensure this is kept future-proof.
(If you care about support for iOS 3 or 2, use the NSSearchPathForDirectoriesInDomains() function instead of the -URLsForDirectory:inDomains: method.)
The content inside Library folder (except cache) is backup by itunes.
What apple mean by "Should not use for user data files." (???) is that don't use this folder for data you want to be view in the File Sharing sytem via itunes.
So If you want the user to access the data via itunes write to /Document folder
If you want to hide the data from the user write to /Library folder
Why don't you try the keychain? If your data is not too extensive it can provide a quick way to store sensitive information
Keychain Documentation from Apple

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

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.