Skip to content
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
46 changes: 20 additions & 26 deletions Externals/RMSharedPreferences/NSURL+RMApplicationGroup.m
Original file line number Diff line number Diff line change
Expand Up @@ -23,38 +23,32 @@

#import "NSURL+RMApplicationGroup.h"

#import <Security/Security.h>
#import <Security/SecCode.h>
#import <pwd.h>

@implementation NSURL (RMApplicationGroup)

+ (NSString *)defaultGroupContainerIdentifier
{
SecTaskRef task = NULL;

NSString *applicationGroupIdentifier = nil;
do {
task = SecTaskCreateFromSelf(kCFAllocatorDefault);
if (task == NULL) {
break;
}

CFTypeRef applicationGroupIdentifiers = SecTaskCopyValueForEntitlement(task, CFSTR("com.apple.security.application-groups"), NULL);
if (applicationGroupIdentifiers == NULL || CFGetTypeID(applicationGroupIdentifiers) != CFArrayGetTypeID() || CFArrayGetCount(applicationGroupIdentifiers) == 0) {
break;
}

CFTypeRef firstApplicationGroupIdentifier = CFArrayGetValueAtIndex(applicationGroupIdentifiers, 0);
if (CFGetTypeID(firstApplicationGroupIdentifier) != CFStringGetTypeID()) {
break;
}

applicationGroupIdentifier = (__bridge NSString *)firstApplicationGroupIdentifier;
} while (0);

if (task != NULL) {
CFRelease(task);
}
NSString* applicationGroupIdentifier = nil;

SecCodeRef selfCode = NULL;
SecCodeCopySelf(kSecCSDefaultFlags, &selfCode);

if (selfCode)
{
CFDictionaryRef cfDic = NULL;
SecCodeCopySigningInformation(selfCode, kSecCSRequirementInformation, &cfDic);
NSDictionary* signingDic = CFBridgingRelease(cfDic);

NSDictionary* entitlementsDic = [signingDic objectForKey:@"entitlements-dict"];
NSArray* appGroupsArray = [entitlementsDic objectForKey:@"com.apple.security.application-groups"];

if ([appGroupsArray isKindOfClass:[NSArray class]] && appGroupsArray.count > 0)
applicationGroupIdentifier = [appGroupsArray objectAtIndex:0];

CFRelease(selfCode);
}

return applicationGroupIdentifier;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -206,7 +206,7 @@
};
buildConfigurationList = 6D74A055166373B400D70B8F /* Build configuration list for PBXProject "RMSharedPreferences" */;
compatibilityVersion = "Xcode 3.2";
developmentRegion = en-GB;
developmentRegion = "en-GB";
hasScannedForEncodings = 0;
knownRegions = (
en,
Expand Down Expand Up @@ -286,7 +286,7 @@
GCC_WARN_ABOUT_RETURN_TYPE = YES;
GCC_WARN_UNINITIALIZED_AUTOS = YES;
GCC_WARN_UNUSED_VARIABLE = YES;
MACOSX_DEPLOYMENT_TARGET = 10.8;
MACOSX_DEPLOYMENT_TARGET = 10.7.5;
ONLY_ACTIVE_ARCH = YES;
SDKROOT = macosx;
WARNING_CFLAGS = "-Wall";
Expand All @@ -311,7 +311,7 @@
GCC_WARN_ABOUT_RETURN_TYPE = YES;
GCC_WARN_UNINITIALIZED_AUTOS = YES;
GCC_WARN_UNUSED_VARIABLE = YES;
MACOSX_DEPLOYMENT_TARGET = 10.8;
MACOSX_DEPLOYMENT_TARGET = 10.7.5;
SDKROOT = macosx;
WARNING_CFLAGS = "-Wall";
};
Expand Down
2 changes: 2 additions & 0 deletions Externals/RMSharedPreferences/RMSharedUserDefaults.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@
*/
- (id)initWithApplicationGroupIdentifier:(NSString *)applicationGroupIdentifier;

- (id) initWithSharedFileURL:(NSURL *)fileURL;

@end

extern NSString * const RMSharedUserDefaultsDidChangeDefaultNameKey;
Expand Down
55 changes: 36 additions & 19 deletions Externals/RMSharedPreferences/RMSharedUserDefaults.m
Original file line number Diff line number Diff line change
Expand Up @@ -74,11 +74,6 @@ + (void)resetStandardUserDefaults

- (id)initWithApplicationGroupIdentifier:(NSString *)applicationGroupIdentifier
{
self = [super initWithUser:nil];
if (self == nil) {
return nil;
}

NSURL *applicationGroupLocation = [NSURL containerURLForSecurityApplicationGroupIdentifier:applicationGroupIdentifier];
if (applicationGroupLocation == nil) {
@throw [NSException exceptionWithName:NSInternalInconsistencyException reason:@"A default application group identifier cannot be found in the entitlements" userInfo:nil];
Expand All @@ -89,27 +84,49 @@ - (id)initWithApplicationGroupIdentifier:(NSString *)applicationGroupIdentifier
[[NSFileManager defaultManager] createDirectoryAtURL:applicationGroupPreferencesLocation withIntermediateDirectories:YES attributes:nil error:NULL];

NSString *userDefaultsDictionaryFileName = applicationGroupIdentifier ? : [NSURL defaultGroupContainerIdentifier];
_userDefaultsDictionaryLocation = [[applicationGroupPreferencesLocation URLByAppendingPathComponent:userDefaultsDictionaryFileName] URLByAppendingPathExtension:@"plist"];

NSURL* defaultsLocation = [[applicationGroupPreferencesLocation URLByAppendingPathComponent:userDefaultsDictionaryFileName] URLByAppendingPathExtension:@"plist"];

self = [self initWithSharedFileURL:defaultsLocation];
if (self == nil) {
return nil;
}

return self;
}

- (id) initWithSharedFileURL:(NSURL *)fileURL
{
self = [super initWithUser:nil];
if (self == nil) {
return nil;
}

if ( ! fileURL) {
@throw [NSException exceptionWithName:NSInternalInconsistencyException reason:@"Shared defaults file name not specified" userInfo:nil];
return nil;
}

_userDefaultsDictionaryLocation = fileURL;

_updatedUserDefaultsDictionary = [NSMutableDictionary dictionary];
_registeredUserDefaultsDictionary = [NSMutableDictionary dictionary];

_accessorLock = [[NSRecursiveLock alloc] init];
_synchronizeLock = [[NSLock alloc] init];
NSString *queuePrefixName = [applicationGroupIdentifier stringByAppendingFormat:@".sharedpreferences"];

NSString *queuePrefixName = [fileURL.lastPathComponent stringByAppendingFormat:@".sharedpreferences"];

_fileCoordinationOperationQueue = [[NSOperationQueue alloc] init];
[_fileCoordinationOperationQueue setName:[queuePrefixName stringByAppendingFormat:@".filecoordination"]];

_synchronizationQueue = [[NSOperationQueue alloc] init];
[_synchronizationQueue setMaxConcurrentOperationCount:1];
[_synchronizationQueue setName:[queuePrefixName stringByAppendingFormat:@".synchronization"]];

[self _synchronize];

[NSFileCoordinator addFilePresenter:self];

return self;
}

Expand All @@ -130,15 +147,15 @@ - (id)objectForKey:(NSString *)defaultName
__block id object = nil;

[self _lock:[self accessorLock] criticalSection:^ {
object = [self updatedUserDefaultsDictionary][defaultName];
object = [[self updatedUserDefaultsDictionary] objectForKey:defaultName];
if (object != nil) {
return;
}
object = [self userDefaultsDictionary][defaultName];
object = [[self userDefaultsDictionary] objectForKey:defaultName];
if (object != nil) {
return;
}
object = [self registeredUserDefaultsDictionary][defaultName];
object = [[self registeredUserDefaultsDictionary] objectForKey:defaultName];
}];

return object;
Expand Down Expand Up @@ -351,7 +368,7 @@ - (NSDictionary *)__userDefaultsUpdates:(NSDictionary *)userDefaultsDictionary u
NSSet *userDefaultsUpdatesFromDisk = [self _keyDiffsBetweenDictionaries:userDefaultsDictionary :[self userDefaultsDictionary]];

[userDefaultsUpdatesFromDisk enumerateObjectsUsingBlock:^ (NSString *defaultName, BOOL *stop) {
id value = userDefaultsDictionary[defaultName] ? : [NSNull null];
id value = [userDefaultsDictionary objectForKey:defaultName] ? : [NSNull null];
[userDefaultsChanges setObject:value forKey:defaultName];
}];

Expand Down
4 changes: 2 additions & 2 deletions Sample Application/Helper/RMHelperAppDelegate.m
Original file line number Diff line number Diff line change
Expand Up @@ -41,10 +41,10 @@ - (void)applicationDidFinishLaunching:(NSNotification *)notification

- (void)preferencesDidUpdate:(NSNotification *)notification
{
NSString *defaultName = [notification userInfo][RMSharedUserDefaultsDidChangeDefaultNameKey];
NSString *defaultName = [[notification userInfo] objectForKey:RMSharedUserDefaultsDidChangeDefaultNameKey];

if ([defaultName isEqualToString:RMSharedPreferencesSomeTextDefaultKey]) {
NSString *text = [notification userInfo][RMSharedUserDefaultsDidChangeDefaulValueKey];
NSString *text = [[notification userInfo]objectForKey:RMSharedUserDefaultsDidChangeDefaulValueKey];
[self _updateTextField:text];
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -467,9 +467,8 @@
GCC_WARN_ABOUT_RETURN_TYPE = YES;
GCC_WARN_UNINITIALIZED_AUTOS = YES;
GCC_WARN_UNUSED_VARIABLE = YES;
MACOSX_DEPLOYMENT_TARGET = 10.8;
MACOSX_DEPLOYMENT_TARGET = 10.7.5;
ONLY_ACTIVE_ARCH = YES;
SDKROOT = macosx;
WARNING_CFLAGS = "-Wall";
};
name = Debug;
Expand All @@ -492,8 +491,7 @@
GCC_WARN_ABOUT_RETURN_TYPE = YES;
GCC_WARN_UNINITIALIZED_AUTOS = YES;
GCC_WARN_UNUSED_VARIABLE = YES;
MACOSX_DEPLOYMENT_TARGET = 10.8;
SDKROOT = macosx;
MACOSX_DEPLOYMENT_TARGET = 10.7.5;
WARNING_CFLAGS = "-Wall";
};
name = Release;
Expand Down