Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What happens to local files on iOS when the app is updated?

Tags:

ios

My iOS app is writing to local files to:

/var/mobile/Containers/Data/Application/A.../Library/a.txt

When my app updates, it gets a new application container ID and folder:

/var/mobile/Containers/Data/Application/B...

What happens to all the files I wrote to container ID A?

Is there an "update hook" that will allow me to copy all the "A" container files to path "B"?

Can the new version of the app (B) read the old versions files (A)?

Is there any documentation around what happens to the filesystem during updates?

Is it possible to recover files from container A after B has been installed?

like image 719
zino Avatar asked May 18 '19 13:05

zino


2 Answers

When you update an app, by changing its version number in the .plist file, iOS creates a new directory for that app with a different hexadecimal name and it copies all the files to the new directories. Now if you are using the absolute paths to get the details of files from the directories then those paths would be incorrect and you won't get the file details.

I just tried this in simulator. I created a function as below which will return the document directory path url

func getDocumentsDirectory() -> URL {

    let paths = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask)
    let documentsDirectory = paths[0]
    return documentsDirectory
}

I called this function from didFinishLaunchingWithOptions in app delegate when version number was 1.0 and it returned the below path :

file:///Users/BhargavRathod/Library/Developer/CoreSimulator/Devices/3082611F-BCCA-4D17-B390-E0EF4CA454DA/data/Containers/Data/Application/72759097-38F3-4292-825E-1D2343219973/Documents/

When I updated the version number to 1.1 it returned me the new path as :

file:///Users/BhargavRathod/Library/Developer/CoreSimulator/Devices/3082611F-BCCA-4D17-B390-E0EF4CA454DA/data/Containers/Data/Application/72DC31E9-C32F-42CC-8449-F1946ADB1018/Documents/

So if you are using a absolute path to access any file from document directory then it is not good practice. You can just save the file name(or relative path after the document directory) and whenever the access to that file name is required then get the file name and append it after the document directory path and access the file.

I hope this will be of some help to you.

like image 107
BhargavR Avatar answered Nov 03 '22 00:11

BhargavR


My iOS app is writing to local files to: /var/mobile/Containers/Data/Application/A.../Library/a.txt

Okay, stop right there. This is wrong.

Your app has its own sandbox. This sandbox persists forever, thru updates, as long as your app keeps its Bundle ID, and as long as the user does not delete the app.

You thus have no business knowing or thinking about the full absolute path to where your file is. All you know, and all you need to know, is that it is in your app’s sandbox in the Library directory. You can, at any time, obtain the URL of the Library directory by means of its search path:

https://developer.apple.com/documentation/foundation/filemanager/searchpathdirectory/librarydirectory

And that is where the file will always be. As long as you ask the FileManager for your Library directory and for the a.txt file within it, you will find the same file, regardless of any updates, as long as the user doesn’t actually delete your app (because that deletes the sandbox).

Can explain what happens to the files

Nothing. They stay where they are within the sandbox. The absolute URL of the sandbox may change, but your files are unaffected.

how to keep the files written by the previous version of the app

They are kept automatically. You don’t have to do anything.


(Having said all that, keep in mind that if you submit an app with a different bundle id, that is not a new version of your app. It is a totally different app. In that case you would have a very different problem to solve. This would be no different from any other problem of communicating files from one app to another. You’d need to put the files in a common location, make them available thru the Document Browser, make them exportable by the user, or whatever.)

like image 39
matt Avatar answered Nov 02 '22 23:11

matt