当我尝试在命令行SWIFT应用程序中使用带有参数kSecAttrSynchronizable
的SecItemAdd()
向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
1条答案
按热度按时间lrpiutwd1#
kSecValueData
需要是Data
,而不是String
试试看
kSecValueData: password.data(using: .utf8)!