Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get NSUserDefaults plist file from device

When testing my app on the simulator, I like the ability to edit, or even trash the apps plist file (which contains the NSUserDefaults) from the iPhone Simulator folder. This proves useful when testing (e.g. your app stores a dictionary in there, but you change the model/keys that you use for this data, and therefore need to remove the dictionary stored).

Is it possible to access this file on device (for your own app), without jailbreak?

Thanks in advance

like image 315
Alex Coplan Avatar asked Jul 26 '11 10:07

Alex Coplan


People also ask

Where is NSUserDefaults stored?

All the contents saved by NSUserDefaults is saved inside a plist file that can be found under Library -> Preferences -> $AppBundleId.

Where can I find UserDefaults?

Navigate to AppData -> Library -> Preferences. The property list file (. plist) in this folder is the database file of the standard UserDefaults of your app.

How do you check if NSUserDefaults is empty in Objective C?

There isn't a way to check whether an object within NSUserDefaults is empty or not. However, you can check whether a value for particular key is nil or not.

What is NSUserDefaults?

The NSUserDefaults 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.


1 Answers

The file is in Library/Preferences. The file is a binary plist with name <iOS application target identifier>.plist (look for the Identifier field in your app target settings), or list the directory contents:

NSString *path = [NSSearchPathForDirectoriesInDomains(NSLibraryDirectory, NSUserDomainMask, YES) firstObject];
NSArray *dirContents = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:path error:nil];

You could also load clean defaults with a #ifdef macro based on some env variable:

#ifdef TESTING
    // use the code provided by tsakoyan below
#endif
like image 174
Jano Avatar answered Oct 10 '22 15:10

Jano