尝试使用SecItemAdd()将项添加到密钥链,结果为-50

bvn4nwqk  于 2022-10-04  发布在  Swift
关注(0)|答案(1)|浏览(159)

当我尝试在命令行SWIFT应用程序中使用带有参数kSecAttrSynchronizableSecItemAdd()向MacOS密钥链添加项时,收到错误-50 (One or more parameters passed to the function were not valid)。如果我移除kSecAttrSynchronizable,它可以正常工作。我尝试使用KeychainAccess并尝试手动操作(使用MacOS提供的密钥链服务API),结果是一样的。

import Foundation
let account = "username"
let password = "password".data(using: String.Encoding.utf8)!

var query: [String: Any] = [kSecClass as String: kSecClassInternetPassword,
                        kSecAttrAccount as String: account,
                        kSecAttrServer as String: "nabeelomer.me",
                        kSecAttrSynchronizable as String: kCFBooleanTrue,
                        kSecValueData as String: password]

let status = SecItemAdd(query as CFDictionary, nil)
print(status)

应用程序是否需要未记录的权限?

SWIFT 4.0、MacOS 10.13.3、Xcode 9.1

lrpiutwd

lrpiutwd1#

kSecValueData需要是Data,而不是String

试试看

kSecValueData: password.data(using: .utf8)!

相关问题