Releasing IOS app with files in Library directory - ios

I have an app that is going to display a list of local video files. The app will only have a couple of video files at first, but that number will grow with future updates and in-app purchases. I would like to store all these videos in 1 place and it makes sense to put them in the app's Library directory so they are not user-accessible. Is it possible to publish an app with files already in the app's Library directory? Or do I have to put them in the app's bundle and then copy them to the Library directory when the app is first launched? Although that would result in the 2 videos files being duplicated since I can't remove them from the bundle.

Yes, you do need to copy the files from the app bundle into the Library if you want to treat all the videos in the same way. You cannot change the contents of the app bundle from inside your app and the only files your app has when it first launches are those that are in the app bundle.
If you really wanted (and if the video files don't change), you could make special cases in your code to be able to get the starting videos from the app bundle and the in-app purchase downloaded videos from the Library directory. But, unless the video files are quite large this seems a bit excessive.
A third option would be to have the user download the starter files on first app launch, but that may be suboptimal for other reasons.

Please look at my answer in this thread. It might concern you or at least be interesting to read.
Does updating iOS apps delete library directory files?

Related

Having a bit of trouble with copying a local file on iOS

Having only worked with Android/web dev before I am having a hard time figuring out where in the world my file should be getting copied to in iOS. After reading the file browser documentation on the iOS developer resource page it says that each app is an "island" which contains its own folder system. If I want to have my user be able to copy a file from my app's sdk to their iOS device, where should I put it?
I downloaded a file browser app from the app store on my iOS testing device but am I right in thinking that there is no global file browsing system?
I am using a Cordova plugin called Asset2sd which works perfectly for me in Android, getting the root storage folder and downloading the file to there. It has no iOS alternative so I'm going to have to figure something else out, I just need to know where to start. Do I have to have the user access my app's internal files somehow? Can I create a folder for them to look in when they download my app? Some documentation or something would be wonderful. I am totally lost.
Thanks!
You're right. Apps don't have access to the global file system. Each app only has access to their own app directory. Here's in-depth information on Sandboxing: https://developer.apple.com/app-sandboxing/
In your app's sandbox, you basically can manage files as you want, i.e. download, copy, move, delete, etc. So you can create a folder Downloads in your apps documents directory and then display the contents of this folder for instance in a table view.
Related documentation/links:
https://developer.apple.com/library/prerelease/ios/documentation/Cocoa/Reference/Foundation/Classes/NSFileManager_Class/index.html
https://stackoverflow.com/a/6907432/967327
https://developer.apple.com/library/ios/documentation/iPhone/Conceptual/iPhoneOSProgrammingGuide/iPhoneAppProgrammingGuide.pdf

iOS application cannot access downloaded file when relaunched - UUID changes

I've been debugging a problem where my app successfully downloads a file and copies it to the Documents directory. Lately the app cannot open the downloaded file on subsequent launches of the app. I've been printing out file URLs and this is what I see when the file is initially downloaded and opened:
unzipped /var/mobile/Containers/Data/Application/9AC69C00-228E-482F-99D8-DD8F214FCE88/Documents/3.atcase to
/var/mobile/Containers/Data/Application/9AC69C00-228E-482F-99D8-DD8F214FCE88/Library/Application Support/thebundleID/current-case
You can see the UUIDs are the same, but when I launch the app again I see this:
couldn't unzip /var/mobile/Containers/Data/Application/9AC69C00-228E-482F-99D8-DD8F214FCE88/Documents/3.atcase to
/var/mobile/Containers/Data/Application/C18E7EFA-C0D0-4213-AF85-F5BC0D2A4207/Library/Application Support/thebundleID/current-case
This time the UUIDs appear to be different.
I will say tentatively that the answer is yes, the UUID used in the application container folder does seem to change every time the app is run, though I'd love to have some confirmation from a more experienced developer.
The solution to my problem was straightforward: Do not store absolute file URLs in the database. The URLs for the files are now constructed relative to the Documents directory URL every time the app is run. Since I name the files in a regular way, there is no need to store URLs anyway.

Allowed to delete files/folders from Main Bundle resources?

I am designing an app that will come bundled with some audio files. They'll total about 50MB in size. The app will allow you to download other audio files via in-app purchase. My goal is to have all of these files stored in one location (the Documents Directory).
What I'm doing in my code is this:
On first launch, copy the bundled songs from Main Bundle Resources to the documents directory.
Delete the files from their original location in the Main Bundle Resources.
I have all of this working fine so I know it's possible to do. My question is can anyone point me to Apple documentation/guidelines that says I'm not allowed to do this? Or, is this perfectly acceptable according to Apple's guidelines? I know they don't want you writing to the Resources folder, but haven't been able to find a definitive answer on deleting.
The documentation you're looking for is in the App Distribution Guide [emphasis mine]:"
Code signing your app allows the operating system to identify who signed your app and to verify that your app has not been modified since you signed it. Your app’s executable code is protected by its signature because the signature becomes invalid if any of the executable code in the app bundle changes. Note that resources such as images and nib files are not signed; therefore, a change to these files does not invalidate the signature.
That said, as I noted in my comments on #Wain's answer, this probably isn't a great thing to do if the files are read-only. It adds a big copy step on first launch, and you have to deal with it again on every upgrade.
It is not acceptable and will not work when running on a device (I guess you're testing on the simulator). You can't edit the bundle contents. The bundle is signed and editing it will invalidate the signature.
It would be advisable to deploy the app without the audio files and then to start downloading them as soon as there is a (suitable) internet connection after first launch (if this is an option for you).

IOS - In-app purchase downloadable media storage guidelines

I have an app which has in app purchase downloadable videos. The first time I submitted to the app store it was rejected because I was writing it to <Application_Home>/Documents directory. The rejected reason stated that
In particular, we found that on launch and/or content download, your
app stores 20MB.
I read the guideline and it told me that
Data that can be downloaded again or regenerated should be stored in
the <Application_Home>/Library/Caches directory. Examples of files you
should put in the Caches directory include database cache files and
downloadable content, such as that used by magazine, newspaper, and
map applications.
I checked the simulator's cache directory and found that there is a default folder within the Caches folder with the name of the Bundle Id of the app. So I changed it to the following path
<Application_Home>/Library/Caches/<Bundle ID>/
Ran this in the sim and seems fine. However, when I uploaded it, it was rejected. The problem seems to come from writing the data.
Question: Is there something wrong with this directory? Should I just store it in the Caches root folder?
Thanks.

Store images in iOS app directory when install

I am writing an iOS app which download images from a backend server, and store them in the document directory. I know how it is done.
But I need to write a prototype first, which use some pre-given images (not retrieve from the backend server). My question is, is it possible to store images in the document directory during initial installation of the app? I don't want to hardcode them in the bundle directory.
Thanks
Nothing can be done when the app is installed, only when the app is run. The first time the app runs, you can either copy pre-bundled images to the other directory (Documents) or you can download them as needed. It sounds like you want the first option for the prototype.
Basically you need to do what you don't want to do - hardcode them in the bundle.
Remember, the app's sandbox is setup when the app is installed. This includes the creation of the Documents directory. But nothing is put there. Only your running app can store something somewhere in the app's sandbox.

Resources