forked from MKSG-MugunthKumar/MKiCloudSync
-
Notifications
You must be signed in to change notification settings - Fork 0
/
MKiCloudSync.m
106 lines (77 loc) · 3.83 KB
/
MKiCloudSync.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
//
// MKiCloudSync.m
// iCloud1
//
// Created by Mugunth Kumar on 20/11/11.
// Copyright (c) 2011 Steinlogic. All rights reserved.
// As a side note on using this code, you might consider giving some credit to me by
// 1) linking my website from your app's website
// 2) or crediting me inside the app's credits page
// 3) or a tweet mentioning @mugunthkumar
// 4) A paypal donation to [email protected]
//
// A note on redistribution
// if you are re-publishing after editing, please retain the above copyright notices
#import "MKiCloudSync.h"
@implementation MKiCloudSync
+(void) updateToiCloud:(NSNotification*) notificationObject {
#ifdef DEBUG
NSLog(@"Updating to iCloud");
#endif
NSDictionary *dict = [[NSUserDefaults standardUserDefaults] dictionaryRepresentation];
[dict enumerateKeysAndObjectsUsingBlock:^(id key, id obj, BOOL *stop) {
[[NSUbiquitousKeyValueStore defaultStore] setObject:obj forKey:key];
}];
[[NSUbiquitousKeyValueStore defaultStore] synchronize];
}
+(void) updateFromiCloud:(NSNotification*) notificationObject {
#ifdef DEBUG
NSLog(@"Updating from iCloud");
#endif
NSUbiquitousKeyValueStore *iCloudStore = [NSUbiquitousKeyValueStore defaultStore];
NSDictionary *dict = [iCloudStore dictionaryRepresentation];
// prevent NSUserDefaultsDidChangeNotification from being posted while we update from iCloud
[[NSNotificationCenter defaultCenter] removeObserver:self
name:NSUserDefaultsDidChangeNotification
object:nil];
[dict enumerateKeysAndObjectsUsingBlock:^(id key, id obj, BOOL *stop) {
[[NSUserDefaults standardUserDefaults] setObject:obj forKey:key];
}];
[[NSUserDefaults standardUserDefaults] synchronize];
// enable NSUserDefaultsDidChangeNotification notifications again
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(updateToiCloud:)
name:NSUserDefaultsDidChangeNotification object:nil];
[[NSNotificationCenter defaultCenter] postNotificationName:kMKiCloudSyncNotification object:nil];
}
+(void) start {
if(NSClassFromString(@"NSUbiquitousKeyValueStore")) { // is iOS 5?
if([NSUbiquitousKeyValueStore defaultStore]) { // is iCloud enabled
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(updateFromiCloud:)
name:NSUbiquitousKeyValueStoreDidChangeExternallyNotification
object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(updateToiCloud:)
name:NSUserDefaultsDidChangeNotification object:nil];
#ifdef DEBUG
} else {
NSLog(@"iCloud not enabled");
#endif
}
}
#ifdef DEBUG
else {
NSLog(@"Not an iOS 5 device");
}
#endif
}
+ (void) dealloc {
[[NSNotificationCenter defaultCenter] removeObserver:self
name:NSUbiquitousKeyValueStoreDidChangeExternallyNotification
object:nil];
[[NSNotificationCenter defaultCenter] removeObserver:self
name:NSUserDefaultsDidChangeNotification
object:nil];
}
@end