0

Hello I am currently trying to query the keychain for my project without the kSecAttrAccountValue attribute and only using the kSecAttrServiceValue. Is this possible? The status says 0 however, the response is nil. I tried by adding an account and it works great! However, my goal is to obtain the result without it or all the results stored within the same service. Please let me know thank you!

Reference: Save and Load from KeyChain | Swift

Code:

let keychainQuery: NSMutableDictionary = NSMutableDictionary(objects: [kSecClassGenericPasswordValue, service, kCFBooleanTrue, kSecMatchLimitOneValue], forKeys: [kSecClassValue, kSecAttrServiceValue, kSecReturnDataValue, kSecMatchLimitValue])

        var dataTypeRef :AnyObject?

        // Search for the keychain items
        let status: OSStatus = SecItemCopyMatching(keychainQuery, &dataTypeRef)
        var contentsOfKeychain: String?

        if status == errSecSuccess {
            if let retrievedData = dataTypeRef as? Data {
                contentsOfKeychain = String(data: retrievedData, encoding: String.Encoding.utf8)
            }
        } else {
            print("Nothing was retrieved from the keychain. Status code \(status)")
        }

        return contentsOfKeychain
paul590
  • 1,385
  • 1
  • 22
  • 43

1 Answers1

1

Using the keychain is much simpler with KeychainAccess:

// Create keychain object
let keychain = Keychain(service: "com.company.AppName")

// Store value in keychain
keychain["password"] = "test"

// Retrieve value from keychain
let password = keychain["password"]
sundance
  • 2,930
  • 1
  • 20
  • 25