Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

iOS Document folder is not a directory and/or is missing

Trying to archive an array of Codable elements.

do {
   let data = try PropertyListEncoder().encode(elements)
   let success = NSKeyedArchiver.archiveRootObject(data, toFile:self.archiveURL.path)
   print(success ? "Successful save" : "Save Failed")
} catch {
   print("Save Failed")
}

For some reason path (archiveURL) is constantly wrong:

let archiveURL = FileManager().urls(for: .documentDirectory, in: .userDomainMask).first!

is returning always URL like this:

file:///Users/userName/Library/Developer/CoreSimulator/Devices/deviceID/data/Documents

but searching through folders I see no /Documents folder. Has something changed recently? It used to work few weeks back (pretty sure). Super annoying and I can't find any workaround/fix for that.

like image 841
raistlin Avatar asked Nov 30 '25 23:11

raistlin


2 Answers

I believe this is a (new-ish) "feature" of the iOS simulators - not all of the ususal directories exist when an app is initially installed.

Fortunately it's easy to create them, e.g. for the applicationSupport directory I do this in my apps:

let fileManager = FileManager.default
let directory = NSSearchPathForDirectoriesInDomains(.applicationSupportDirectory, .userDomainMask, true).first!
if !fileManager.fileExists(atPath: directory) {
  try fileManager.createDirectory(atPath: directory, withIntermediateDirectories: true, attributes: nil)
}
like image 163
Gereon Avatar answered Dec 03 '25 14:12

Gereon


This happens in Simulators running iOS 11.x and up.

Before iOS 11, the Simulator created the global (app independent) Documents folder on startup. Since iOS 11.x (don't know if it was a special point release), this does not happen anymore.

Be aware that NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true).first returns this global documents path only in (unit) tests, not when running the app. If you run the app, you get the application specific Documents folder which is still created when installing the app in the simulator.

This seems to be a bug on Apple side. If you run into this issue (as it seems you have), please file a bug report at https://developer.apple.com/bug-reporting/.

like image 25
ChaosCoder Avatar answered Dec 03 '25 16:12

ChaosCoder



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!