首页 > 解决方案 > Swift:注销后无法再次登录钥匙串

问题描述

这是使用 KeychainService 的正确方法吗?

我第一次登录没有问题,但是在我注销并尝试再次登录后。状态返回 403 禁止。如何删除会话 ID?

public class KeychainService: NSObject {
    class func savePassword(service: String, account: String, data: String) {
        if let dataFromString = data.data(using: String.Encoding.utf8, allowLossyConversion: false) {
            let keychainQuery: NSMutableDictionary = NSMutableDictionary(objects: [kSecClassGenericPasswordValue, service, account, dataFromString], forKeys: [kSecClassValue, kSecAttrServiceValue, kSecAttrAccountValue, kSecValueDataValue])

            let status = SecItemAdd(keychainQuery as CFDictionary, nil)
            if (status != errSecSuccess) {
            }
            print("success")
        }
    }

    class func removePassword(service: String, account: String) {
        let keychainQuery: NSMutableDictionary = NSMutableDictionary(objects: [kSecClassGenericPasswordValue, service, account, kCFBooleanTrue, kSecMatchLimitOneValue], forKeys: [kSecClassValue, kSecAttrServiceValue, kSecAttrAccountValue, kSecReturnDataValue, kSecMatchLimitValue])
        var dataTypeRef: AnyObject?
        let status: OSStatus = SecItemCopyMatching(keychainQuery, &dataTypeRef)

        if status == errSecSuccess {
            let statusDelete = SecItemDelete(keychainQuery as CFDictionary)
            if (statusDelete != errSecSuccess) {

            }
        } else {
            print("failed")
        }
    }
}

标签: iosarrayskeychain

解决方案


推荐阅读