无法将安全项保存到钥匙串

时间:2017-08-12 06:08:10

标签: swift security keychain

我正在尝试使用Swift 3将SecIdentity项目保存到钥匙串中。保存操作状态后总是0(成功),但是当我尝试检索保存的状态时,状态为-25300(这意味着对象不是存在)我做错了什么?

func saveIdentity(identity: SecIdentity) -> Data? {
    let str = "identity"

    let saveQuery = [
        kSecClass as String  : kSecClassIdentity,
        kSecValueRef as String : identity,
         kSecAttrLabel as String : "identity",
        kSecAttrAccessible as String : kSecAttrAccessibleAlways
        ] as [String : Any]

    var item: CFTypeRef? = nil

    var status: OSStatus = SecItemAdd(saveQuery as CFDictionary, nil)
    // Status = 0
    let loadQuery = [
        kSecClass as String  : kSecClassIdentity,
        kSecAttrLabel as String : "identity",
        kSecReturnRef as String : kCFBooleanTrue,
        kSecMatchLimit as String : kSecMatchLimitAll
        ] as [String : Any]

    status = SecItemCopyMatching(loadQuery as CFDictionary, &item)
    // Status = -25300
    if status == noErr {
        return item as! Data?
    } else {
        return nil
    }
}

0 个答案:

没有答案