Avoid migrating user preferences on macOS 10.13

Otherwise user could lose their color pref or possibly crash, as new unarchiver mechanism and related base class don't exist.
This commit is contained in:
Andrew Kitchen 2025-03-26 13:18:30 -07:00
parent 5e8ff49868
commit d62011c749
2 changed files with 18 additions and 14 deletions

View file

@ -29,6 +29,7 @@
NS_ASSUME_NONNULL_BEGIN
API_AVAILABLE(macos(10.14))
@interface KCColorValueTransformer : NSSecureUnarchiveFromDataTransformer
@end

View file

@ -32,21 +32,24 @@
// TODO: migrate legacy keys from dot/namespaces to underscores, and audit for observation (KVC?)
+ (void)performMigration:(NSUserDefaults *)userDefaults {
NSArray *colorKeyNames = [self colorKeyNames];
for (NSString *colorKey in colorKeyNames) {
NSData *colorData = [userDefaults dataForKey:colorKey];
if (!colorData) {
continue;
}
// Only migrate if we're running on macOS 10.14 or later or the user may lose their color preference
if (@available(macOS 10.14, *)) {
NSArray *colorKeyNames = [self colorKeyNames];
for (NSString *colorKey in colorKeyNames) {
NSData *colorData = [userDefaults dataForKey:colorKey];
if (!colorData) {
continue;
}
// If the color can be unarchived by the deprecated unarchiver,
// we need to convert it.
NSColor *color = [NSUnarchiver unarchiveObjectWithData:colorData];
if (color) {
NSData *newColorData = [NSKeyedArchiver archivedDataWithRootObject:color
requiringSecureCoding:NO
error:NULL];
[userDefaults setObject:newColorData forKey:colorKey];
// If the color can be unarchived by the deprecated unarchiver,
// we need to convert it.
NSColor *color = [NSUnarchiver unarchiveObjectWithData:colorData];
if (color) {
NSData *newColorData = [NSKeyedArchiver archivedDataWithRootObject:color
requiringSecureCoding:NO
error:NULL];
[userDefaults setObject:newColorData forKey:colorKey];
}
}
}