I have bunch of preference values registered via [NSUserDefaults registerDefaults:]
call on application startup. I need to replace them at some stage with new ones but new values scope (keys set) is less than initial one. So after I call [NSUserDefaults registerDefaults:]
again I have new values with some old that weren't replaced. Is there a possibility to remove previously registered values?
相关问题
- NSOutlineView drag line stuck + blue border
- iphone sdk see size of local file (one created
- iOS Terminating app due to uncaught exception '
- How can you detect the connection and disconnectio
- QuickLook Plugin Failing with sandboxing error
相关文章
- Converting (u)int64_t to NSNumbers
- “getter” keyword in @property declaration in Objec
- NSMenuItem KeyEquivalent “ ”(space) bug
- Detect if cursor is hidden on Mac OS X
- NSNumberFormatter doesn't allow typing decimal
- Is subclassing NSNotification the right route if I
- Creating an NSMutableArray with a literal via muta
- adding click action to NSTextField
You can use
removeVolatileDomainForName:
to remove theNSRegistrationDomain
. This clears all values registered viaregisterDefaults:
. But if you need to do this you’re most likely doing something wrong. The right way would be to actually store the new settings using one of theset...:forKey:
methods.To completely replace the registered defaults, you will replace the
NSRegistrationDomain
domain (which is volatile, by the way).To remove individual registered defaults, you get the values in the
NSRegistrationDomain
, remove the offending key(s), and replace the domain.A category on
NSUserDefaults
might look something like this:Motivation:
The reason I need this is because I want to turn on user-agent spoofing (for reasons of 3rd-party compatibility). User-agent spoofing only appears to work when the value is set via
registerDefaults:
, soset...:forKey:
won't stop the user-agent string it. If I want to stop spoofing the user-agent some time later (without restarting the app), I need a way to remove defaults. Further, I don't want to clear the other registered defaults my app users. The above solution appears to accomplish this perfectly.Swift 3 port of EthanB's original answer:
Used in
deinit
of myUIWebView
to restore the default user agent back to my app.