Store files permanently in iOS device - ios

I need to store files(text/pdf) to the device memory permanently. If I write to documents directory, my files get erased once my app is uninstalled. So I need an isolated directory, like the gallery. Can somebody help me with this?

Since Apps live in Sandboxes you can not store files outside the AppĀ“s Sandbox. You could use iCloud or another Backup System and from there the user could restore the files when the App was deleted.

As far as I know, you can't, and that is for a good reason. There would be no way for the user to control how much memory you are using, nor a way for them to free that memory. Your documents should be removed when the app is removed. If you need persistent storage, store it yourself, on a remote server; don't try to pollute the user's device.

You should take a look at the UIDocumentInteractionController
https://developer.apple.com/library/ios/documentation/UIKit/Reference/UIDocumentInteractionController_class/
As far as I know this is the only way.

Related

How to force iCloud file to be synchronized?

I'm using iCloud drive to synchronize each devices of a user and it works relatively well except sometime when a file is not direclty uploaded to iCloud Drive. To unlock this situation, I have to open the "Files" folder on the device which send the file and it looks as it force synchronization because then all the other devices detect the change and it works.
A simple solution is to find how to force iCloud files synchronization ?
I didn't find any clean solutions, may someone have found a hack to ask iCloud to synchronize files ?
Thank's a lot

(iCloud) How to programmatically Backup/Restore data for all Apps like iTunes does?

I'd like to backup and restore files (or other type of informations like key-value, SQL-like transaction based info.) for NOT ONLY my app but also all the other Apps using iCloud.
I'm just new to iCloud and read only a few documents but it seems iCloud APIs are just for single App boundary (of course I understand there is an option for sharing information between 'Group' of applications).
What I want to do is backup and restore almost everything. How can I do that?
(There some apps doing this like
http://www.copytrans.net/copytranscontacts.php
http://www.macrumors.com/2012/05/18/elcomsofts-phone-forensics-software-offers-near-real-time-access-to-icloud-backups/)
I checked that the some of the Mobile App files are stored inside the "~/Library/Application\ Support/MobileSync/Backup" directory but there are only some of the files from all the files actually stored in iCloud.
And I tested CloudKit, but it just takes control over App's own data only.
Thanks in advance.
One thing to keep in mind when thinking about this is that iTunes actually doesn't backup or restore anything to/from iCloud. iOS device does it on its own: there's a daemon process running and if iCloud Backup is enabled, it will backup directly to iCloud, without talking to iTunes. Restore is also done without interaction with iTunes.
Next, as #rmaddy pointed out, your app on the device cannot access files of other apps, so you won't be able to do what you want from the device. If you want to do this via companion app running on PC/Mac then it might be possible, but will depend on what exactly do you need/want.
Re: contacts. Apps can access AddressBook and thus can sync/upload its contents. Apps can also access some other shared data, such as Photo Library. This is still a far cry from your original request to "Backup/Restore data for all Apps".
Re: downloading iCloud backups. Downloading isn't a big deal those days (Disclosure: I am the author of that forensic tool to download iCloud backups you're mentioned). There is even an open-source code for that. However, downloading and restoring are two very different processes. Specifically, you can download, but your app won't have enough permissions to write files in place.
Hope this helps and clears things up.

turn off iCloud backup for my application iOS sdk

I have some problems because of iCloud and i want to turn off its for my app,how can i do it in the code of my application,i thought about my problem and the decision can be turning off the iCloud, thanks for help
If you store files in Documents, they will be backed up to iCloud automatically. If you don't want that, the easiest way is to just store them somewhere else. Library/Caches is a good choice. If you really MUST store in Documents but you want to disable iCloud backup you can use the technique outlined here:
https://developer.apple.com/library/ios/#qa/qa1719/_index.html

What directory should I store analytics data in on iOS?

I'm trying to store analytics data that is saved locally about a user's actions so it can be uploaded later when the user has an internet connection. I'd like the data to be stored locally and not deleted between subsequent opens of the app under normal circumstances. I do not want the data to be synced to iCloud. I'd also ideally like the data to be preserved between updates. It's fine if the data gets deleted in cases of low space.
I'm getting different answers from different sources about where to store the data- either in NSCachesDirectory or NSLibraryDirectory. Note NSCachesDirectory is a subdirectory of NSLibraryDirectory, eg. the filesystem looks like Application_Home/Library/Caches/.
According to the official documentation: http://developer.apple.com/library/ios/#documentation/iPhone/Conceptual/iPhoneOSProgrammingGuide/PerformanceTuning/PerformanceTuning.html#//apple_ref/doc/uid/TP40007072-CH8-SW9 implies I should use NSCachesDirectory to store the data and it is not deleted under most circumstances. It also implies NSLibraryDirectory is synced to iCloud.
According to these answers: How can I get a writable path on the iPhone?, https://stackoverflow.com/a/5444762/340520/, When are files from NSCachesDirectory removed?, NSCachesDirectory is not preserved between app updates and claims that I must constantly recreate the NSCachesDirectory. The first answer also implies NSLibraryDirectory is the best place to store the data. However those answers are two years old.
According to the documentation and this answer: http://developer.apple.com/library/ios/#documentation/iPhone/Conceptual/iPhoneOSProgrammingGuide/PerformanceTuning/PerformanceTuning.html#//apple_ref/doc/uid/TP40007072-CH8-SW10, https://stackoverflow.com/a/8830746/340520, NSCachesDirectory ARE preserved between app updates.
Localytics' iOS SDK stores their data in NSCachesDirectory: https://github.com/twobitlabs/Localytics-iOS/blob/master/src/LocalyticsDatabase.m
Mixpanel's iOS SDK stores their data in NSLibraryDirectory: https://github.com/mixpanel/mixpanel-iphone/blob/master/Mixpanel/Mixpanel.m
Between all these sources, I've been leaning toward using NSCachesDirectory, but I'm not confident that the data won't get regularly deleted under some circumstances.
NSCachesDirectory is the wrong place to store persistent information that you will need across app starts or even device re-starts.
To prove my point try this ...
Get a iPhone device that has only 8GB disk space.
Use your app to write a file in the NSCachesDirectory.
Start downloading random apps to fill up the disk space. Very soon you will see the Storage limit dialog shown by the OS.
Now just re-start your phone, start the app and see if you can find your file that you wrote.
If the first time you find the file, try the experiment again and you will find your file missing. During the device startup, if you see the device logs you will notice logs indicating purging directory to make space etc.
Use NSCachesDirectory to store information you can keep downloading from your server. Not information that you need to upload to your server.
The Caches directory should only be used for files that your app can easily replace if they are deleted. The Caches directory is may or may not be purged during an app update and possibly if the device runs out of storage space. Only use this for temporary files or files you can easily replace.
My first choice would be the Library/Application Support directory (NSApplicationSupportDirectory). Please note that this directory is not created by default. Your app must create it on first startup. This path is kept during app updates (like most of the app sandbox) and it is backed up via iTunes (or iCloud) device backup.
I think the best place is Library/Application Support (NSApplicationSupportDirectory) refer How do I prevent files from being backed up to iCloud and iTunes? for details.

Storing file out of sandbox in iphone or Accessing filesystem by other applications

The main objective of my application is to download pdf, html, doc etc file types from server and read it.
Now the requirement is , the files i have downloaded should be accessible by other reader applications that are already installed in the device. So how to allow my filesystem to be read by other applications?
Is there any possibility of storing filesystem in common location or outside sandbox, which will not be deleted when the user uninstall or delete the application from the device? The reason behind this question is that, the user should not loose his contents once downloaded, the user might read the downloaded files using other reader applicatiion that is already installed in his device.
Thanks in advance,
Shatthi
No, you can't. The sandbox is there for a reason.

Resources