Keychain (钥匙串)

一个 Swift 包,可以轻松地在你的 iOS 应用程序中实现钥匙串功能,并提供类似 UserDefaults 的实现方式。

安装

https://github.com/GroupeMINASTE/Keychain.swift.git 添加到你的 Swift 包配置中 (或使用 Xcode 菜单: File > Swift Packages > Add Package Dependency)

用法

// Import the package
import Keychain

// When your need to access the Keychain, initialize it like this:
let keychain = Keychain()

// If you want to use an access group for your Keychain, pass it as a String argument
// let keychain = Keychain(accessGroup: "TEAMID.your.app.identifier")

// To save a value for a key named "yourKey", simply use:
let saved:Bool = keychain.save(5, forKey: "yourKey")
// The returned boolean indicates if the operation was successful

// To read a value for this key
let value = keychain.value(forKey: "yourKey") as? Int ?? 0

// And finally to delete your key and it's value, use:
let deleted:Bool = keychain.remove(forKey: "yourKey")
// The returned boolean indicates if the operation was successful