KeychainAccess
repository·master·Indexed 27 days ago
https://github.com/kishikawakatsumi/keychainaccessA Swift wrapper that simplifies interacting with the system Keychain on Apple platforms, including iOS, macOS, watchOS, tvOS, and Mac Catalyst. It provides a Swifty interface for managing secure credentials, supporting Access Groups, iCloud sharing, Touch ID/Face ID integration, and Shared Web Credentials.
What's inside KeychainAccess
- KeychainAccess is a Swift wrapper for the Keychain API designed for iOS, macOS, watchOS, tvOS, and Mac Catalyst. It provides a simplified, Swifty interface for managing secure credentials and data within the system keychain.
Key Features of KeychainAccess
masterKeychainAccess provides several high-level features for secure data management:
- Simple interface for Keychain operations.
- Support for Access Groups.
- Support for Accessibility settings.
- Support for iCloud sharing.
- Integration with Touch ID and Face ID (iOS 8+).
- Support for Shared Web Credentials (iOS 8+).
- Compatibility with Swift 3, 4, and 5.
Manually add KeychainAccess to your project
masterIf you prefer manual installation, follow these steps:
- Add
Lib/KeychainAccess.xcodeprojto your project. - Link
KeychainAccess.frameworkwith your target. - Add a
Copy Files Build Phaseto include the framework in your application bundle.
- Add
Integrate Touch ID / Face ID authentication
masterTo use biometric authentication, you must run operations on a background thread to avoid locking the UI thread when the system authentication dialog appears.
Requirements:
- Add
NSFaceIDUsageDescriptionto yourInfo.plistfor Face ID support. - Use
.accessibility(.whenPasscodeSetThisDeviceOnly, authenticationPolicy: [.biometryAny])to protect items. - Use
.authenticationPrompt("message")to provide a custom prompt.
Warning: Updating or obtaining protected items requires authentication and must be handled asynchronously.
- Add
Install KeychainAccess via Swift Package Manager
masterYou can install KeychainAccess using Swift Package Manager (SPM) via Xcode or the CLI.
Xcode: Select
File > Add Packages... > Add Package Dependency...and provide the repository URL.CLI: Create a
Package.swiftfile and include KeychainAccess in your dependencies.// swift-tools-version:5.0 import PackageDescription let package = Package( name: "MyLibrary", products: [ .library(name: "MyLibrary", targets: ["MyLibrary"]), ], dependencies: [ .package(url: "https://github.com/kishikawakatsumi/KeychainAccess.git", from: "3.0.0"), ], targets: [ .target(name: "MyLibrary", dependencies: ["KeychainAccess"]), ] )Install KeychainAccess via CocoaPods
masterTo install KeychainAccess using CocoaPods, add the following lines to your
Podfile:use_frameworks! pod 'KeychainAccess'Install KeychainAccess via Carthage
masterTo install KeychainAccess using Carthage, add the following line to your
Cartfile:github "kishikawakatsumi/KeychainAccess"Instantiate Keychain for Application or Internet Passwords
masterYou can create a
Keychaininstance for different types of credentials. Useservicefor application passwords andserver/protocolTypefor internet passwords.Application Passwords
Use a service identifier (e.g., a bundle ID).
Internet Passwords
Use a server URL and a
protocolType(e.g.,.https). You can also specify anauthenticationType(e.g.,.htmlForm).// Application Password let keychain = Keychain(service: "com.example.github-token") let keychain = Keychain(service: "com.example.github-token", accessGroup: "12ABCD3E4F.shared") // Internet Password let keychain = Keychain(server: "https://github.com", protocolType: .https) let keychain = Keychain(server: "https://github.com", protocolType: .https, authenticationType: .htmlForm)Use Shared Web Credentials
masterShared web credentials allow native iOS apps to share credentials with their website counterparts (e.g., Safari).
Setup Requirements:
- Add
com.apple.developer.associated-domainsentitlement to your app with your domains. - Add an
apple-app-site-associationfile to your website.
Usage:
Use
getSharedPasswordto check for credentials in the Shared Web Credentials store andsetSharedPasswordto save them there.- Add
Resolve Keychain sharing error [-34018]
masterIf you encounter the errorOSStatus error:[-34018] Internal error when a required entitlement isn't present, client has neither application-identifier nor keychain-access-groups entitlements., you must add aKeychain.entitlementsfile to your project to enable keychain sharing capabilities.Add and Remove items from Keychain
masterItems can be added using subscripting (for
StringorNSData) or the.set()method. To remove an item, set its subscript value tonilor use the.remove()method.Note: The
.set()method can throw errors, so it should be wrapped in ado-catchblock.// Adding via subscripting keychain["kishikawakatsumi"] = "01234567-89ab-cdef-0123-456789abcdef" keychain[data: "secret"] = NSData(contentsOfFile: "secret.bin") // Adding via set method keychain.set("01234567-89ab-cdef-0123-456789abcdef", key: "kishikawakatsumi") // Removing keychain["kishikawakatsumi"] = nil try keychain.remove("kishikawakatsumi")Configure Keychain attributes (Label, Comment, Accessibility, Sync)
masterKeychainAccess provides a fluent interface to configure item attributes during instantiation or just before a
setoperation.Common Configuration Methods:
.label(String): Sets the item label..comment(String): Sets a comment for the item..synchronizable(Bool): Enables/disables iCloud Keychain synchronization..accessibility(KeychainAccessibility): Sets the accessibility level (e.g.,.afterFirstUnlockfor background apps,.whenUnlockedfor foreground apps)..accessGroup(String): Enables sharing items across different apps via an access group.
let keychain = Keychain(service: "com.example.github-token") .label("github.com (kishikawakatsumi)") .synchronizable(true) .accessibility(.afterFirstUnlock)