Shipping zip with thousands of audio files within app bundle - ios

I am developing app for studying foreign languages, and I'll need to ship 80000 little audio files in my app.
To reduce the size of my app I decided to ship only part of them in the bundle, on the zip archive, and then un-archive it to documents directory. Then, when iPhone will be in Wi-Fi network, I'll little by little download all the others.
But the problem is that Apple says, that we are not allowed to store big files in the Documents Directory. Flagging files for not backing up works only since 5.0.1
So I see two ways for me:
Ship all the files without zipping them and storing them in the app bundle – but that's too huge.
Ship a zip, un-archive it to the cache directory and then check, if they are (not deleted by system), and download one, if it's removed.
What should I choose or is there any other way for me to work it out?

Zipping audio files isn't a good way. I'm sure that saved space will not play any role.
Best solution is to use AAC encoded audiofiles.
Choose suitable quality between 64-128 kbps. And probably mono.
iPhones 3Gs and higher have hardware support for this codec.

There isn't much point in zipping files in the app bundle if you will just decompress them to the documents directory. When app bundles are delivered to iOS devices from the app store they are zipped up anyway (.ipa file), so unzipping to documents will make your app actually take up much more space on the user's device (once in the app bundle and once in the documents directory).
Automated way:
If you want to download them without using too much documents space you can write a predictive cache which has a maximum allowed size. You can keep track of when each file was last accessed and when you need to download a new file remove the oldest-accessed files until you have enough space. Your app can predict which files are needed next based on progression through the lessons, but even if they access a not-present file you can download it just-in-time and add it to the cache for future re-use.
Manual way:
You could provide a user interface to show the user how much space the different lessons are using and split it into lesson packs. Allow the user to control which packs remain cached and show how much space they use. They can decide when to download new packs as they progress through the lessons and when to remove old ones (if they want to save space). If the user wants to keep the files locally then you should let them, even though it uses lots of documents space.

Related

Pre-load content into Documents or Library directories from XCode

I am building an iPhone app that requires preloaded content. This content is presented so that it tests the user's knowledge, but is not modified by the user. The content consists of proprietary image files and pdf files.
From what I understand, the best practice would be to store this in the app's Library or Documents directories (please inform if this assumption is not correct). In a future version of the app I might want to upload additional (not replacement) content via API, but this is not part of the initial version.
I have seen many posts and tutorials regarding obtaining paths to the Documents and Library directories of an app, and reading/writing to them. This is all good and useful, but not what I am looking for here.
I would like to preload the content into the Documents and/or Library directories, for the simulator initially, to test app in simulator; and ultimately to the release version. I would have thought this would be possible to do from XCode without writing code.
I have not been able to find a solution to this on Stack Overflow or other places on the net. Any pointers, links, solutions are welcome. I am using XCode 10.3 with Swift 4.2.
See the File System Programming Guide: File System Basics, which shows us:
The “data container” (including the Documents and the various Library folders) is for content generated/saved by the app. When, in Xcode, you mark resources as being part of the target, that becomes part of the bundle, and your app can retrieve it from there at runtime.
Theoretically, yes, you could copy data from the bundle to the Documents and/or Library folders, but, yes, you would have do that programmatically. It seems a bit wasteful to have two copies of these resources on the device, but you can do whatever you want. Generally, though, resources included in the bundle would just be be opened directly from there at runtime, not copying it to the data container (except for those cases where you would need to change it, because bundle contents are read-only).
FYI, for additional information regarding the file system, see the iOS Storage Best Practices video.

Do we need to cleanup downloaded image files?

In my iOS app I am downloading a bunch of image files and saving them on disk in Library/Caches. Do I need to delete those files given iOS automatically clears them when its running low on disk space.
It depends where you save them. Read the File System Programming Guide for detailed discussion.
In Summary, If you are saving the files to:
Temp: You don't need to worry about cleanup, but you need to be aware that these files will not persist between app launches
Library/Caches Generally iOS won't delete these files but can clear them when running on low disk space. So developer has to make sure these files can be regenerated when required.
All other folders yes, you need to make sure the files are properly cleaned up when they are no longer needed.
UPDATE: Now that you have specified that you are saving to Library/Caches, From Apple Docs about Caches directory:
Use this directory to write any app-specific support files that your app can re-create easily. Your app is generally responsible for managing the contents of this directory and for adding and deleting files as needed.
In iOS 2.2 and later, the contents of this directory are not backed up by iTunes. In addition, iTunes removes files in this directory during a full restoration of the device.
On iOS 5.0 and later, the system may delete the Caches directory on rare occasions when the system is very low on disk space. This will never occur while an app is running. However, you should be aware that iTunes restore is not necessarily the only condition under which the Caches directory can be erased.
To be specific Your app is generally responsible ... for adding and deleting files as needed., You should always clean up after yourself. If you know you are done with the files, keeping them on disk and waiting for the device to run on low disk space so that the OS deletes them is a bad idea.
btw, if you are downloading files from the internet, why don't you use an already build library (so that you don't have to worry about these things).
If you are developing for iOS 8.0+ and using Swift, you could use AlamofireImage as an embedded framework. It comes with a very good caching system. I think you can use it on iOS 7 as well by copying the Swift files.
If you are using Objective C, you could use SDWebImage which comes with its own caching system. You could also use AFNetworking and enable basic caching, this blog post should get you started.

Can iOS delete my App data when no free space

I want to know what iOS can do with my app data when the disk free space become very low. Can iOS delete some files to free disk space?
It depends on what you are doing. To be a good citizen, you should do the following:
Any files that you can reproduce yourself should be marked as "don't backup". That saves lots of traffic to the cloud when there is no need to backup a file that you can just download again when needed.
Any files that you can afford to lose at any time should be in the temporary directory. These files will be deleted automatically (but you should not wait for them to be deleted automatically) at certain times and when the phone runs out of space.
Any files that you can reproduce should be in a "cache" directory to allow iOS to delete them.
The user can always delete all the files belonging to your application. Much better to mark anything that can be restored as "cached" so only what is essential is displayed in Settings as belonging to your app, and the user isn't tempted to throw it away.
It wont delete it, although it can delete your caches in Library/Caches. You need to conform to specs detailed in the link above, as your app might be rejected if you'll store something not where it should be (for example some caches in Documents).
NO , iOS will not delete some files or data in your app. iOS will not delete user data automatically.

iOS requirements for data storage (using cache folder versus marking files not to be backedup)

I got my app rejected due to violation of 2.23
After inspection, it would appear that I was indeed not a correct path for storing downloaded images and data files (i.e. files that I would prefer to have available for offline usage, but which the app can re-download again if removed by iOS)
However, after looking at:
https://developer.apple.com/library/ios/qa/qa1719/_index.html
It appears it may not even be enough to proper "cache" path for iOS > 5? Example:
/var/mobile/Applications/00000000-0000-0000-0000-000000000000/Library/Caches/'
Will using above make my app pass this requirement? Or am I forced to using the API for making files not to be backed-up?
Using the caches directory is correct if you can re-download the files. They will not be backed up. You only need to use the "do not backup" flag if the files exist in a location that normally is backed up (e.g. the documents directory).

Where to save downloaded data that can't be recreated?

I created magazine reader app that uses png images as pages. When user downloads magazine, all png images are downloaded and stored in Caches folder.
Problem with Caches is that files in there can be apparently deleted anytime. Since app is designed to be used in offline mode as well, re-downloading of missing pages is impossible.
I tried to save it into Documents folder but my app got rejected, this apparently is not proper place for them.
So my question is, where can I put them to make that iOS won't delete them? I don't need them to be backed up to itunes or synced or anything like that, I just need them to stay there until I remove them.
I tried looking into the documentation but I could not find a category that would fit my needs, am I missing something trivial?
EDIT: I need to support iOS 4 as well
http://developer.apple.com/library/mac/#documentation/FileManagement/Conceptual/FileSystemProgrammingGUide/FileSystemOverview/FileSystemOverview.html
Put it in the Libary Folder
Handle support files
—files your application downloads or generates and can recreate as needed—in one of two ways:
In iOS 5.0 and earlier, put support files in the /Library/Caches directory to prevent them from being backed up
In iOS 5.0.1 and later, put support files in the /Library/Application Support directory and apply the com.apple.MobileBackup extended attribute to them. This attribute prevents the files from being backed up to iTunes or iCloud. If you have a large number of support files, you may store them in a custom subdirectory and apply the extended attribute to just the directory.
Apple has a tech note that addresses this at http://developer.apple.com/library/ios/#qa/qa1719/_index.html
It shows sample code for setting a no-backup attribute on files.

Resources