-
Notifications
You must be signed in to change notification settings - Fork 2
/
CSCFPreferences.m
108 lines (72 loc) · 2.33 KB
/
CSCFPreferences.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
//
// CSCFPreferences.m
//
// Created by CreatureSurvive on 9/2/18.
// Copyright (c) 2018 Dana Buehre. All rights reserved.
//
#import "CSCFPreferences.h"
@interface CSCFPreferences ()
@property(nonatomic, copy) NSString *bundleID;
@property(nonatomic, assign) BOOL autoSyncronize;
@end
@implementation CSCFPreferences
#pragma mark - Initialization
- (instancetype)initWithBundleID:(NSString *)bundleID {
if ((self = [super init])) {
self.bundleID = bundleID;
}
return self;
}
- (instancetype)initWithBundleID:(NSString *)bundleID autoSyncronize:(BOOL)synchronize {
if ((self = [[CSCFPreferences alloc] initWithBundleID:bundleID])) {
self.autoSyncronize = synchronize;
}
return self;
}
#pragma mark - Synchronization
- (BOOL)synchronize {
return CFPreferencesSynchronize( (__bridge CFStringRef)self.bundleID, kCFPreferencesCurrentUser, kCFPreferencesAnyHost);
}
#pragma mark - Convenience
- (id)objectForKey:(NSString *)key {
if (self.autoSyncronize) {
[self synchronize];
}
CFPropertyListRef value = CFPreferencesCopyValue( (__bridge CFStringRef)key, (__bridge CFStringRef)self.bundleID, kCFPreferencesCurrentUser, kCFPreferencesAnyHost);
id object = nil;
if (value != NULL) {
object = (__bridge id)value;
CFRelease(value);
}
return object;
}
- (NSString *)stringForKey:(NSString *)key {
return [self objectForKey:key];
}
- (BOOL)boolForKey:(NSString *)key {
id value = [self objectForKey:key] ? : @(NO);
return [value boolValue];
}
- (float)floatForKey:(NSString *)key {
id value = [self objectForKey:key] ? : @(0.0f);
return [value floatValue];
}
- (double)doubleForKey:(NSString *)key {
id value = [self objectForKey:key] ? : @(0.0);
return [value doubleValue];
}
- (int)intForKey:(NSString *)key {
id value = [self objectForKey:key] ? : @(0);
return [value intValue];
}
- (void)setObject:(id)object forKey:(NSString *)key {
@try {
CFPreferencesSetValue((__bridge CFStringRef)key, (__bridge CFPropertyListRef)object, (__bridge CFStringRef)self.bundleID, kCFPreferencesCurrentUser, kCFPreferencesAnyHost);
if (self.autoSyncronize) {
[self synchronize];
}
} @catch (NSException *exception) {
NSLog(@"ERROR: failed to save object for key: %@ EXCEPTION: %@", key, exception.description);
}
}
@end