Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Importing from a newer prefs file
@implementation NSUserDefaults (RSFoundation)
+ (BOOL) rsImportPreferencesFromBundleID:(NSString*)otherBundleID ifNewerThanBundleID:(NSString*)ourBundleID
{
BOOL didMigrate = NO;
NSArray* libraryFolders = NSSearchPathForDirectoriesInDomains(NSLibraryDirectory, NSUserDomainMask, YES);
if ([libraryFolders count] > 0)
{
NSString* prefsFolder = [[libraryFolders objectAtIndex:0] stringByAppendingPathComponent:@"Preferences"];
NSString* otherPreferencesFileName = [otherBundleID stringByAppendingPathExtension:@"plist"];
NSString* ourPreferencesFileName = [ourBundleID stringByAppendingPathExtension:@"plist"];
NSString* otherPreferencesFile = [prefsFolder stringByAppendingPathComponent:otherPreferencesFileName];
NSString* ourPreferencesFile = [prefsFolder stringByAppendingPathComponent:ourPreferencesFileName];
NSFileManager* fm = [NSFileManager defaultManager];
if ([fm fileExistsAtPath:otherPreferencesFile])
{
NSDate* otherModDate = [fm lastModDateAtPath:otherPreferencesFile];
NSDate* ourModDate = [fm lastModDateAtPath:ourPreferencesFile];
if ((ourModDate == nil) || ([ourModDate compare:otherModDate] == NSOrderedAscending))
{
NSUserDefaults* sud = [NSUserDefaults standardUserDefaults];
NSDictionary *newerPrefs = [sud persistentDomainForName:otherBundleID];
if (newerPrefs != nil)
{
// Swap the old prefs in as our own
[sud setPersistentDomain:newerPrefs forName:ourBundleID];
[NSUserDefaults resetStandardUserDefaults];
// Log about it
NSLog(@"Found newer preferences in %@ - importing", otherPreferencesFileName);
didMigrate = YES;
}
}
}
}
return didMigrate;
}
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.