3

I am would like to know how to access the Documents directory of my ios appplication. I have tried the following on xcode:

window > Devices and Simulators > [Select my device] > [select my application by name] > Show container.

The container comes back empty. I know that I have files stored because my application sigs me in automatically through the code in my app delegate:

let userId = UserDefaults.standard.string(forKey: "userId")
guard let id = userId else { 
   // <GO TO LOGIN PAGE return true> 
}
// <GO TO HOME PAGE>

I am trying to verify that the following files exists as I have saved an image to the following url:

file:///var/mobile/Containers/Data/Application/6CEE9832-CE7B-4C31-9A62-F9F62D382C49/Documents/tempImage_wb.jpg
Ashley Mills
  • 50,474
  • 16
  • 129
  • 160
dean
  • 321
  • 3
  • 17

1 Answers1

1

Your UserDefaults dictionary data is saved as a plist file, named as your app bundleIdentifier inside the Preferences directory inside your app Library directory:

let fileName = Bundle.main.bundleIdentifier!
let library = FileManager.default.urls(for: .libraryDirectory, in: .userDomainMask).first!
let preferences = library.appendingPathComponent("Preferences")
let userDefaultsPlistURL = preferences.appendingPathComponent(fileName).appendingPathExtension("plist")
print("Library directory:", userDefaultsPlistURL.path)
print("Preferences directory:", userDefaultsPlistURL.path)
print("UserDefaults plist file:", userDefaultsPlistURL.path)
if FileManager.default.fileExists(atPath: userDefaultsPlistURL.path) {
    print("file found")
}

If you need to check the contents of your Documents or to locate this file in your computer simulator all you need to do is to open your Mac finder and use the File menu option Go > Go To Folder… and copy and paste your app preferences folder path that is printed in the console there.

let documents = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask).first!
let imageURL = documents.appendingPathComponent("tempImage_wb.jpg")
print("Documents directory:", imageURL.path)

enter image description here

Leo Dabus
  • 229,809
  • 59
  • 489
  • 571