KeychainItem

public struct KeychainItem

A struct representation of a generic password item in the keychain.

The keychain query uses generic password for the item’s class, the serviceName property for the item’s service, and the account property for the item’s account name.

  • String indicating the item’s service name.

    Declaration

    Swift

    public let serviceName: String
  • String indicating the item’s account name.

    Declaration

    Swift

    public let account: String
  • Creates a KeychainItem instance with the specified service name and account.

    Declaration

    Swift

    public init(serviceName: String, account: String)

    Parameters

    serviceName

    The item’s service name.

    account

    The item’s account name.

  • An access control object specifying that the keychain can only be accessed when the device is unlocked and constrain access to an item with either biometry or passcode.

    Declaration

    Swift

    public static var accessControlWithUserPrescence: SecAccessControl? { get }

    Return Value

    A SecAccessControl object specified with kSecAttrAccessibleWhenPasscodeSetThisDeviceOnly as its protection type and userPresence as its flag.

  • Reads the keychain item and returns the data.

    Throws

    KeychainError thrown while trying to read the keychain item value.

    Declaration

    Swift

    public func read(context: LAContext? = nil, message: String? = nil) throws -> Data?

    Parameters

    context

    The local authentication context.

    message

    Description of the operation for which the app is attempting to authenticate.

    Return Value

    The keychain item data.

  • Reads the keychain item password and returns the string.

    Throws

    KeychainError thrown while trying to read the keychain item value.

    Declaration

    Swift

    public func readPassword(context: LAContext? = nil, message: String? = nil) throws -> String?

    Parameters

    context

    The local authentication context.

    message

    Description of the operation for which the app is attempting to authenticate.

    Return Value

    The keychain item password string.

  • Reads the keychain item and returns the json serialized object.

    Throws

    KeychainError thrown while trying to read the keychain item value.

    Declaration

    Swift

    public func readJSON(context: LAContext? = nil, message: String? = nil) throws -> [String : Any]?

    Parameters

    context

    The local authentication context.

    message

    Description of the operation for which the app is attempting to authenticate.

    Return Value

    The keychain item as a json serialized object distionary.

  • Writes data in the keychain item.

    Throws

    KeychainError thrown while trying to write the keychain item value.

    Declaration

    Swift

    public func write(data: Data, with access: SecAccessControl? = nil, context: LAContext? = nil) throws

    Parameters

    data

    The data to be written in the keychain item.

    access

    The access control object.

    context

    The local authentication context.

  • Writes a password string in the keychain.

    Throws

    KeychainError thrown while trying to write the keychain item value.

    Declaration

    Swift

    public func writePassword(_ password: String, with access: SecAccessControl? = nil, context: LAContext? = nil) throws

    Parameters

    password

    The password string to be written in the keychain item.

    access

    The access control object.

    context

    The local authentication context.

  • Writes a json object in the keychain.

    Throws

    KeychainError thrown while trying to write the keychain item value.

    Declaration

    Swift

    public func write(json: [String : Any], with access: SecAccessControl? = nil, context: LAContext? = nil) throws

    Parameters

    json

    The json object dictionary to be written in the keychain item.

    access

    The access control object.

    context

    The local authentication context.

  • Deletes the keychain item value in the keychain.

    Throws

    KeychainError thrown while trying to delete the keychain item value.

    Declaration

    Swift

    public func delete() throws
  • Returns a dictionary containing the query for the keychain item.

    The query uses generic password for the item’s class, the serviceName property for the item’s service, and the account property for the item’s account name.

    Declaration

    Swift

    public func keychainQuery() -> [String : Any]

    Return Value

    A dictionary containing the query for the keychain item.