UnbluKeychainPreferencesStorage

@available(*, deprecated, message: "UnbluKeychainPreferencesStorage is deprecated. Provide your own secure `UnbluPreferencesStorage` implementation instead. When this class is removed, securePreferencesStorage will fall back to the non-secure preferencesStorage.")
class UnbluKeychainPreferencesStorage

Helper class that stores Unblu preferences in the iOS Keychain.

NOTE: This helper is deprecated. The SDK should no longer access the Keychain directly. Apps must provide their own implementation of UnbluPreferencesStorage.

Below is the shortest correct minimal example for simple string storage.

final class KeychainPreferencesStorage: UnbluPreferencesStorage {

    private let service = "com.example.myapp.unbluPrefs"

    func put(key: String, value: String?) {
        delete(key: key)
        guard let value, let data = value.data(using: .utf8) else { return }

        let query: [String: Any] = [
            kSecClass as String: kSecClassGenericPassword,
            kSecAttrService as String: service,
            kSecAttrAccount as String: key,
            kSecValueData as String: data,
            kSecAttrAccessible as String: kSecAttrAccessibleAfterFirstUnlock
        ]

        SecItemAdd(query as CFDictionary, nil)
    }

    func get(key: String) -> String? {
        var query: [String: Any] = [
            kSecClass as String: kSecClassGenericPassword,
            kSecAttrService as String: service,
            kSecAttrAccount as String: key,
            kSecReturnData as String: true,
            kSecMatchLimit as String: kSecMatchLimitOne
        ]

        var result: AnyObject?
        let status = SecItemCopyMatching(query as CFDictionary, &result)
        guard status == errSecSuccess, let data = result as? Data else { return nil }

        return String(data: data, encoding: .utf8)
    }

    private func delete(key: String) {
        let query: [String: Any] = [
            kSecClass as String: kSecClassGenericPassword,
            kSecAttrService as String: service,
            kSecAttrAccount as String: key
        ]
        SecItemDelete(query as CFDictionary)
    }
}
  • Initializes the class with the given UnbluKeychainAccessControl and an optional service to associate items in the Keychain with.

    Declaration

    Swift

    @available(*, deprecated, message: "UnbluKeychainPreferencesStorage is deprecated. Provide your own secure `UnbluPreferencesStorage` implementation instead. When this class is removed, securePreferencesStorage will fall back to the non-secure preferencesStorage.")
    init(accessControl: UnbluKeychainAccessControl, shared: Bool? = false)

    Parameters

    accessControl

    The access control level that Unblu should store items into the Keychain using.

    shared

    we use shared container or local container

  • Stores the given value into the Keychain with the given key

    Declaration

    Swift

    @available(*, deprecated, message: "UnbluKeychainPreferencesStorage is deprecated. Provide your own secure `UnbluPreferencesStorage` implementation instead. When this class is removed, securePreferencesStorage will fall back to the non-secure preferencesStorage.")
    func put(key: String, value: String?)

    Parameters

    key

    The key to store the value in the Keychain

    value

    The value for the given key

  • Returns the value for the given key from the Keychain

    Declaration

    Swift

    @available(*, deprecated, message: "UnbluKeychainPreferencesStorage is deprecated. Provide your own secure `UnbluPreferencesStorage` implementation instead. When this class is removed, securePreferencesStorage will fall back to the non-secure preferencesStorage.")
    func get(key: String) -> String?

    Parameters

    key

    The key to retrieve from the Keychain

    Return Value

    The value for the given key stored in the Keychain. nil if there is no value found.