Skip to content
This repository has been archived by the owner on Jan 10, 2023. It is now read-only.

Cannot seem to save two keys #3

Closed
hernan43 opened this issue Nov 8, 2014 · 6 comments
Closed

Cannot seem to save two keys #3

hernan43 opened this issue Nov 8, 2014 · 6 comments

Comments

@hernan43
Copy link

hernan43 commented Nov 8, 2014

If I do the following code:

        KeychainWrapper.serviceName = "com.yaddayadda.whatever"

        if KeychainWrapper.setString(tokenField.stringValue, forKey: "apiKey") {
            println("Saved \(tokenField.stringValue) to apiKey")
        } else {
            println("Failed to save apiKey: \(tokenField.stringValue)")
        }

        if KeychainWrapper.setString(urlField.stringValue, forKey: "apiURL") {
            println("Saved \(urlField.stringValue) to apiURL")
        } else {
            println("Failed to save apiURL: \(urlField.stringValue)")
        }

The apiURL will always fail no matter what. Is it just that this lib only allows saving of a single item?

@jrendel
Copy link
Owner

jrendel commented Nov 10, 2014

This lib should work fine saving multiple values. I'll try and dig into this sometime today or tonight and see whats up.

@hernan43
Copy link
Author

Let me preface this by saying I'm a total beginner at Swift programming so forgive my ignorance in advance. I did some low grade debugging and noticed that in setData:

let status: OSStatus = SecItemAdd(keychainQueryDictionary, nil)

        if status == errSecSuccess {
            return true
        } else if status == errSecDuplicateItem {
            return self.updateData(value, forKey: keyName)
        } else {
            return false
        }

It seems like that "apiURL" section always gets a errSecDuplicateItem status.

@jrendel
Copy link
Owner

jrendel commented Nov 10, 2014

I just threw in a couple more tests to cover adding multiple strings and updating the save key with different strings. Both are working for me. I'd suggest pulling down my updates and running the tests to make sure you're getting the same results.

For the duplicate issue, are you sure your key names are unique? If you try to save twice to the same key name, you'll hit the duplicate item error status and its going to instead perform an update. Perhaps while debugging, when you get to the duplicate item status return, check what the keyName value is on the next line each time and see if you're saving to the same keyName by accident.

@hernan43
Copy link
Author

The code at the top is exactly what I am using. But I did think of something. I am using this in an OS X application and not in an iOS app. I wonder if the underlying API is different enough on the two platforms that it is causing issues.

@jrendel
Copy link
Owner

jrendel commented Nov 11, 2014

I haven't actually tested this with OS X. But I know there are differences between how keychain is handled on OS X and iOS, so that is most likely the cause of you issue.

@jrendel
Copy link
Owner

jrendel commented Dec 9, 2014

Closing, as the issue is related to accessing Keychain in OS X, which is not the same as on iOS and out of scope of this library for now.

@jrendel jrendel closed this as completed Dec 9, 2014
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants