SwiftUI Color Issue

I have ran into an issue that is illustrated by the code in the following GitHub repository.

https://github.com/dougholland/ColorTest

When a SwiftUI color originates from the ColorPicker it can be persisted correctly and renders the same as the original color. When the color originates from the MapFeature.backgroundColor, it is always rendered with the light appearance version of the color and not the dark appearance version. The readme in the GitHub repo has screenshots that show this.

Any assistance would be greatly appreciated as this is affecting an app that is in development and I'd like to resolve this before the app is released.

If this is caused by a framework bug, any possible workaround would be greatly appreciated also. I suspect it maybe a framework issue, possibly with some code related to the MapFeature.backgroundColor, because the issue does not occur when the color originates from the ColorPicker.

Your sample project has this function:

override func transformedValue(_ value: Any?) -> Any? {
        guard let color = value as? UIColor else { return nil }
        
        do {
            let data = try NSKeyedArchiver.archivedData(withRootObject: color, requiringSecureCoding: true)
            
            return data
        } catch {
            assertionFailure("assertion failure: \(error)")
            
            return nil
        }
    }

You are archiving a UIColor's raw underlying data, which depends on its iOS version specific implementation details here. UIKit objects are not safe to directly archive yourself — leaving aside the color correctness issue you're inquiring about here, if the system changes the implementation of a UIKit class that you archive, you may not be be able to unarchive that data in the future, since the implementation details encoded in the archived object changed between the time the archive was created with one iOS version and the unarchive attempt on another iOS version.

To safely store the color here, I suggest you store its constituent parts instead.

— Ed Ford,  DTS Engineer

I'm really looking for a way to store the MapFeature.backgroundColor, such that restoring the color, when the app is in dark appearance, results in the same color.

Have you tried resolving the color through either a ColorScheme or a UITraitCollection?

However, beyond that, what are you trying to accomplish here? If you're trying to precisely match the system UI from the map by storing these colors, what happens if the system tweaks the colors it uses? The color values on the system point of view aren't guaranteed to always stay exactly the same. You might be better off storing the place, and then query the system for the color rather than storing the color value.

—Ed Ford,  DTS Engineer

SwiftUI Color Issue
 
 
Q