Swift钥匙串KeychainSwift的使用

令狐经武
2023-12-01
pod 'KeychainSwift', '12.0.0'
import Foundation
import KeychainSwift

class KeyChainManager {
    
    static let shared = KeyChainManager.init()
    
    private let keychain = KeychainSwift(keyPrefix: Dex4DKeys.keychainKeyPrefix)
    
    private let option = KeychainSwiftAccessOptions.accessibleWhenUnlockedThisDeviceOnly
    
    private let pinKey = "AppPin"
    
    var hasPin: Bool {
        if let _ = getPin() {
            return true
        } else {
            return false
        }
    }
    
    func getPin() -> String? {
        return keychain.get(pinKey)
    }
    
    func setPin(value: String) {
        keychain.set(value, forKey: pinKey, withAccess: option)
    }
    
    func getValue(for key: String) -> String? {
        return keychain.get(key)
    }
    
    func set(value: String, for key: String) {
        keychain.set(value, forKey: key, withAccess: option)
    }

}
 类似资料: