Skip to content

Commit

Permalink
First commit
Browse files Browse the repository at this point in the history
  • Loading branch information
MKSG-MugunthKumar committed Nov 20, 2011
0 parents commit 7eb9358
Show file tree
Hide file tree
Showing 3 changed files with 124 additions and 0 deletions.
23 changes: 23 additions & 0 deletions MKiCloudSync.h
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
101 changes: 101 additions & 0 deletions MKiCloudSync.m
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 added README.mdown
Empty file.

0 comments on commit 7eb9358

Please sign in to comment.