Search code examples
iosobjective-cxcodensuserdefaultsplist

Not finding NSUserDefaults plist in simulator folder


I know that there are other same questions as mine. But I think that I have tried everything and I still can't be able to find the NSUserDefaults plist file in my simulator folder. I made sure that I got the right ID for the simulator and I wasn't able to find the NSUserDefaults plist as I want to check exactly what values are being saved. Does anyone know what is the exact name of the file that I should be searching for?

For info, I am searching in the following path: /Users/username/Library/Developer/CoreSimulator/Devices/<device_id>/data/Library/Preferences

Does anyone know if I am doing it wrong? How can I find that file? Thank you for your help!


Solution

  • you can print the location by asking at runtime.
    Each time you compile and run simulator the folders may differ from before.

    how to write NSUserDefaults

    NSUserDefaults *userdef = [NSUserDefaults standardUserDefaults];
    [userdef setObject:@"testString" forKey:@"testkey"];
    [userdef synchronize]; //dont forget synchoniszing after setting new objects
    

    how to read NSUserDefaults

    NSUserDefaults *defs = [NSUserDefaults standardUserDefaults];
    NSString *result = [defs objectForKey:@"testkey"];
    NSLog(@"read out userdefs = %@",result);
    

    If you did not set any NSUserDefaults yet, there is no file.

    Otherwise where is your <BundleIdentifier>.<appName>.plist ?

    NSArray *path = NSSearchPathForDirectoriesInDomains(NSLibraryDirectory, NSUserDomainMask, YES);
    NSString *libraryFolder = [path objectAtIndex:0];
    NSString *appID = [[[NSBundle mainBundle] infoDictionary] objectForKey:@"CFBundleIdentifier"];
    NSString *userdefFile = [NSString stringWithFormat:@"%@/Preferences/%@.plist", libraryFolder, appID];
    NSLog(@"NSUserDefaults File located at: %@", userdefFile);