How to access the UserDefaults file folder on my ios application - Swift

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP



How to access the UserDefaults file folder on my ios application - Swift



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





Have you tried downloading the container instead?
– silicon_valley
1 hour ago




1 Answer
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:


UserDefaults


bundleIdentifier


Preferences


Library


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






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

make 2 or more post in bootsrap

Store custom data using WC_Cart add_to_cart() method in Woocommerce 3

Firebase Auth - with Email and Password - Check user already registered