Install SwiftKeychainWrapper
developYou can install SwiftKeychainWrapper using CocoaPods, Carthage, Swift Package Manager, or by manual installation.
import SwiftKeychainWrapperrepository·develop·Indexed 23 days ago
https://github.com/jrendel/swiftkeychainwrapperA 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.
You can install SwiftKeychainWrapper using CocoaPods, Carthage, Swift Package Manager, or by manual installation.
import SwiftKeychainWrapperFor 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.
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)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")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)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)