An elegant, fast, thread-safe, multipurpose key-value storage, compatible with all Apple platforms.

Overview

KeyValueStorage

Build & Test Coverage Swift Package Manager compatible CocoaPods compatible


An elegant, fast, thread-safe, multipurpose key-value storage, compatible with all Apple platforms.


Supported Platforms

iOS macOS watchOS tvOS
9.0+ 10.10+ 2.0+ 9.0+

Installation

Swift Package Manager

Swift Package Manager is a tool for automating the distribution of Swift code and is integrated into the swift compiler.

Once you have your Swift package set up, adding KeyValueStorage as a dependency is as easy as adding it to the dependencies value of your Package.swift.

dependencies: [
    .package(url: "https://github.com/narek-sv/KeyValueStorage.git", .upToNextMajor(from: "1.0.1"))
]

or

In any file you'd like to use KeyValueStorage in, don't forget to import the framework with import KeyValueStorage.

CocoaPods

CocoaPods is a dependency manager for Cocoa projects. For usage and installation instructions, visit their website. To integrate KeyValueStorage into your Xcode project using CocoaPods, specify it in your Podfile:

pod 'KeyValueStorageSwift'

Then run pod install.

In any file you'd like to use KeyValueStorage in, don't forget to import the framework with import KeyValueStorageSwift.


Usage

Main functionality

First, initialize the storage:

let storage = KeyValueStorage()

then declare the key by specifing the key name and the type of the item:

let key = KeyValueStorageKey<Int>(name: "myAge")

after which you can save:

// Saves the item and associates it with the key or overrides the value if there is already such item. 

let myAge = 21
storage.save(myAge, forKey: key)

fetch:

// Fetches and returns the item associated with the key or returns nil if there is no such item.

let fetchedAge = storage.fetch(forKey: key) 

delete:

// Deletes the item associated with the key or does nothing if there is no such item.

storage.delete(forKey: key)

set:

// Sets the item identified by the key to the provided value.

let newAge = 24
storage.set(newAge, forKey: key) // save

storage.set(nil, forKey: key) // delete

or clear the whole storage content:

storage.clear()

KeyValueStorage works with any type that conforms to Codable protocol.

Storage types

The KeyValueStorage supports 3 storage types

  • In-memory (This storage type persists the items only within an app session.)
  • User-Defaults (This storage type persists the items within the whole app lifetime.)
  • Keychain (This storage type keeps the items in secure storage and persists even app re-installations. Supports iCloud synchronization.)

You specify the storage type when declaring the key:

let key1 = KeyValueStorageKey<Int>(name: "id", storage: .inMemory)
let key2 = KeyValueStorageKey<Date>(name: "birthday", storage: .userDefaults)
let key3 = KeyValueStorageKey<String>(name: "password", storage: .keychain())

If you don't specify a storage type .userDefaults will be used.

Xcode autocompletion

To get the advantages of the Xcode autocompletion it is recommended to declare all your keys in the extension of the KeyValueStorageKey like so:

extension KeyValueStorageKey {
    static var key1: KeyValueStorageKey<Int> {
        .init(name: "id", storage: .inMemory)
    }
    
    static var key2: KeyValueStorageKey<Date> {
        .init(name: "birthday", storage: .userDefaults)
    }
    
    static var key3: KeyValueStorageKey<String> {
        .init(name: "password", storage: .keychain())
    }
}

then Xcode will suggest all the keys specified in the extension when you put a dot: Screen Shot 2022-08-20 at 18 04 02

App Groups

KeyValueStorage also supports working with shared containers, which allows you to share your items among different App Extensions or your other Apps. To do so, first, you need to configure your app by following the steps described in this article.

Then you simply have to initialize your KeyValueStorage with the init(accessGroup:teamID:) initializer by providing your newly created accessGroup identifier and your development teamID. That's it; you are ready to use App Groups.

Keychain

Use accessibility parameter to specify the security level of the keychain storage. By default the .whenUnlocked option is used. It is one of the most restrictive options and provides good data protection.

let key = KeyValueStorageKey<String>(name: "password", storage: .keychain(accessibility: .whenUnlocked))

You can use .afterFirstUnlock if you need your app to access the keychain item while in the background. Note that it is less secure than the .whenUnlocked option.

Here are all the supported accessibility types:

  • afterFirstUnlock
  • afterFirstUnlockThisDeviceOnly
  • whenPasscodeSetThisDeviceOnly
  • whenUnlocked
  • whenUnlockedThisDeviceOnly

Set synchronizable property to true to enable keychain items synchronization across user's multiple devices. The synchronization will work for users who have the Keychain enabled in the iCloud settings on their devices. Deleting a synchronizable item will remove it from all devices.

let key = KeyValueStorageKey<String>(name: "password", storage: .keychain(accessibility: .afterFirstUnlock, isSynchronizable: true))

License

See License.md for more information.

You might also like...
🛶Shallows is a generic abstraction layer over lightweight data storage and persistence.

Shallows Shallows is a generic abstraction layer over lightweight data storage and persistence. It provides a StorageKey, Value type, instances of w

StorageManager - FileManager framework that handels Store, fetch, delete and update files in local storage
StorageManager - FileManager framework that handels Store, fetch, delete and update files in local storage

StorageManager - FileManager framework that handels Store, fetch, delete and update files in local storage. Requirements iOS 8.0+ / macOS 10.10+ / tvOS

Your Data Storage Troubleshooter 🛠
Your Data Storage Troubleshooter 🛠

Your Data Storage Troubleshooter 🛠 Introduction StorageKit is a framework which reduces the complexity of managing a persistent layer. You can easily

pick the voice from the local storage.you can play and pause the voice

flutter_add_voice A new Flutter project. Getting Started This project is a starting point for a Flutter application. A few resources to get you starte

Easiest local storage library in Swift
Easiest local storage library in Swift

SundeedQLite SundeedQLite is the easiest offline database integration, built using Swift language Requirements iOS 12.0+ XCode 10.3+ Swift 5+ Installa

A stealth AirTag clone that bypasses all of Apple's tracking protection features
A stealth AirTag clone that bypasses all of Apple's tracking protection features

A modified version of OpenHaystack to showcase the possibility of building a stealth AirTag clone that bypasses all of Apple's tracking protection features.

A CLI tool for the survey of the SSH-Key strength in your GitHub organization members.

GitHub organization SSH-keys checker A CLI tool for the survey of the SSH-Key strength in your GitHub organization members. Requirements macOS 12.0+ S

📕A single value proxy for NSUserDefaults, with clean API.

OneStore A single value proxy for NSUserDefaults, with clean API. With OneStore… Create one proxy(an OneStore object) for each NSUserDefaults value. M

A fast, pure swift MongoDB driver based on Swift NIO built for Server Side Swift
A fast, pure swift MongoDB driver based on Swift NIO built for Server Side Swift

A fast, pure swift MongoDB driver based on Swift NIO built for Server Side Swift. It features a great API and a battle-tested core. Supporting both MongoDB in server and embedded environments.

Releases(v1.0.2)
Owner
null
An Objective-C wrapper for RocksDB - A Persistent Key-Value Store for Flash and RAM Storage.

ObjectiveRocks ObjectiveRocks is an Objective-C wrapper of Facebook's RocksDB - A Persistent Key-Value Store for Flash and RAM Storage. Current RocksD

Iskandar Abudiab 56 Nov 5, 2022
An efficient, small mobile key-value storage framework developed by WeChat. Works on Android, iOS, macOS, Windows, and POSIX.

中文版本请参看这里 MMKV is an efficient, small, easy-to-use mobile key-value storage framework used in the WeChat application. It's currently available on Andr

Tencent 15.4k Jan 6, 2023
Typed key-value storage solution to store Codable types in various persistence layers with few lines of code!

?? Stores A typed key-value storage solution to store Codable types in various persistence layers like User Defaults, File System, Core Data, Keychain

Omar Albeik 94 Dec 31, 2022
A protocol-centric, type and queue safe key-value workflow.

Light-weight, strict protocol-first styled PropertyKit helps you to easily and safely handle guaranteed values, keys or types on various situations of

gitmerge 12 Feb 26, 2021
CloudKit, Apple’s remote data storage service, provides a possibility to store app data using users’ iCloud accounts as a back-end storage service.

CloudKit, Apple’s remote data storage service, provides a possibility to store app data using users’ iCloud accounts as a back-end storage service. He

Yalantis 252 Nov 4, 2022
YapDB is a collection/key/value store with a plugin architecture. It's built atop sqlite, for Swift & objective-c developers.

YapDatabase is a collection/key/value store and so much more. It's built atop sqlite, for Swift & Objective-C developers, targeting macOS, iOS, tvOS &

Yap Studios 3.3k Dec 29, 2022
Key-Value store for Swift backed by LevelDB

SwiftStore Key/Value store for Swift backed by LevelDB. Usage Create instances of store import SwiftStore // Create a store. let store = SwiftStore(s

Hemanta Sapkota 119 Dec 21, 2022
Swift APIs for SQLite: Type-safe down to the schema. Very, very, fast. Dependency free.

Lighter Lighter is a set of technologies applying code generation to access SQLite3 databases from Swift, e.g. in iOS applications or on the server. L

Lighter.swift 330 Dec 26, 2022
Disk is a powerful and simple file management library built with Apple's iOS Data Storage Guidelines in mind

Disk is a powerful and simple file management library built with Apple's iOS Data Storage Guidelines in mind

Saoud Rizwan 3k Jan 3, 2023
🔥 🔥 🔥Support for ORM operation,Customize the PQL syntax for quick queries,Support dynamic query,Secure thread protection mechanism,Support native operation,Support for XML configuration operations,Support compression, backup, porting MySQL, SQL Server operation,Support transaction operations.

?? ?? ??Support for ORM operation,Customize the PQL syntax for quick queries,Support dynamic query,Secure thread protection mechanism,Support native operation,Support for XML configuration operations,Support compression, backup, porting MySQL, SQL Server operation,Support transaction operations.

null 60 Dec 12, 2022