Why my deviceId changes when saving it to the Keychain using KeychainSwift?

This is how I generate deviceId:

class Device {
    private let device: UIDevice
    private let keychain: Keychainable
    var deviceIdentifier: String {
        guard let identifier = keychain.decode(forKey: StorageKey.DeviceIdentifier) else {
            let uuid = device.identifierForVendor!.uuidString
            keychain.encode(value: uuid, forKey: StorageKey.DeviceIdentifier)
            return uuid
        }
        return identifier
    }
}

and this is how my keychain is defined:

import KeychainSwift

protocol Keychainable: AnyObject {
    func encode(value: String, forKey key: String)
    func decode(forKey key: String) -> String?
    func delete(forKey key: String)
}

class Keychain: Keychainable {
    private let keychain: KeychainSwift
    init(keychain: KeychainSwift) {
        self.keychain = keychain
    }

    func encode(value: String, forKey key: String) {
        keychain.set(value, forKey: key)
    }

    func decode(forKey key: String) -> String? {
        keychain.get(key) //🔥
    }

    func delete(forKey key: String) {
        keychain.delete(key)
    }
}

But for some reason through the time the app is used deviceIdentifier changes. Why? It definitely should not. Where is the issue?

My first idea is that in the line marked with 🔥 there is an issue while fetching string for key, and then it generates a new one. Is it possible?

Upvotes: 0

Views: 58

Answers (0)

Related Questions