I'm trying to view the stored keys and values that I save to User Defaults with this code:
UserDefaults.standard.setValue(myStringVar, forKey: "my_key")
But I can't find the location of the file containing this data in iPhone simulator?
Storing Data in User Defaults The user's defaults database is stored on disk as a property list or plist. A property list or plist is an XML file. At runtime, the UserDefaults class keeps the contents of the property list in memory to improve performance.
The UserDefaults class provides a programmatic interface for interacting with the defaults system. The defaults system allows an app to customize its behavior to match a user's preferences. For example, you can allow users to specify their preferred units of measurement or media playback speed.
There's no straight way to save UIColor with UserDefaults so we will have to manipulate the type a bit. We are going to convert UIColor to Data and save it as any? . To convert the type of UIColor , we will need to use a special method called NSKeyedArchiver . Therefore, let's make an extension to the UserDefaults .
You find your user default value in list file using below code
var path: [AnyObject] = NSSearchPathForDirectoriesInDomains(.libraryDirectory, .userDomainMask, true) as [AnyObject]
let folder: String = path[0] as! String
NSLog("Your NSUserDefaults are stored in this folder: %@/Preferences", folder)
At this path you will find a plist file which is named by your <bundle identifier>.plist
You can also check it by typing print command in AppDelegate.swift file under
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
print(NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true).last! as String)
}
When app builds it will print exact path in Debug area. You should then be able to locate .plist file which contains key-value pairs for data you stored.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With