Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for custom ignore regex #3

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions FTiCloudSync.podspec
Original file line number Diff line number Diff line change
@@ -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
3 changes: 2 additions & 1 deletion NSUserDefaults+iCloud.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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;
Expand Down
21 changes: 11 additions & 10 deletions NSUserDefaults+iCloud.m
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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)

Expand All @@ -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];
}
}
Expand All @@ -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];
}
Expand All @@ -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
}
}];
Expand All @@ -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];
});
Expand All @@ -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];
});
Expand Down