// // KeyChain.swift // GME Remit // // Created by InKwon Devik Kim on 08/04/2019. // Copyright © 2019 Gobal Money Express Co. Ltd. All rights reserved. // import Foundation import KeychainAccess final class KeyChain { enum KeyType: String { case password case id case biometricAuth case login case temporaryID case temporaryPW } static let shared = KeyChain() private var keychain: Keychain private init() { self.keychain = Keychain(service: "com.gme.gmeremit") // There is no userId, it's logout or remove app all and reinstall app. if GMEDB.shared.user.string(.userId) == nil { try? keychain.removeAll() } } func save(data: String, key: KeyType) { do { try self.keychain.set(data, key: key.rawValue) } catch let error { print("keychain save error: \(error.localizedDescription)") } } func get(key: KeyType) -> String? { guard let pw = ((try? self.keychain.get(key.rawValue)) as String??) else { return nil } return pw } func remove(key: KeyType) { do { try self.keychain.remove(key.rawValue) } catch let error { print("keychain remove error: \(error.localizedDescription)") } } func removeAll() { do { try self.keychain.removeAll() } catch let error { print("keychain remove error: \(error.localizedDescription)") } } }