Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

iOS10 UserDefaults file location/path?

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?

like image 895
Jesper Johansson Avatar asked Dec 05 '16 07:12

Jesper Johansson


People also ask

Where is UserDefaults saved?

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.

What is UserDefaults in IOS?

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.

How do you save UIColor in UserDefaults?

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 .


2 Answers

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

like image 83
Crazy Developer Avatar answered Oct 16 '22 06:10

Crazy Developer


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.

like image 44
Mile Dev Avatar answered Oct 16 '22 08:10

Mile Dev