Keychain
open class Keychain
Keychain is a class to make keychain access in Swift easy.
-
Predefined Accessibility levels used to secure Keychain items with various security levels.
See moreDeclaration
Swift
public enum Accessibility
-
Default Keychain instance
Declaration
Swift
public static let `default`: Keychain
-
Default iCloud Keychain instance
Declaration
Swift
public static let iCloud: Keychain
-
Default serviceName for the default
Keychain
instanceDeclaration
Swift
public static let defaultServiceName: String
-
Synchronizable indicates whether the Keychain in question is synchronized to other devices through iCloud. Any operation made with a Keychain where synchronizable is true will be synced accross all iCloud devices connected to the user’s account.
Updating or deleting items in a synchronizable Keychain will affect all copies of the item, not just the one on your local device. Be sure that it makes sense to use the same password on all devices before making a password synchronizable.
Note
Items stored or retrieved using a synchronizable Keychain may not also specify a Keychain.Accessibility value that is incompatible with syncing (namely, those whose names end with
ThisDeviceOnly
.)Items stored or retrieved by a synchronizable Keychain cannot be specified by reference.
Do not use persistent references to synchronizable items. They cannot be moved between devices, and may not resolve if the item is modified on some other device.
Declaration
Swift
public let synchronizable: Bool
-
ServiceName is used for the kSecAttrService property to uniquely identify this keychain accessor. If no service name is specified, Keychain will default to using the bundleIdentifier.
Declaration
Swift
public let serviceName: String
-
AccessGroup is used for the kSecAttrAccessGroup property to identify which Keychain Access Group this entry belongs to. This allows you to use the Keychain with shared keychain access between different applications.
Declaration
Swift
public let accessGroup: String?
-
Create a new Keychain instance with a custom Service Name and optional custom access group.
Declaration
Swift
public init(serviceName: String = Keychain.defaultServiceName, accessGroup: String? = nil, synchronizable: Bool = false)
Parameters
serviceName
The ServiceName for this instance. Used to uniquely identify all keys stored using this keychain wrapper instance.
accessGroup
Optional unique AccessGroup for this instance. Use a matching AccessGroup between applications to allow shared keychain access.
-
Checks if keychain data exists for a specified key.
Declaration
Swift
open func hasValue(forKey key: String, withAccessibility accessibility: Keychain.Accessibility? = nil) -> Bool
Parameters
forKey
The key whose value should be check
withAccessibility
Optional accessibility to use when retrieving the keychain item
Return Value
True if a value exists for the key. False otherwise.
-
Find the Accessibility level of a key
Declaration
Swift
open func accessibility(ofKey key: String) -> Keychain.Accessibility?
Parameters
ofKey
The key whose Accessibility level should be checked
Return Value
Returns the Accessibility level of a key if it exists.
nil
otherwise. -
Retrieve an DataConvertible object or persistent data reference for a specified key.
Declaration
Swift
open func retrieve<ValueType>(_ type: ValueType.Type, forKey key: String, withAccessibility accessibility: Keychain.Accessibility? = nil, asReference reference: Bool = false) -> ValueType? where ValueType : DataConvertible
Parameters
type
the return type of the desired DataConvertible object
forKey
The key to lookup data for.
withAccessibility
Optional accessibility to use when retrieving the keychain item.
asReference
Optional flag for returning as a persistent data reference
Return Value
The object associated with the key if it exists, nil otherwise.
-
Store a DataConvertible object to the keychain with a specified key. Any data previously saved with this key be overwritten with the new value.
Declaration
Swift
@discardableResult open func store<ValueType>(_ value: ValueType, forKey key: String, withAccessibility accessibility: Keychain.Accessibility = Keychain.Accessibility.default) -> Bool where ValueType : DataConvertible
Parameters
value
The DataConvertible object to store
forKey
The key to store the object under
withAccessibility
Optional accessibility to use when storing the keychain item
Return Value
True if the store was successful, false otherwise.
-
Removes an object associated with a specified key. If re-using a key but with a different accessibility, first remove the previous key value using removeObjectForKey(:withAccessibility) using the same accessibilty it was saved with.
Declaration
Swift
@discardableResult open func removeObject(forKey key: String, withAccessibility accessibility: Keychain.Accessibility? = nil) -> Bool
Parameters
forKey
The key value to remove data for
withAccessibility
Optional accessibility level to use when looking up the keychain item
Return Value
True if successful, false otherwise.
-
Removes all keychain items matching the current ServiceName and AccessGroup, if set.
Declaration
Swift
open func purge() -> Bool
Return Value
True if successful, false otherwise
-
Get the keys of all keychain entries matching the current ServiceName and AccessGroup if one is set.
Declaration
Swift
open func allKeys() -> Set<String>