Keychain
public class Keychain
Keychain access class used for storing, retrieving and deleting values from the Apple keychain.
-
Specifies a prefix to use for keychain storage keys.
Defaults to the application bundle id followed by an underscore
Declaration
Swift
public static var keychainKeyPrefix: String = "\(DefaultUtility.default.bundleIdentifier)_"
-
Optional keychain access group. If provided this group will be used for all keychain access.
Declaration
Swift
public static var keychainAccessGroup: String?
-
Writes data to the keychain as defined by the provided KeychainCommand object.
example:
let writeCmd = WriteKeychainCommand.toWriteValue("some value", withKey: "myKey").withAccess(KeychainCommand.KeychainAccess.whenPasscodeSetThisDevice) let writeResult = Keychain.writeToKeychain(writeCmd) if writeResult.success { print("Write successful!") }
Declaration
Swift
public static func writeToKeyChain(_ keychainCommand: KeychainCommand) -> (KeychainCommandResult)
Parameters
keychainCommand
WriteKeychainCommand object
Return Value
KeychainCommandResult object describing the success or failure of the write operation
-
Reads data from the keychain as defined by the provided KeychainCommand object.
example:
let readCmd = ReadKeychainCommand.toReadWithKey("myKey").withBiometricAuthenticationPrompt("Authentication Required") // The user will see an "Authentication Required" prompt let readResult = Keychain.readFromKeyChain(readCmd) if readCmd.success { let valueString = Keychain.dataToString(readCmd.value) print("Read successful: \(valueString)") }
Declaration
Swift
public static func readFromKeyChain(_ keychainCommand: KeychainCommand) -> (ReadKeychainCommandResult)
Parameters
keychainCommand
ReadKeychainCommand object
Return Value
ReadKeychainCommandResult object describing the success or failure of the read operation, and containing the obtained value if the read was successful.
-
Deletes data from the keychain as defined by the provided KeychainCommand object.
Declaration
Swift
public static func deleteFromKeyChain(_ keychainCommand: KeychainCommand) -> (KeychainCommandResult)
Parameters
keychainCommand
KeychainCommand object
Return Value
KeychainCommandResult object describing the success or failure of the delete operation.
-
Deletes all data from the keychain for the running app.
Declaration
Swift
public static func clearKeychain() -> (KeychainCommandResult)
Return Value
KeychainCommandResult object describing the success or failure of the delete operation.
-
Given a Data object that is a utf8 encoded string, returns a String
Declaration
Swift
public static func dataToString(_ value: Data?) -> (String?)
-
Given a string object returns a Data object that contains the string encoded with utf8 encoding.
Declaration
Swift
public static func stringToData(_ string: String) -> (Data)
-
Given a Data object that is a boolean value, returns a Bool
Declaration
Swift
public static func dataToBool(_ value: Data?) -> (Bool)
-
Given a boolean value returns a Data object that contains the value.
Declaration
Swift
public static func boolToData(_ bool: Bool) -> (Data)