Created
September 11, 2012 21:27
-
-
Save danielpunkass/3702206 to your computer and use it in GitHub Desktop.
Importing from a newer prefs file
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@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