jrendel / SwiftKeychainWrapper

A simple wrapper for the iOS Keychain to allow you to use it in a similar fashion to User Defaults. Written in Swift.
MIT License
1.59k stars 340 forks source link

XCTestCase: set NewValue return False #176

Open javierbc121086 opened 2 years ago

javierbc121086 commented 2 years ago

Hi, I try save string data on keychain, however, the function set return False.

Any idea ?

I post the code:

    public static var tokenSession: String {
        set {
            KeychainWrapper.standard.set(
                newValue,
                forKey: "sm_token_session_key",
                withAccessibility: .always,
                isSynchronizable: true
            )
        }
        get {
            KeychainWrapper.standard.string(
                forKey: "sm_token_session_key",
                withAccessibility: .always,
                isSynchronizable: true
            ) ?? .empty
        }
    }
mrmurphy commented 2 years ago

@javierbc121086 it might work if you remove isSynchronizable. I'm guessing there's some info plist permission that needs to be added for sync to work. Maybe https://developer.apple.com/documentation/security/keychain_services/keychain_items/sharing_access_to_keychain_items_among_a_collection_of_apps?

mrmurphy commented 2 years ago

Ah note, there must be something else going on here. The reason I wasn't getting anything back in my code was that I wasn't passing isSynchronizable to both the getter and the setter, just the setter. They both need it.