Skip to content

Instantly share code, notes, and snippets.

@danielpunkass
Created September 11, 2012 21:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save danielpunkass/3702206 to your computer and use it in GitHub Desktop.
Save danielpunkass/3702206 to your computer and use it in GitHub Desktop.
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