SwiftySimpleKeychain
public class SwiftySimpleKeychain
A simple helper class to deal with storing and retrieving values from Keychain.
It has support for sharing keychain items using Access Group and ffine grained accesibility
over a specific Kyechain Item (Using Access Control).
The support is only available for iOS and macOS, otherwise it will default using the coarse grained accesibility field.
When a NSString
or NSData
is stored using Access Control and the accesibility flag
SwiftySimpleKeychainItem.accessibleAfterFirstUnlock
, iOS / macOS will prompt the user for
it’s passcode or pass a TouchID challenge (if available).
-
Instantiates a SwiftySimpleKeychain
Usage:
keychain = SwiftySimpleKeychain(with: "service-name")
Declaration
Swift
public init(with service: String = "default", accessGroup: String? = nil)
Parameters
service
Service name under all items are saved. Defaults to
default
accessGroup
an Access Group for Keychain item sharing. Defaults to
nil
-
Deletes an entry in the Keychain with the given key
Declaration
Swift
func deleteEntry(for key: String) -> Bool
Parameters
key
Identifier of the entry
-
Deletes all entries in the Keychain Aborts if there is an error deleting one item.
Declaration
Swift
func clearAll()
-
Checks if the entry with key exists in the Keychain
Usage:
if keychain.hasValue(for: "aKey") { // entry exists in Keychain }
Declaration
Swift
func hasValue(for key: String) -> Bool
Parameters
key
Identifier of the entry
-
Returns a DataErrorResult for the given key
Declaration
Swift
func dataResult(for key: String, with promptMessage: String? = nil) -> DataErrorResult
Parameters
key
Identifier of the key
promptMessage
A message to show to the user in case the Keychain should be unlocked
-
Returns a StringErrorResult for the given key
Declaration
Swift
func stringResult(for key: String, with promptMessage: String? = nil) -> StringErrorResult
Parameters
key
Identifier of the key
promptMessage
A message to show to the user in case the Keychain should be unlocked
-
Returns a Data for the given key if exists
Usage:
if let data = keychain.data(for: "aKey") { // valid data retrieved from the Keychain }
Declaration
Swift
func data(for key: String, with promptMessage: String? = nil) -> Data?
Parameters
key
Identifier of the key
promptMessage
A message to show to the user in case the Keychain should be unlocked
-
Returns a String for the given key if exists
Usage:
if let text = keychain.string(for: "aKey") { // valid string retrieved from the Keychain }
Declaration
Swift
func string(for key: String, with promptMessage: String? = nil) -> String?
Parameters
key
Identifier of the key
promptMessage
A message to show to the user in case the Keychain should be unlocked
-
Sets a Data for the given key
Usage:
_ = keychain.set(data: aData, for: "aKey")
Declaration
Swift
func set(data: Data?, for key: String, with promptMessage: String? = nil) -> VoidErrorResult
Parameters
data
The Data to store. If nill, it delets the entry.
key
Identifier of the key
promptMessage
A message to show to the user in case the Keychain should be unlocked
-
Sets a String for the given key
Usage:
_ = keychain.set(string: "value", for: "aKey")
Declaration
Swift
func set(string: String? = nil, for key: String, with promptMessage: String? = nil) -> VoidErrorResult
Parameters
data
The String to store. If nill, it delets the entry.
key
Identifier of the key
promptMessage
A message to show to the user in case the Keychain should be unlocked
-
Instantiates a SwiftySimpleKeychain
Usage:
keychain = SwiftySimpleKeychain.with("service-name")
Declaration
Swift
static func with(_ service: String = "default", accessGroup: String? = nil) -> SwiftySimpleKeychain
Parameters
service
Service name under all items are saved. Defaults to
default
accessGroup
an Access Group for Keychain item sharing. Defaults to
nil