MaxHasADHD / TraktKit

Swift wrapper for Trakt.tv API.
MIT License
110 stars 37 forks source link

Keychain error #51

Open MaxHasADHD opened 4 years ago

MaxHasADHD commented 4 years ago

I discovered that if you access the keychain soon after launching / coming to foreground keychain will return an error instead of the data. To fix this I'll be keeping the accessToken and refreshToken is memory instead of fetching from keychain every time. I'll also be changing the kSecAttrAccessible to kSecAttrAccessibleAfterFirstUnlock. As for getting the key ASAP after app launch I can only recommend trying to delay accessing the token.

Some threads on the issue https://forums.developer.apple.com/thread/4743 https://forums.developer.apple.com/message/351282#351282

I haven't found any other solutions, but if anyone has any other suggestions I'm all ears.

MaxHasADHD commented 4 years ago

In the future I'd also love to allow apps to use their own storage to save / load the keys so you don't have to use the keychain code I wrote. I'll leave the keychain code in there though so no one is signed out. It'll just give you an extra option if you want something better or more custom.

MaxHasADHD commented 4 years ago

TraktKit 1.2.2 has been pushed up. Keychain code has been updated and will now search for a token with the new access mode kSecAttrAccessibleAfterFirstUnlock, if it not found it will update the existing values. Some other changes were made like when adding an item to keychain can now handle updating existing items if they exist instead of deleting them first. accessToken and refreshToken are cached in memory now too and are updated when those values are changed. We'll only check the keychain if the cached value is nil. signOut now also clears the refreshToken and expiration date. I'm keeping isSignedIn returning if accessToken is nil or not, but I also recommend perhaps checking if the expiration date exists in user defaults which might help determine if you're logged in if keychain decides to return a false error.