SwiftKeychainWrapper Documentation

repository·develop·Indexed 23 days ago

https://github.com/jrendel/swiftkeychainwrapper

A simple Swift wrapper for the iOS/tvOS Keychain providing an interface similar to UserDefaults. It supports the KeychainWrapper.standard singleton, custom instances for specific service names or access groups, subscripting for key access, and configuration for iCloud synchronization and accessibility options.

Tokens
1K
Snippets
5
Records
6
Agent score
32%

What's inside SwiftKeychainWrapper

  1. Use the standard KeychainWrapper singleton

    develop

    For most use cases, use the KeychainWrapper.standard singleton. It uses your app's main bundle identifier as the service name and saves data as a Generic Password type that is only accessible when the app is unlocked and open.

    To use it, first import SwiftKeychainWrapper.

  2. Access Keychain using subscripting

    develop

    You can access the keychain using subscript syntax, similar to a dictionary. This is often used by extending KeychainWrapper.Key to define predefined keys.

    // Define keys for convenience
    extension KeychainWrapper.Key {
        static let myKey: KeychainWrapper.Key = "myKey"
    }
    
    // Use subscripting
    KeychainWrapper.standard[.myKey] = "some string"
    
    let myValue: String? = KeychainWrapper.standard[.myKey]
    
    KeychainWrapper.standard.remove(forKey: .myKey)
  3. Create a custom KeychainWrapper instance

    develop

    If you need to use a custom service name or an access group (to share keychain items between applications), create a custom instance of KeychainWrapper instead of using the singleton.

    let uniqueServiceName = "customServiceName"
    let uniqueAccessGroup = "sharedAccessGroupName"
    let customKeychainWrapperInstance = KeychainWrapper(serviceName: uniqueServiceName, accessGroup: uniqueAccessGroup)
    
    // Use the custom instance
    let saveSuccessful: Bool = customKeychainWrapperInstance.set("Some String", forKey: "myKey")
    let retrievedString: String? = customKeychainWrapperInstance.string(forKey: "myKey")
    let removeSuccessful: Bool = customKeychainWrapperInstance.removeObject(forKey: "myKey")
  4. Configure Synchronizable option for iCloud

    develop

    To sync keychain items with iCloud so they are available across all of a user's devices, set the isSynchronizable parameter to true when saving a value.

    Important: You cannot modify a value for a key if it was previously set with a different accessibility option. You must first remove the existing value before setting it again with the new option.

    // Enable iCloud synchronization
    KeychainWrapper.standard.set("Some String", forKey: "myKey", isSynchronizable: true)
    
    // How to change accessibility for an existing key:
    KeychainWrapper.standard.set("String one", forKey: "myKey", withAccessibility: .AfterFirstUnlock)
    KeychainWrapper.standard.removeObject(forKey: "myKey")
    KeychainWrapper.standard.set("String two", forKey: "myKey", withAccessibility: .Always)
  5. Configure Keychain accessibility options

    develop

    By default, items are only accessible when the device is unlocked. You can change this behavior for specific requests using the withAccessibility parameter and the KeychainItemAccessibilty enum.

    KeychainWrapper.standard.set("Some String", forKey: "myKey", withAccessibility: .AfterFirstUnlock)