KeychainWrapper

open class KeychainWrapper

KeychainWrapper is a class to help make Keychain access in Swift more straightforward. It is designed to make accessing the Keychain services more like using NSUserDefaults, which is much more familiar to people.

  • Undocumented

    Declaration

    Swift

    open class KeychainWrapper
  • Default keychain wrapper access

    Declaration

    Swift

    public static let standard = KeychainWrapper()
  • ServiceName is used for the kSecAttrService property to uniquely identify this keychain accessor. If no service name is specified, KeychainWrapper will default to using the bundleIdentifier.

    Declaration

    Swift

    private (set) public var 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 KeychainWrapper with shared keychain access between different applications.

    Declaration

    Swift

    private (set) public var accessGroup: String?
  • Create a custom instance of KeychainWrapper with a custom Service Name and optional custom access group.

    Declaration

    Swift

    public init(serviceName: String, accessGroup: String? = nil)

    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: KeychainItemAccessibility? = nil) -> Bool

    Parameters

    forKey

    The key to check for.

    withAccessibility

    Optional accessibility to use when retrieving the keychain item.

    Return Value

    True if a value exists for the key. False otherwise.

  • Undocumented

    Declaration

    Swift

    open class KeychainWrapper
  • Undocumented

    Declaration

    Swift

    open class KeychainWrapper
  • Undocumented

    Declaration

    Swift

    open class KeychainWrapper
  • Undocumented

    Declaration

    Swift

    open class KeychainWrapper
  • Undocumented

    Declaration

    Swift

    open class KeychainWrapper
  • Returns a string value for a specified key.

    Declaration

    Swift

    open func string(forKey key: String, withAccessibility accessibility: KeychainItemAccessibility? = nil) -> String?

    Parameters

    forKey

    The key to lookup data for.

    withAccessibility

    Optional accessibility to use when retrieving the keychain item.

    Return Value

    The String associated with the key if it exists. If no data exists, or the data found cannot be encoded as a string, returns nil.

  • Returns an object that conforms to NSCoding for a specified key.

    Declaration

    Swift

    open func object(forKey key: String, withAccessibility accessibility: KeychainItemAccessibility? = nil) -> NSCoding?

    Parameters

    forKey

    The key to lookup data for.

    withAccessibility

    Optional accessibility to use when retrieving the keychain item.

    Return Value

    The decoded object associated with the key if it exists. If no data exists, or the data found cannot be decoded, returns nil.

  • Returns a Data object for a specified key.

    Declaration

    Swift

    open func data(forKey key: String, withAccessibility accessibility: KeychainItemAccessibility? = nil) -> Data?

    Parameters

    forKey

    The key to lookup data for.

    withAccessibility

    Optional accessibility to use when retrieving the keychain item.

    Return Value

    The Data object associated with the key if it exists. If no data exists, returns nil.

  • Returns a persistent data reference object for a specified key.

    Declaration

    Swift

    open func dataRef(forKey key: String, withAccessibility accessibility: KeychainItemAccessibility? = nil) -> Data?

    Parameters

    forKey

    The key to lookup data for.

    withAccessibility

    Optional accessibility to use when retrieving the keychain item.

    Return Value

    The persistent data reference object associated with the key if it exists. If no data exists, returns nil.

  • Undocumented

    Declaration

    Swift

    open class KeychainWrapper
  • Undocumented

    Declaration

    Swift

    open class KeychainWrapper
  • Undocumented

    Declaration

    Swift

    open class KeychainWrapper
  • Undocumented

    Declaration

    Swift

    open class KeychainWrapper
  • Save a String value to the keychain associated with a specified key. If a String value already exists for the given key, the string will be overwritten with the new value.

    Declaration

    Swift

    @discardableResult open func set(_ value: String, forKey key: String, withAccessibility accessibility: KeychainItemAccessibility? = nil) -> Bool

    Parameters

    value

    The String value to save.

    forKey

    The key to save the String under.

    withAccessibility

    Optional accessibility to use when setting the keychain item.

    Return Value

    True if the save was successful, false otherwise.

  • Save an NSCoding compliant object to the keychain associated with a specified key. If an object already exists for the given key, the object will be overwritten with the new value.

    Declaration

    Swift

    @discardableResult open func set(_ value: NSCoding, forKey key: String, withAccessibility accessibility: KeychainItemAccessibility? = nil) -> Bool

    Parameters

    value

    The NSCoding compliant object to save.

    forKey

    The key to save the object under.

    withAccessibility

    Optional accessibility to use when setting the keychain item.

    Return Value

    True if the save was successful, false otherwise.

  • Save a Data object to the keychain associated with a specified key. If data already exists for the given key, the data will be overwritten with the new value.

    Declaration

    Swift

    @discardableResult open func set(_ value: Data, forKey key: String, withAccessibility accessibility: KeychainItemAccessibility? = nil) -> Bool

    Parameters

    value

    The Data object to save.

    forKey

    The key to save the object under.

    withAccessibility

    Optional accessibility to use when setting the keychain item.

    Return Value

    True if the save was successful, false otherwise.

  • Undocumented

    Declaration

    Swift

    open class KeychainWrapper
  • Remove 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: KeychainItemAccessibility? = 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.

  • Remove all keychain data added through KeychainWrapper. This will only delete items matching the currnt ServiceName and AccessGroup if one is set.

    Declaration

    Swift

    open func removeAllKeys() -> Bool
  • Remove all keychain data, including data not added through keychain wrapper.

    Warning

    This may remove custom keychain entries you did not add via SwiftKeychainWrapper.

    Declaration

    Swift

    open class func wipeKeychain()