From cad03b92a5949bcc16f860c045ac380fa05038f7 Mon Sep 17 00:00:00 2001 From: Olivier Carbonneau Date: Sat, 12 Oct 2013 11:55:06 -0400 Subject: [PATCH 1/3] Add support for custom ignore regex --- NSUserDefaults+iCloud.h | 3 ++- NSUserDefaults+iCloud.m | 21 +++++++++++---------- 2 files changed, 13 insertions(+), 11 deletions(-) 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..5c494f1 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]) { 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]) { dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_LOW, 0), ^{ [[NSUbiquitousKeyValueStore defaultStore] removeObjectForKey:key]; }); From 64e91bc1c2c479fcd623867a54ff341a61ded958 Mon Sep 17 00:00:00 2001 From: Olivier Carbonneau Date: Thu, 5 Jun 2014 21:37:11 -0400 Subject: [PATCH 2/3] Prevent a crash when key is longer than 64 caracters --- NSUserDefaults+iCloud.m | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/NSUserDefaults+iCloud.m b/NSUserDefaults+iCloud.m index 5c494f1..dcd8802 100644 --- a/NSUserDefaults+iCloud.m +++ b/NSUserDefaults+iCloud.m @@ -72,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] && ![key isMatchedByRegex:FTiCloudIgnoreListRegex] && [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]; }); @@ -83,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] && ![key isMatchedByRegex:FTiCloudIgnoreListRegex] && [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]; }); From dd330ee9289aa7e722cd25caa65e38b664bb1ccd Mon Sep 17 00:00:00 2001 From: Olivier Carbonneau Date: Tue, 18 Nov 2014 08:44:09 -0500 Subject: [PATCH 3/3] add pod spec --- FTiCloudSync.podspec | 14 ++++++++++++++ 1 file changed, 14 insertions(+) create mode 100644 FTiCloudSync.podspec 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