diff --git a/FTiCloudSync.podspec b/FTiCloudSync.podspec new file mode 100644 index 0000000..a06b441 --- /dev/null +++ b/FTiCloudSync.podspec @@ -0,0 +1,14 @@ +Pod::Spec.new do |s| + s.name = 'FTiCloudSync' + s.version = '0.0.1' + s.license = { :type => 'Creative Commons-Attribution-ShareAlike 3.0 Unported', :text => 'You are free to share, adapt and make commercial use of the work as long as\nyou give attribution and keep this license. To give credit, we suggest this\ntext in the about screen or App Store description: \"Uses FTiCloudSync by\nOrtwin Gentz\", with a link to the GitHub page.\n\nIf you need a different license without attribution requirement, please\ncontact me and we can work something out.\n' } + s.summary = 'Automatically syncs NSUserDefaults across multiple iOS devices using iCloud' + s.homepage = 'https://github.com/futuretap/FTiCloudSync' + s.author = { 'Luc Vandal' => 'http://www.futuretap.com/contact/', + 'Ortwin Gentz' => 'http://edovia.com/company/#contact_form' } + s.source = { :git => 'https://github.com/futuretap/FTiCloudSync.git', :commit => 'f28114f9ecb838d5fa9076da19e4acf5846d67b3' } + s.platform = :ios + s.dependency 'RegexKitLite' + s.requires_arc = false + s.source_files = 'NSUserDefaults+iCloud.{h,m}' , 'MethodSwizzling.{c,h}' +end \ No newline at end of file diff --git a/NSUserDefaults+iCloud.h b/NSUserDefaults+iCloud.h index 11b19e3..c706ac2 100644 --- a/NSUserDefaults+iCloud.h +++ b/NSUserDefaults+iCloud.h @@ -5,7 +5,7 @@ // Copyright (c) 2012: // Ortwin Gentz, FutureTap GmbH, http://www.futuretap.com // All rights reserved. -// +// // Licensed under CC-BY-SA 3.0: http://creativecommons.org/licenses/by-sa/3.0/ // You are free to share, adapt and make commercial use of the work as long as you give attribution and keep this license. // To give credit, we suggest this text: "Uses FTiCloudSync by Ortwin Gentz", with a link to the GitHub page @@ -15,6 +15,7 @@ extern NSString* const FTiCloudSyncDidUpdateNotification; extern NSString* const FTiCloudSyncChangedKeys; extern NSString* const FTiCloudSyncRemovedKeys; +extern NSString* FTiCloudIgnoreListRegex; @interface NSUserDefaults(iCloud) + (void)updateFromiCloud:(NSNotification*) notificationObject; diff --git a/NSUserDefaults+iCloud.m b/NSUserDefaults+iCloud.m index 738af69..dcd8802 100644 --- a/NSUserDefaults+iCloud.m +++ b/NSUserDefaults+iCloud.m @@ -5,7 +5,7 @@ // Copyright (c) 2012: // Ortwin Gentz, FutureTap GmbH, http://www.futuretap.com // All rights reserved. -// +// // Licensed under CC-BY-SA 3.0: http://creativecommons.org/licenses/by-sa/3.0/ // You are free to share, adapt and make commercial use of the work as long as you give attribution and keep this license. // To give credit, we suggest this text: "Uses FTiCloudSync by Ortwin Gentz", with a link to the GitHub page @@ -19,6 +19,7 @@ NSString* const FTiCloudSyncChangedKeys = @"changedKeys"; NSString* const FTiCloudSyncRemovedKeys = @"removedKeys"; NSString* const iCloudBlacklistRegex = @"(^!|^Apple|^ATOutputLevel|Hockey|DateOfVersionInstallation|^MF|^NS|Quincy|^BIT|^TV|UsageTime|^Web|preferredLocaleIdentifier)"; +NSString* FTiCloudIgnoreListRegex = @""; @implementation NSUserDefaults(Additions) @@ -28,10 +29,10 @@ +(void)initialize { Swizzle([NSUserDefaults class], @selector(setObject:forKey:), @selector(my_setObject:forKey:)); Swizzle([NSUserDefaults class], @selector(removeObjectForKey:), @selector(my_removeObjectForKey:)); Swizzle([NSUserDefaults class], @selector(synchronize), @selector(my_synchronize)); - - [[NSNotificationCenter defaultCenter] addObserver:self - selector:@selector(updateFromiCloud:) - name:NSUbiquitousKeyValueStoreDidChangeExternallyNotification + + [[NSNotificationCenter defaultCenter] addObserver:self + selector:@selector(updateFromiCloud:) + name:NSUbiquitousKeyValueStoreDidChangeExternallyNotification object:nil]; } } @@ -45,9 +46,9 @@ + (void)updateFromiCloud:(NSNotification*)notificationObject { @synchronized(self) { NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults]; NSDictionary *dict = [[NSUbiquitousKeyValueStore defaultStore] dictionaryRepresentation]; - + [dict enumerateKeysAndObjectsUsingBlock:^(id key, id obj, BOOL *stop) { - if (![key isMatchedByRegex:iCloudBlacklistRegex] && ![[defaults valueForKey:key] isEqual:obj]) { + if (![key isMatchedByRegex:iCloudBlacklistRegex] && ![key isMatchedByRegex:FTiCloudIgnoreListRegex] && ![[defaults valueForKey:key] isEqual:obj]) { [defaults my_setObject:obj forKey:key]; // call original implementation [changedKeys addObject:key]; } @@ -56,7 +57,7 @@ + (void)updateFromiCloud:(NSNotification*)notificationObject { removedKeys = [NSMutableArray arrayWithArray:[defaults dictionaryRepresentation].allKeys]; [removedKeys removeObjectsInArray:dict.allKeys]; [removedKeys enumerateObjectsUsingBlock:^(id key, NSUInteger idx, BOOL *stop) { - if (![key isMatchedByRegex:iCloudBlacklistRegex]) { + if (![key isMatchedByRegex:iCloudBlacklistRegex] && ![key isMatchedByRegex:FTiCloudIgnoreListRegex]) { [defaults my_removeObjectForKey:key]; // non-swizzled/original implementation } }]; @@ -71,7 +72,7 @@ + (void)updateFromiCloud:(NSNotification*)notificationObject { - (void)my_setObject:(id)object forKey:(NSString *)key { BOOL equal = [[self objectForKey:key] isEqual:object]; [self my_setObject:object forKey:key]; - if (!equal && ![key isMatchedByRegex:iCloudBlacklistRegex] && [NSUbiquitousKeyValueStore defaultStore]) { + if (!equal && ![key isMatchedByRegex:iCloudBlacklistRegex] && ![key isMatchedByRegex:FTiCloudIgnoreListRegex] && [NSUbiquitousKeyValueStore defaultStore] && [key length] <= 64) { dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_LOW, 0), ^{ [[NSUbiquitousKeyValueStore defaultStore] setObject:object forKey:key]; }); @@ -82,7 +83,7 @@ - (void)my_removeObjectForKey:(NSString *)key { BOOL exists = !![self objectForKey:key]; [self my_removeObjectForKey:key]; // call original implementation - if (exists && ![key isMatchedByRegex:iCloudBlacklistRegex] && [NSUbiquitousKeyValueStore defaultStore]) { + if (exists && ![key isMatchedByRegex:iCloudBlacklistRegex] && ![key isMatchedByRegex:FTiCloudIgnoreListRegex] && [NSUbiquitousKeyValueStore defaultStore] && [key length] <= 64) { dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_LOW, 0), ^{ [[NSUbiquitousKeyValueStore defaultStore] removeObjectForKey:key]; });