From d62011c7496d78366929882905fce6203e22fd29 Mon Sep 17 00:00:00 2001 From: Andrew Kitchen Date: Wed, 26 Mar 2025 13:18:30 -0700 Subject: [PATCH] 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. --- keycastr/KCColorValueTransformer.h | 1 + keycastr/KCUserDefaultsMigration.m | 31 ++++++++++++++++-------------- 2 files changed, 18 insertions(+), 14 deletions(-) diff --git a/keycastr/KCColorValueTransformer.h b/keycastr/KCColorValueTransformer.h index 84da036..a4997fd 100644 --- a/keycastr/KCColorValueTransformer.h +++ b/keycastr/KCColorValueTransformer.h @@ -29,6 +29,7 @@ NS_ASSUME_NONNULL_BEGIN +API_AVAILABLE(macos(10.14)) @interface KCColorValueTransformer : NSSecureUnarchiveFromDataTransformer @end diff --git a/keycastr/KCUserDefaultsMigration.m b/keycastr/KCUserDefaultsMigration.m index e507ae3..8f9593e 100644 --- a/keycastr/KCUserDefaultsMigration.m +++ b/keycastr/KCUserDefaultsMigration.m @@ -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]; + } } }