-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAPUbiquitousUserDefaultsEngine.m
146 lines (122 loc) · 5.99 KB
/
APUbiquitousUserDefaultsEngine.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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
//
// APUbiquitousUserDefaultsEngine.m
// Squirrel
//
// Created by Axel Péju on 26/12/11.
// Copyright (c) 2011 Axel Péju. All rights reserved.
//
#import "APUbiquitousUserDefaultsEngine.h"
@interface APUbiquitousUserDefaultsEngine () {
@private
id ap_ubiquitousKeyValueStoreObserver;
id ap_userDefaultsObserver;
}
// Ubiquitous keys validation
- (BOOL)ap_ubiquitousKeysValid;
@end
@implementation APUbiquitousUserDefaultsEngine
@synthesize ubiquitousKeys = ap_ubiquitousKeys;
+ (APUbiquitousUserDefaultsEngine *)sharedEngine {
static APUbiquitousUserDefaultsEngine * sharedEngine = nil;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
sharedEngine = [[APUbiquitousUserDefaultsEngine alloc] init];
});
return sharedEngine;
}
- (void)dealloc {
[self stop];
}
- (BOOL)pullUserDefaultsFromiCloud {
BOOL success = [self ap_ubiquitousKeysValid];
if(success) {
NSSet * ubiquitousKeys = [self ubiquitousKeys];
NSUserDefaults * userDefaults = [NSUserDefaults standardUserDefaults];
NSUbiquitousKeyValueStore * keyValueStore = [NSUbiquitousKeyValueStore defaultStore];
[ubiquitousKeys enumerateObjectsUsingBlock:^(NSString * key, BOOL *stop) {
id objectForKey = [keyValueStore objectForKey:key];
if (objectForKey) {
[userDefaults setObject:objectForKey forKey:key];
}
}];
}
return success;
}
- (BOOL)pushUserDefaultsToiCloud {
BOOL success = [self ap_ubiquitousKeysValid];
if(success) {
NSSet * ubiquitousKeys = [self ubiquitousKeys];
NSUserDefaults * userDefaults = [NSUserDefaults standardUserDefaults];
NSUbiquitousKeyValueStore * keyValueStore = [NSUbiquitousKeyValueStore defaultStore];
[ubiquitousKeys enumerateObjectsUsingBlock:^(NSString * key, BOOL *stop) {
id objectForKey = [userDefaults objectForKey:key];
if (objectForKey) {
[keyValueStore setObject:objectForKey forKey:key];
}
}];
}
return success;
}
- (BOOL)start {
NSUbiquitousKeyValueStore * keyValueStore = [NSUbiquitousKeyValueStore defaultStore];
NSUserDefaults * userDefaults = [NSUserDefaults standardUserDefaults];
NSNotificationCenter * center = [NSNotificationCenter defaultCenter];
// Observe the values in iCloud
ap_ubiquitousKeyValueStoreObserver = [center addObserverForName:NSUbiquitousKeyValueStoreDidChangeExternallyNotification
object:keyValueStore
queue:nil
usingBlock:^(NSNotification *note) {
NSDictionary * userInfo = [note userInfo];
NSArray * changedKeys = [userInfo valueForKey:NSUbiquitousKeyValueStoreChangedKeysKey];
NSMutableSet * relevantKeys = [NSMutableSet setWithArray:changedKeys];
[relevantKeys intersectSet:[self ubiquitousKeys]];
// Pull values from iCloud
[relevantKeys enumerateObjectsUsingBlock:^(NSString * key, BOOL *stop) {
id objectForKey = [keyValueStore objectForKey:key];
if (objectForKey) {
[userDefaults setObject:objectForKey forKey:key];
}
}];
}];
// Observe changes in the user defaults
ap_userDefaultsObserver = [center addObserverForName:NSUserDefaultsDidChangeNotification
object:userDefaults
queue:nil
usingBlock:^(NSNotification *note) {
if([self ap_ubiquitousKeysValid]) {
// Push values to iCloud
[[self ubiquitousKeys] enumerateObjectsUsingBlock:^(NSString * key, BOOL *stop) {
[keyValueStore setObject:[userDefaults objectForKey:key] forKey:key];
}];
// Save changes
[keyValueStore synchronize];
}
}];
// Pull values from the cloud as initial sync
[self pullUserDefaultsFromiCloud];
return YES;
}
- (BOOL)stop {
NSNotificationCenter * center = [NSNotificationCenter defaultCenter];
if(ap_ubiquitousKeyValueStoreObserver) {
[center removeObserver:ap_ubiquitousKeyValueStoreObserver];
}
if(ap_userDefaultsObserver) {
[center removeObserver:ap_userDefaultsObserver];
}
return YES;
}
#pragma mark
#pragma mark Ubiquitous keys validation
// Returns NO if keys are not strings, or if at least one of the user defaults is nil
- (BOOL)ap_ubiquitousKeysValid {
NSSet * ubiquitousKeys = [self ubiquitousKeys];
__block BOOL success = YES;
[ubiquitousKeys enumerateObjectsUsingBlock:^(NSString * key, BOOL *stop) {
if(![key isKindOfClass:[NSString class]]) {
success = NO;
}
}];
return success;
}
@end