> On Oct 12, 2023, at 04:54, Gabriel Zachmann via Cocoa-dev 
> <cocoa-dev@lists.apple.com> wrote:
> 
> Now, under Sonoma (14.0), I have an issue with the settings (user defaults) 
> of my screen saver, again.
> 
> I go into System Settings, change some settings of my screen saver, they take 
> effect immediately in the little Preview window in SysSettings, but when I 
> click on "Preview" or let the screen saver come on automatically, the new 
> settings are *not* in effect.
> 
> From one user, I heard that the new settings take effect only after rebooting.
> 
> So, maybe, something has changed in the way macOS 14.0 handles the 
> NSUserDefaults, and/or maybe I make an error with using the API. Maybe there 
> is a bug in macOS 14.0 ?
> 
> Below you can find a recap of how I handle the settings using NSUserDefaults.
> Interestingly, the same code works fine in a regular app (not screensaver).
> 
> My screensaver stores different settings for different monitors in the 
> [NSUserDefaults standardUserDefaults].
> To do so, I generate 
>  defaults_ = [NSUserDefaults standardUserDefaults];
> 
> Then, I create a dictionary containing the default settings and register 
> them, like this:
> 
>   NSDictionary * monitor_defaults = [NSDictionary 
> dictionaryWithObjectsAndKeys: defaultsForMonitor, displayname, nil ];
>   [defaults_ registerDefaults: monitor_defaults];
>   NSDictionary * monitor_user_prefs = [defaults_ dictionaryForKey: 
> displayname];
> 
> Then, I read the actual values from monitor_user_prefs.
> 
> When the user changes a setting, I do the inverse, like this:
> 
>   .. create a new dictionary monitor_user_prefs, containing all the key/value 
> pairs of the settings ..
>   [defaults_ setObject: monitor_user_prefs forKey: displayName_];
>   bool success = [defaults_ synchronize];

registerDefaults isn't doing what you think it's doing. Read its docs more 
carefully.

And there's no need to call synchronize. The docs clearly point that out: "this 
method is unnecessary and shouldn't be used."

To set a new default, simply use setObject:forKey:. To read them, simply use 
objectForKey: or dictionaryForKey:, since you're using one.

What you haven't shown is how you're coming up with a screen name. Are you sure 
it's the same every time for each screen?

Also, you *are* using the correct NSUserDefaults object, yes?

NSUserDefaults*                 defaults = [ScreenSaverDefaults 
defaultsForModuleWithName:@"com.yourcompany.yourscreensaver"];

--
Steve Mills
Drummer, Mac geek

_______________________________________________

Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)

Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com

Help/Unsubscribe/Update your Subscription:
https://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to arch...@mail-archive.com

Reply via email to