forked from MKSG-MugunthKumar/MKiCloudSync
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit 7eb9358
Showing
3 changed files
with
124 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
// | ||
// MKiCloudSync.h | ||
// 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 <Foundation/Foundation.h> | ||
#define kMKiCloudSyncNotification @"MKiCloudSyncDidUpdateToLatest" | ||
|
||
@interface MKiCloudSync : NSObject | ||
|
||
+(void) start; | ||
@end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,101 @@ | ||
// | ||
// 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 { | ||
|
||
NSLog(@"Updating to iCloud"); | ||
|
||
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 { | ||
|
||
NSLog(@"Updating from iCloud"); | ||
|
||
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]; | ||
} else { | ||
|
||
NSLog(@"iCloud not enabled"); | ||
|
||
} | ||
} | ||
else { | ||
|
||
NSLog(@"Not an iOS 5 device"); | ||
} | ||
} | ||
|
||
+ (void) dealloc { | ||
|
||
[[NSNotificationCenter defaultCenter] removeObserver:self | ||
name:NSUbiquitousKeyValueStoreDidChangeExternallyNotification | ||
object:nil]; | ||
|
||
[[NSNotificationCenter defaultCenter] removeObserver:self | ||
name:NSUserDefaultsDidChangeNotification | ||
object:nil]; | ||
} | ||
@end |
Empty file.