PSUserDefaults
Deliberately shaped like NSUserDefaults, so it reads the way you
expect. It is a different store: values go to
/Library/TweakInject/Preferences/Defaults/, where a settings
pane in another process can see them.
Include
#import "PSUserDefaults.h"
Creating one
Signatures
- (nullable instancetype)initWithSuiteName:(NSString *)domain; @property (readonly, copy) NSString *suiteName;
There is no shared singleton and init is unavailable: every
instance names its domain, because a tweak writing to the wrong domain fails
silently and is tedious to find. Pass your package identifier.
Defaults
Signature
- (void)registerDefaults:(NSDictionary<NSString *, id> *)defaults;
Values used when a key has never been written. They are not persisted, so
call this on every launch before reading, exactly as with
NSUserDefaults.
Typed accessors
Signatures
- (BOOL)boolForKey:(NSString *)key; - (NSInteger)integerForKey:(NSString *)key; - (double)doubleForKey:(NSString *)key; - (float)floatForKey:(NSString *)key; - (nullable NSString *)stringForKey:(NSString *)key; - (nullable NSArray *)arrayForKey:(NSString *)key; - (nullable NSDictionary *)dictionaryForKey:(NSString *)key; - (nullable NSData *)dataForKey:(NSString *)key; - (void)setBool:(BOOL)value forKey:(NSString *)key; - (void)setInteger:(NSInteger)value forKey:(NSString *)key; - (void)setDouble:(double)value forKey:(NSString *)key; - (void)setFloat:(float)value forKey:(NSString *)key;
Objects and subscripting
Signatures
- (nullable id)objectForKey:(NSString *)key; - (void)setObject:(nullable id)value forKey:(NSString *)key; - (void)removeObjectForKey:(NSString *)key; - (nullable id)objectForKeyedSubscript:(NSString *)key; - (void)setObject:(nullable id)value forKeyedSubscript:(NSString *)key;
The subscript pair means defaults[@"key"] works for both reading
and writing.
Everything at once
Signatures
- (NSDictionary<NSString *, id> *)dictionaryRepresentation; - (BOOL)synchronize;
Example
Objective-C
PSUserDefaults *prefs =
[[PSUserDefaults alloc] initWithSuiteName:@"com.example.mytweak"];
[prefs registerDefaults:@{ @"enabled": @YES, @"intensity": @0.5 }];
if ([prefs boolForKey:@"enabled"]) {
double intensity = [prefs doubleForKey:@"intensity"];
apply(intensity);
}
prefs[@"lastRun"] = [NSDate date];