CRDKeychain
为 iOS、macOS、watchOS 和 tvOS 提供简单直接的 Swift 代码库键链访问框架
概览
我最近创建了一种从 Swift 基于的应用程序中访问 Apple 键链的方法。虽然目前已有几个功能齐全的库可实现此目的,但我发现它们相当复杂,涉及到大量代码。我需要一种小而紧凑的库,只需添加几个文件即可轻松添加到任何项目中。因此,我决定创建自己的 cocoa 框架和 cocoapod,以便在不同的 iOS、macOS、watchOS 和 tvOS Swift 基于项目中都能保持一致的用户界面。
要求
- iOS 9.0+ / macOS 10.12+ / watchOS 3.0+ / tvOS 9.0+
- Xcode 10.2+
- Swift 5.0+
安装
您可以将此代码库用于您的项目,只需将这些文件从共享文件夹添加到您的Swift项目中即可。
-
CRDKeychain.swift
- 此文件定义了CRDKeychain对象以及从密钥库中获取、设置和删除条目的方法。
-
CRDKeychainEntry.swift
- 此文件定义了表示密钥库条目及其各种属性的CRDKeychainEntry对象。CRDKeychain中的方法使用此对象作为输入或输出。
-
CRDKeychainError.swift
- 此文件定义了CRDKeychain的
init()
和其他方法可能抛出的错误。
- 此文件定义了CRDKeychain的
-
CRDKeychainEntryError.swift
- 此文件定义了CRDKeychainEntry的
init()
可能抛出的错误。
- 此文件定义了CRDKeychainEntry的
CocoaPods
或者,您还可以将其作为Cocoapod进行安装
CocoaPods是Cocoa项目的依赖关系管理器。您可以使用以下命令安装它
$ gem install cocoapods
构建CRDKeychain需要CocoaPods 1.7.0+。
要使用CocoaPods将CRDKeychain集成到您的Xcode项目中,请在您的Podfile
中指定它
source 'https://github.com/CocoaPods/Specs.git'
platform :ios, '10.0'
use_frameworks!
target '<Your Target Name>' do
pod 'CRDKeychain'
end
然后,运行以下命令
$ pod install
用法
要开始使用密钥库,您可以为表示应用程序密钥库的CRDKeychain
对象创建一个新的实例
let keychain: CRDKeychain
do {
keychain = try CRDKeychain()
} catch let error as NSError {
print("\(error)")
}
默认初始化器自动以应用程序的捆绑标识符作为所有应用程序密钥库条目的服务名称(kSecAttrService
)进行设置,以羹密钥库共享访问组名称(kSecAttrAccessGroup
)为nil,以及条目的安全设置(kSecAttrAccessible
)为kSecAttrAccessibleWhenUnlockedThisDeviceOnly
。
您可以将这些值之一或多个传递给初始化器以覆盖默认值。
方法
以下方法可用于与密钥库交互
-
valueFor(key: String, includeData: Bool = false) throws -> CRDKeychainEntry?
- 获取指定键的值,并根据
includeData
标志返回数据。
- 获取指定键的值,并根据
-
public func exists(key: String) throws -> Bool
- 如果为给定键存在密钥库条目,则返回true。
-
public func set(entry: CRDKeychainEntry) throws
- 使用指定值添加或更新密钥库条目。
-
public func getAll(includeData: Bool = false) throws -> [CRDKeychainEntry]?
- 获取所有密钥库条目(如果设置了标志,则包含数据)作为数组。
-
public func remove(key: String) throws
- 删除指定的键的密钥库条目。
-
公开 func removeAll() throws
- 移除所有密钥库条目。
CRDKeychainEntry
CRDKeychain的方法接受和返回名为CRDKeychainEntry的对象,这些对象基本上只是一个表示密钥库记录属性的对象 - 比如账户名称、描述、标签,当然还有条目中的秘密数据部分。有关可用的属性和它们代表的相应密钥库属性,请参阅CRDKeychainEntry.swift
。
基本操作
添加新密钥库条目
do {
// Create a new keychain entry to add to the keychain.
let expectedEntry = CRDKeychainEntry(key: "key1")
expectedEntry.account = "account1"
expectedEntry.label = "label1"
expectedEntry.desc = "this is the description"
expectedEntry.notes = "this is the comment"
expectedEntry.secret = "this is the data".data(using: .utf8)
// Add our new entry to the keychain.
try keychain?.set(entry: expectedEntry)
} catch let error as NSError {
print("\(error)")
}
从密钥库获取现有条目
do {
// Get the entry just added, including the data.
var entryFound = try keychain?.valueFor(key: "key1", includeData: true)
} catch let error as NSError {
print("\(error)")
}
要更新条目,只需修改由valueFor
获得的条目的属性,然后调用带修改后的CRDKeychainEntry
的set
方法。
do {
// Get the entry just added, including the data.
var entryFound = try keychain?.valueFor(key: "key1", includeData: true)
// Modify the entry
entryFound.account = "account2"
entryFound.label = "label2"
entryFound.desc = "this is the modified description"
entryFound.notes = "this is the modified comment"
entryFound.secret = "this is the modified data".data(using: .utf8)
// Save the modified entry, replacing the original in the keychain.
try keychain?.set(entry: entryFound)
} catch let error as NSError {
print("\(error)")
}
要删除条目,只需调用带有您要删除的条目键的remove
方法。
do {
// Remove the entry previously modified.
try keychain?.remove(key: "key1")
} catch let error as NSError {
print("\(error)")
}
您还可以使用getAll(includeData: Bool = false)
方法检索密钥库中的所有条目(如果有的话),可选地包括每个条目的数据;使用removeAll()
方法删除所有条目;使用exists(key: String)
来测试密钥库中是否有条目。
将includeData
参数设置为可选的并默认为false,当从密钥库获取条目时,是因为检索条目的属性与数据比仅检索属性要慢一些。通常,您只想获取条目的属性,例如在显示表视图或集合视图中的某些属性时。仅在需要直接处理数据时才检索数据。
请注意,由于kSecAttrSynchronizable
在watchOS上不可用,因此在此平台上设置CRDKeychainEntry对象的synchronizable
属性将没有任何效果。
结论
希望这个小库/框架能帮助您在下一个Swift项目中。我会随着时间的推移和兴趣进行更新,当然我也欢迎您所有的反馈。
许可证
CRDKeychain是遵照Apache 2.0许可证发布的。有关详情,请参阅LICENSE。