evgenyneu / keychain-swift

Helper functions for saving text in Keychain securely for iOS, OS X, tvOS and watchOS.
MIT License
2.82k stars 345 forks source link

Keychain not syncing #127

Open emrahgunduz opened 4 years ago

emrahgunduz commented 4 years ago

I've been trying to solve this problem for days now. Currently I'm saving a user id and device id to keychain so if the user installs the app to another device, he/she can access the data from our backend without any problems.

I'm developing on an iPhone, and the second device is an iPad. When I first tested and synced keys, iPad loaded them as expected and everything worked fine. But then, syncing stopped when I started deleting or updating the keys while continuing the development.

Currently both devices are not updating each other.

I tried deleting all the keys, disabling/enabling keychain on both devices, logging out and in again, resetting and logging in. Nothing seems to work. I am unable to continue to progress on the app.

I'm currently stuck with this problem. As you are more experienced with the keychain functionality, you might have seen a similar error before. Do you have a solution or idea for where to look at?

Thanks.

This is how I'm writing to keychain:

  static func write (key: SettingKey, value: String, syncronizable: Bool = true) -> Void {
    let keychain = KeychainSwift()
    keychain.accessGroup = Settings.accessGroup
    keychain.synchronizable = syncronizable
    keychain.set(value, forKey: key.rawValue, withAccess: .accessibleAfterFirstUnlock)

    if keychain.lastResultCode != noErr {
      let error = KeychainError(status: keychain.lastResultCode)
      appSimple("🔑 ⚠️ Keychain error for \"\(key.name())\" -> \(error.description)")
    }
  }

And this is the read code:

  static func read (key: SettingKey, syncronizable: Bool = true) -> String? {
    let keychain = KeychainSwift()
    keychain.accessGroup = Settings.accessGroup
    keychain.synchronizable = syncronizable

    let value: String? = keychain.get(key.rawValue)

    if keychain.lastResultCode != noErr {
      let error = KeychainError(status: keychain.lastResultCode)
      appSimple("🔑 ⚠️ Keychain error for \"\(key.name())\" -> \(error.description)")
      return nil
    }

    return value
  }