简体   繁体   中英

What happens to previous app installs when running an app on a device from Xcode?

I'm somewhat new to iOS development and I'm sure this is probably a basic question but I've been wondering about what happens each time you run your app on a physical device from Xcode. Let's say that my app takes a picture and saves it to the Documents directory. The first time I run the app, I take a picture and save it as a JPEG to the Documents directory. The path where the file gets saved is:

/var/mobile/Containers/Data/Application/86D12E64-92BA-405D-98C7-E2C3502DE39C/Documents/picture.jpg

Now I find a bug in my app so I stop it from Xcode. I fix the bug and run the app again. I repeat the steps above and this time I save the picture to the Documents directory and the path is:

/var/mobile/Containers/Data/Application/4A85B3CF-C578-4733-8AEC-19DEFA3796DE/Documents/picture.jpg

From what I understand the part of the path between /Application/ and /Documents represents the "app id" so it's not surprising to me that this seems to change each time I run the app from Xcode. But what happens to the data on the device's file system from all the previous times that I ran the app from Xcode? Does Xcode uninstall previous app installations and effectively remove that directory from the file system? Thanks for explaining what is probably a silly question!

It's expected behaviour. iOS 8 onwards, the URL to app's sandbox changes every time you relaunch the app for security reasons. Hence you should never store the absolute URL of the directory. You should access the path using File API every time like the following:

let directoryURL: URL = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask)[0]
let fileURL: URL = directoryURL.appendingPathComponent("picture.jpg")

The actual URL is changed every launch (virtually), but OS maps correctly the directly when you use before if you use the API.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM