微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

SWIFT:钥匙串中的更新kSecValueData失败

如何解决SWIFT:钥匙串中的更新kSecValueData失败

我正在尝试更新现有钥匙串条目中的密码,但失败并显示以下消息:

无法更新钥匙串中的令牌:传递给函数一个或多个参数无效。

我可以毫无问题地更新帐户名称,还可以创建钥匙串元素。不知道为什么更新失败。我在MacOs Catalina上使用XCode 11.6。

我们将不胜感激:-)

 // try to update first
        let updateQuery: [String: Any] = [kSecclass as String: kSecclassGenericPassword,kSecAttrService as String: LocalKeychainStorage.KEYCHAIN_LABEL,kSecAttrAccount as String: LocalKeychainStorage.ACCOUNT_LABEL
                                        ]
        let attributes: [String: Any] = [kSecValueData as String: credentials.token]
        let status = SecItemUpdate(updateQuery as CFDictionary,attributes as CFDictionary)

        guard status != errSecItemNotFound else {
            // try to add it,as it was not found
            let addQuery: [String: Any] =  [kSecclass as String: kSecclassGenericPassword,kSecAttrAccount as String: LocalKeychainStorage.ACCOUNT_LABEL,kSecValueData as String: credentials.token]
            let status = SecItemAdd(addQuery as CFDictionary,nil)
            guard status == errSecSuccess else {
                throw LocalKeychainStorageError.keychainAddFailed(status: status)
            }
            print("Add to keystore for \(LocalKeychainStorage.KEYCHAIN_LABEL) successful")
            return
        }
        guard status == errSecSuccess else {
            let errorString: String = SeccopyErrorMessageString(status,nil)! as String
            
            throw LocalKeychainStorageError.keychainUpdateFailed(status: errorString)
        }
        print("Update of keystore for \(LocalKeychainStorage.KEYCHAIN_LABEL) successful")
    }

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。