macOS & NSUserDefaults

Folks;


When I run an archived copy of my mac application on a test dev machine (not my main dev box) I see errors in the Console log that trouble me...


1) I see this error immediately before any of my code executes!

error 16:14:25.839821 -0700 [MYAPPNAME] Couldn't read values in CFPrefsPlistSource<0x6000000ed280> (Domain: [MYAPPBUNDLEID], User: kCFPreferencesCurrentUser, ByHost: No, Container: (null), Contents Need Refresh: Yes): accessing preferences outside an application's container requires user-preference-read or file-read-data sandbox access, detaching from cfprefsd


I open my standard defaults (a .plist) in -awakeFromNib like so:

NSUserDefaults *ud = [NSUserDefaults standardUserDefaults];

[ud registerDefaults:[NSDictionary dictionaryWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"WhodoMacOSDefaults" ofType:@"plist"]]];

[ud setObject:launch_Date forKey:kKudosLaunchDate];


This activity throws 2 of these errors in the console:

error 16:14:26.429881 -0700 [MYAPPNAME] attempt to set <private> for key in <private> in non-persistent preferences domain CFPrefsPlistSource<0x6000000ed280> (Domain: [MYAPPBUNDLEID], User: kCFPreferencesCurrentUser, ByHost: No, Container: (null), Contents Need Refresh: Yes)


Then subsequently every 'set' for NSUserDefaults throw another of these into the console log...


Nothing fancy going on in this app: no app groups, no iCloud, no hardened runtime...


I don't see a 'user-preference-read' option in the standard entitlement document.


Am I missing some basic conguration setting?

Is this all just a by-product of running a 'development' archive?


Any thoughts appreciated!

Steve

Accepted Reply

Have you done any hacking around on preferences? Is the a sandbox build?


In either case, you should always do:

killall prefsd


if the preferences system seems flaky or if you have hacked around on things. See if that clears it up. Restarting would effectively do the same thing too.

Replies

Have you done any hacking around on preferences? Is the a sandbox build?


In either case, you should always do:

killall prefsd


if the preferences system seems flaky or if you have hacked around on things. See if that clears it up. Restarting would effectively do the same thing too.

John;


Well Thank-You!


I quite the app and then used ActivityMonitor and 'ForceQuit' the 'root' instance and the instance running on the user account.


Re-ran the app and no more of the errors described above. Cool

Have you found our what was the cause of the problem?

It happens to me now, all the time, when I run (debug) from Xcode, my App which is like yours - a simplistic Cocoa app (Document based) with no "extensions" or "groups" or whatever.


It is, though, a sandboxed app (I set up the checkboxes to the best of my understanding, and archive it without diffuculty) but now I get these errors:

2019-09-07 19:49:09.598967+0300 PlanktoMetrix-II[84292:22916507] [User Defaults] Couldn't read values in CFPrefsPlistSource<0x6000000eab80> (Domain: com.apple.PowerManagement, User: kCFPreferencesAnyUser, ByHost: Yes, Container: (null), Contents Need Refresh: Yes): accessing preferences outside an application's container requires user-preference-read or file-read-data sandbox access, detaching from cfprefsd


and I'm wondering what I have done to cause this. My app Does register a set of defaults from a plist resource I have in the app once, upon launch - but that's all. And as much as I can see - nothing related to PowerManagement.

So I've done this (killall prefds as well as explicitly killing the cfprefsd root and user process). I'm working with an iOS app I've ported to mac via catalyst, and I cannot get rid of the messages.


2020-01-07 16:56:48.238531-0800 NetProbe[68038:5897039] [User Defaults] Couldn't write values for keys (
    ApplicationAccessibilityEnabled
) in CFPrefsPlistSource<0x600002c13b00> (Domain: com.ap

I get the message for each preference read or write. The app is sandboxed, but has read/write access for both user selected file and downloads folder. What do I need to do?

What is the rest of that domain? It sure looks like it is the start of "com.apple". If so, that's not your domain and you can't access it, at least not from the sandbox.

Have you done any hacking around on preferences? Is the a sandbox build?

Yes, I did. 😞

I wanted to reset my preferences to factory defaults and thought that I could do that quickly by deleting the preferences file from my application preferences in the ~/Library/Containers directory. After deleting the file, I started to get the same log errors as described in this thread, and my application would not properly initialize to its default preference values. I tried the usual clean build and then deleting the sandboxed application entirely from the ~/Library/Containers directory, but neither helped. The log errors persisted.

I then tried the simplest thing that came to mind, I created an empty preferences file with touch com.example.MyApp.plist in ~/Library/Containers/com.example.MyApp/Data/Library/Preferences directory and made the file read/write by owner only chmod 600 com.example.MyApp.plist.

Running in Xcode the application is initializing default preference values properly and the log errors have stopped. I did not restart my Mac and I did not have to killall prefsd.

I learned a valuable lesson, don't do any hacking around on the preferences.

Add a Comment