Keychain Manager is a Layer-2 framework built over Keychain API which helps in using Keychain in all your Apple devices with easiness and flexibility. It focuses on using all the power of Keychain with high simplicity. The easy to use methods of Keychain Manager helps to setup Keychain on any Apple device with great convenience.
Before using any Keychain Manager methods we need to intialise the class. Keychain Manager supports various types of inilisation which depends upon variety of use cases
- This initilisation stores all the Keychain items on the local device.
- Such initilisations are best used when the app is single login based.
let KCM = KeychainManager()
- This initiliser helps to add a prefix value in your account string.
- Such initilisations are best used when performing tests (Eg: test_account1_).
let KCM = KeychainManager(keyPrefix: "test")
- Keychain Manger allowes developers to share the keychain values to other apps also synchronise with iCloud.
- Such initilisations are best used when you need to share Keychain values among apps.
- Eg: A same app running on two different devices with same iCloudID & To share data between Different apps running on same or different device
let KCM = KeychainManager(accessGroup: "TeamID.KeychainGroupID", synchronizable: true)
- To use this you need to enable the Keychain sharing in capabilities. (How to add Keychain Sharing Capability?)
- Here TeamID is which you get from your developer profile from Developer Account.
- KeychainGroupID is the string which you add in the Keychain Sharing Capability.
- When you need to add both prefix and sharable propert on keychain then this initialisation is the best one to use
let KCM = KeychainManager(keyPrefix: "test", accessGroup: "TeamID.KeychainGroupID", synchronizable: true)
Following are the methods which help to perfrom various operations:
- Used to save data on keychain.
- Keychain Manager Supports variety of data storage
KCM.set(value: "value", service: service_ID, account: account_name)
KCM.set(value: true, service: service_ID, account: account_name)
KCM.set(object: Any_Codable_Object, service: service_ID, account: account_name)
KCM.set(server: server_ID, account: account_name, password: password)
Tip: Make sure Account, Service & Server parameter must be unique for every item.
- Used to get Keychain Items.
- Keychain Manager helps to GET variety of format of Data from Keychain Storage
let value = KCM.get(service: service_ID, account: account_name)
let value = KCM.getBool(service: service_ID, account: account_name)
let value = KCM.get(object: Any_Codable_Object, service: service_ID, account: account_name)
let value = KCM.get(server: server_ID, account: account_name)
- Generic Password
let value = KCM.getAllValues(secClass: .genericPassword)
- Web Credentials
let value = KCM.getAllValues(secClass: .webCredentials)
- Used to update Kechain Item Values
- Since we have variety of SET and GET methods, similarly to update them we have variety of UPDATE methods
KCM.update(value: "value", service: service_ID, account: account_name)
KCM.update(value: true, service: service_ID, account: account_name)
KCM.update(object: Any_Codable_Object, service: service_ID, account: account_name)
KCM.update(server: server_ID, account: account_name, password: password)
- Used to delete Keychain Items
do {
try KCMTest.delete(service: service_ID, isCustomObjectType: false)
}
catch {
print(error.localizedDescription)
}
- isCustomObjectType is used to explicitly tell Keychain Manager to delete a custom Object type.
- By default the value of isCustomObjectType is
false
do {
try KCMTest.delete(server: server_ID)
}
catch {
print(error.localizedDescription)
}
- Is used to check if a certain Server or Service based keychain is valid/present.
if KCM.isValidService(service: service_ID, account: account_name) {
print("๐")
} else {
print("โน๏ธ")
}
if KCM.isValidService(server: server_ID, account: account_name) {
print("๐")
} else {
print("โน๏ธ")
}
- iCloud synchronisation needs to be set during initilisation.
- Make sure to use the sharable initialisation at every method to save all changes on cloud.
No | Device | Version |
---|---|---|
1 | iOS | 13.0.0 + |
2 | iPadOS | 13.0.0 + |
3 | WatchOS | 6.0.0 + |
4 | MacOS | 11.0.0 + |
5 | tvOS | 11.0.0 + |
Make sure you know these keynotes before using Keychain Manager
- GET Bool will return false even after deleting the Keychain item.
- To delete a custom object make sure you explicitly tell Keychain Manager that its a custom object in the delete method.
- By enabling iCloud sync you also enable Keychain Access Group, thus adding Keychain Sharing capability is important (How to do it?).
- Every Keychain item stored through MacOS is also saved in form of iOS, making it easy for developers to share same keychain data among all platforms. Thus keychain manager will give access to your MacOS based keychain items on other platforms too (Read this).
- Keychain Items stored on tvOS will not sync with other platforms (Read this).
Keychain Manger is available through Swift Package Manager. To add Keychain Manager through SPM
- Open project in Xcode
- Select
File > Add Packages
https://github.com/gokulnair2001/KeychainManager
- Open your project target
- Select
Signing & Capabilities
option - Click plus button and search for
Keychain Sharing
- Make sure to use the same Keychain Item in other apps you add the Keychain access group ID of the initial project.
- Use the framework through SPM
- If you face issues in any step open a new issue.
- To fix issues: Fork this repository, make your changes and make a Pull Request.
- Keychain Manager is available under GNU General Public License.
- If you liked
Keychain Manager
do consider buying me a coffee ๐
Made with โค๏ธ in ๐ฎ๐ณ By Gokul Nair