How to integrate iCloud on a non Document-based app? - ios

I've read the Apple docs and Ray Wenderlich's tutorial. It seems that I'm forced to use UIDocument so I read it up in the docs. I've found that it's effective to use Document-based approach.
My problem is I don't want to be tied in techniques specific to the platform (iOS) thus my app has its own models made from scratch that only inherit from NSObject. This includes saving/loading.
Now, I need to integrate iCloud along with my old models. How will I do it in an elegant/effective way where I get to keep my old model implementation and be able to integrate iCloud?

You are not in any way forced to use UIDocument. You can use iCloud via NSFileManager and NSMetadataQuery. The general approach is:
When creating files
Create the file locally, as normal without iCloud
Use -[NSFileManager setUbiquitous:itemAtURL:destinationURL:error:] to transfer the file to iCloud storage.
(if necessary) Check on upload progress using NSMetadataQuery or by polling URL resource values.
When opening files
Use NSMetadataQuery to locate iCloud-resident files
Use -[NSFileManager startDownloadingUbiquitousItemAtURL:error:] to begin download or to synchronize the local copy with the cloud copy.
Check on upload progress using NSMetadataQuery or by polling URL resource values with [NSURL resourceValuesForKeys:].
When editing files
Use NSFileCoordinator to coordinate your file access with the ubiquity daemon.
Use NSFilePresenter to get notifications of changes to files.
This is all covered in sessions from WWDC 2012 (and maybe 2011, I don't recall), and the classes and methods you'll need are all in the iOS documentation.

Related

What is the safest directory in iOS which can be used to download images/pdfs? [duplicate]

Currently i was saving my application data (Media) to the CacheDirectory i.e
/var/mobile/Applications/BEAFC76C-C450-4A3A-9765-A0385A9580F3/Library/Caches
and things were going fine. But recently i got a bug report that the application data has been deleted. When i searched over it, i got this Apple Doc. According to it, DocumentsDirectory should be the ideal place to store the User/Application data.
Put user data in the /Documents/. User data is any
data that cannot be recreated by your app, such as user documents and
other user-generated content.
And Cache should not be used to store the User Data that could not be reproduced by the application.
Put data cache files in the /Library/Caches
directory. Examples of files you should put in this directory include
(but are not limited to) database cache files and downloadable
content, such as that used by magazine, newspaper, and map apps. Your
app should be able to gracefully handle situations where cached data
is deleted by the system to free up disk space.
What should be the ideal place to store it.
EDIT:
I have an application that allows user to store Videos and Photos in the application. For that i used CacheDirectory. But i am getting bug reports that the Data (Videos/Photos) is getting deleted. What conclusion i draw is that the data is being getting delete by the Device itself in order to provide space.
Secondly i also wanna give the iTunes sharing function. So only the particular files has to be stored in the DocumentsDirectory. Some files can never be exposed and some has has to be shared. What should be the ideal way to store the files.
Use Documents (NSDocumentDirectory) for files you wish to share via iTunes.
Use Application Support (NSApplicationSupportDirectory) for files you wish to hide from the user but still be backed up and never deleted by the OS.
Starting iOS 5, Apple says that it's no longer a good thing to save all kind of files in Documents Directory - if you do that, your app will be rejected for sure because this folder is backed up to iTunes & iCloud, unless otherwise specified.
It says that we should save files into Caches or Tmp Directory - these won't be backed up, but it's not a good thing to do because files from these directories can disappear if low memory happens.
So I think the best think to do is to save the important files that you need all the time in your app into Documents Directory and mark them not to be backed up, like this.
Library/Application Support Folder is the folder you should be using.
This directory doesn't always exist, and thus you may need to create it.
You can enable or disable whether you want to backup this data with iTunes or not.
This data is not accessible even if you enable file sharing. Only data that you put in Document directory would be shared with iTunes sharing, so you can still protect your data and get it backed up as well. Apple's documentation

UIDocumentPickerViewController Limitations, Multiple Files selection at once

In order to be able to access iCloud Drive from the application,
I think that we want to use UIDocumentPickerViewController from the application.
but i found following problems when i use UIDocumentPickerViewController.
Cannot upload multiple files at once.
Cannot Download multiple files at once.
When pushViewController from navigationController then display
become strange.
I want to avoid above problems, So is there any another way to get files information from iCloudDrive without using UIDocumentPickerViewController?
like Send some request or query.
I have searched lot and didn't find any query or request to get Files ,Upload files and download files from iCloudDrive.
if you have any idea about this please tell me.
Thanks,
I don't think there's any straightforward alternative, but you could think of...
...using iCloud directly (not iCloud Drive), but then users will only have access to their files from your application
...using Google Drive's sharing extension which supports uploading multiple files at once (the Dropbox SDK probably supports that, too, but their sharing extension doesn't)
...zipping all files before uploading them
...changing the file format so that it's a bundle of multiple files, if you are in control of the file format
...file a radar/feature request, and possibly wait forever ;)
Not sure if that helps, but I don't think you have much of a choice here.

iOS list document from directory download?

I want to allow users of my app to send PDF which is in the device.
The problem is, I don't see how to do that...
First, where are the files downloaded ? In which directory ? And how to list these files ?
I see there is NSfileManager but I don't understand how to use that.
I want something like DocumentPicker. (but available for iOS 7)
There is no common central directory in iOS that stores PDF files in way you describe. Nothing.
Each app has its own Documents folder, and apps have absolutely no way to see each others docs folder. So you, as a new pdf manager custom written app will not be able to look into other apps docs folder.
NSFileManager is a standard Cocoa class the handles files in a generic manner.

How can I use iCloud to synchronize a UIdocument files (saved in local document directory) between my apps?

I have an application that is saving the couple of UIDocumnet files in local document directory and display the UIdocument data.
I want to sync this document directory folder (Each and every file saved locally) with icloud on Button Press Event. I am not able to find any support for synchronizing locally saved document with icloud. I am scratching my head since last few days.
Any help would be much appreciated thanks!
Apple provides extensive documentation on this topic, and it's all built in to Xcode. A good starting point is Managing the Life Cycle of a Document. The basic steps are:
Create the document locally
Use NSFileManager to move the document to iCloud
Use NSMetadataQuery to find existing iCloud documents.
Use NSFileManager again to download documents from iCloud.
There are also numerous WWDC sessions on the topic over the past 2-3 years, available to anyone with a current Apple developer account.

Check if UI(Managed)Document is already in iCloud via URLForUbiquityContainerIdentifier+Path instead of NSMetadataQuery?

I have a quite simple shoebox-style iOS app with 1 single Core Data database (as a UIManagedDocument) and thought about trying to add iCloud support.
I of course have to check if there is already an existing database in the cloud *before creating a new UIManagedDocument at startup*, saving/opening it, etc.
As i already know the filename and that there's either 1 document or no document at all, I didn't really get if I had to
start a NSMetaDataQuery with a predicate for the exact filename
and then get the fileURL from the result (and download it
explicitly?) and open it if there is one, or
just use [[NSFileManager defaultManager] fileExistsAtPath:self.iCloudDBURL]
with iCloudDBURL created from URLForUbiquityContainerIdentifier + appending ? Is this URL only a local one and doesn't check the "real" cloud automatically?
I know the use of UIManagedDocument might not be the "right" way for this kind of app, but I thought it'd easier and I could try..
You need to use the NSMetadataQuery approach.
When using iOS on iCloud, documents don't download automatically-- they only download when you ask for them. Using NSFileManager as you suggest would only tell you if the file existed on the local device. But, the file might exist up in the cloud, not downloaded locally yet. If you use NSMetadataQuery you can find out if the document exists anywhere, even if it's in the cloud and not actually downloaded yet. You can find out about the document if it was created on a different device. This also covers the case where the user deletes and reinstalls the app, but doesn't delete cloud data-- you find out if it exists even though it's not downloaded.
Since you're using UIManagedDocument you shouldn't need to make a specific download call-- it will handle that for you when you open it.

Resources