Prephirences icon indicating copy to clipboard operation
Prephirences copied to clipboard

Possible issue with keychain and archiving objects

Open jsm174 opened this issue 6 years ago • 2 comments

I believe I am having an issue when trying to get archived objects from the keychain.

They always come back as nil.

Take the following code:

        var preferences = UserDefaults.standard
        preferences["color", .archive] = UIColor.red
        let color = preferences["color", .archive]  as? UIColor
        print(color)
        
        var keychainPreferences = KeychainPreferences.sharedInstance
        keychainPreferences["color2", .archive] = UIColor.red
        let color2 = keychainPreferences["color2", .archive]  as? UIColor
        print(color2)

Results in:

Optional(UIExtendedSRGBColorSpace 1 0 0 1)
nil

jsm174 avatar May 30 '19 16:05 jsm174

thanks for the report

maybe keychainPreferences["color2", .archive] return a Data

phimage avatar May 30 '19 16:05 phimage

Did a little more testing and this version seems to work:

        let keychainPreferences = KeychainPreferences.sharedInstance
        keychainPreferences.set(objectToArchive: UIColor.red, forKey: "color3")
        
        let color3 = keychainPreferences.unarchiveObject(forKey: "color3")
        print(color3)

jsm174 avatar May 30 '19 16:05 jsm174