Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Mac sandbox created but no NSUserDefaults plist

I'm trying to track down some issues arising from sandbox creation. Under multiple circumstances it appears that an NSUserDefaults .plist file is not created in Data/Library/Preferences. I have seen this in the debugger and when launching the app from the Applications directory. I have not tried archiving, signing the app and then launching. Is that required?

An alias file ending in .LSSharedFileList.plist is created but it points to itself and therefore does not exist.

I don't know if it's related but Console reports:

appleeventsd[72]: <rdar://problem/11489077> A sandboxed application with pid ... checked in with appleeventsd, but its code signature could not be validated ( either because it was corrupt, or could not be read by appleeventsd ) and so it cannot receive AppleEvents targeted by name, bundle id, or signature. Error=ERROR: #100013  { "NSDescription"="SecCodeCopySigningInformation() returned 100013, -." }  (handleMessage()/appleEventsD.cp #2072) client-reqs-q

Thanks.

like image 901
greg Avatar asked Mar 07 '14 05:03

greg


3 Answers

Could this be related to the caching of NSUserDefaults?

In recent OS X versions the defaults are not immediately written to disk so you might not see them right away. You might want to try synching the prefs manually - From NSUserDefaults Class Reference:

At runtime, you use an NSUserDefaults object to read the defaults that your application uses from a user’s defaults database. NSUserDefaults caches the information to avoid having to open the user’s defaults database each time you need a default value. The synchronize method, which is automatically invoked at periodic intervals, keeps the in-memory cache in sync with a user’s defaults database.

Although I believe even that might not immediately write the defaults to disk in 10.9 anymore as some daemon caching the user defaults is now also involved.

Check also

Reading NSUserDefaults from helper app in the sandbox

Objective-C NSUserDefaults caching prevents another app from accurately reading changes

When (not) to abuse NSUserDefaults

like image 34
cacau Avatar answered Sep 22 '22 05:09

cacau


After spending a ton of time found freaking simple solution:

- (void)fixDefaultsIfNeeded{
    NSArray *domains = NSSearchPathForDirectoriesInDomains(NSLibraryDirectory,NSUserDomainMask,YES);
    //File should be in library
    NSString *libraryPath = [domains firstObject];
    if (libraryPath) {
        NSString *preferensesPath = [libraryPath stringByAppendingPathComponent:@"Preferences"];

        //Defaults file name similar to bundle identifier
        NSString *bundleIdentifier = [[NSBundle mainBundle] bundleIdentifier];

        //Add correct extension
        NSString *defaultsName = [bundleIdentifier stringByAppendingString:@".plist"];

        NSString *defaultsPath = [preferensesPath stringByAppendingPathComponent:defaultsName];

        NSFileManager *manager = [NSFileManager defaultManager];

        if (![manager fileExistsAtPath:defaultsPath]) {
            //Create to fix issues
            [manager createFileAtPath:defaultsPath contents:nil attributes:nil];

            //And restart defaults at the end
            [NSUserDefaults resetStandardUserDefaults];
            [[NSUserDefaults standardUserDefaults] synchronize];
        }
    }
}
like image 68
HereTrix Avatar answered Sep 21 '22 05:09

HereTrix


Just moving this into a full answer so it doesn't get missed. I only read it after trying the other solutions when this was solved in a few clicks.

@greg and @mehals answer in the comments section for the first solution solved it for me without needing to change any of my code

Killing cfprefsd did the trick from activity manager.

like image 20
UKDataGeek Avatar answered Sep 20 '22 05:09

UKDataGeek