概览
KlugKeychain 将标志、令牌或 ID 等小型数值存储在 iOS 和 macOS 的钥匙串中。与普通应用存储不同,钥匙串在应用被卸载后重新安装时仍会保留这些值。
这使它成为应用需要在重新安装后仍记住的内容的持久锚点,例如某台设备是否已使用过免费试用,或某个账户之前是否被删除过。
KlugKeychain 每天都在我们的应用中使用,这就是我们的概念验证。
特性
- 自成一体:一个 ANE,无需添加核心库或依赖 ANE
- 按键存储、读取、检查和移除值
- 值在卸载并重新安装应用后依然保留
- 由操作系统在系统钥匙串中加密
- iOS 和 macOS 使用同一套 API
- 简单的同步调用,无需处理事件
支持的平台
iOS arm64
macOS Apple 芯片 · Intel
在其他平台上,isSupported 返回 false,因此同一份代码可以保留在跨平台应用中。
在 Android 上,KlugDeviceId 提供同样不受重新安装影响的锚点:卸载并重新安装应用后,Android 设备 ID 保持不变。
入门
将扩展添加到您的应用描述文件中:
<extensions>
<extensionID>com.klug.keychain.Keychain</extensionID>
</extensions>
在 macOS 上,应用必须使用 keychain access group entitlement 签名。在 iOS 上不需要额外的 entitlement。
代码示例
三个完整流程。所有调用都是同步的并立即返回;值均为字符串。完整文档 →
1 · 检查支持情况并选择回退方案
import com.klug.keychain.Keychain; var keychain:Keychain = Keychain.getInstance(); // iOS: always true. // macOS: true only if the app is signed with a keychain access group entitlement; // the extension checks this with a test write. // Android, Windows: false, and every call is a safe no-op. if ( !keychain.isSupported ) useLocalStorage(); // your app's fallback, e.g. a SharedObject
2 · 在重新安装后仍记住免费试用
const TRIAL_DAYS:Number = 7; if ( keychain.has( "trialStarted" ) ) { var started:Number = Number( keychain.get( "trialStarted" ) ); var days:Number = ( new Date().time - started ) / 86400000; if ( days > TRIAL_DAYS ) showPurchaseScreen(); // your app } else { // stays in the keychain after the app is uninstalled and installed again keychain.set( "trialStarted", String( new Date().time ) ); }
3 · 存储、读取和移除值
// set() replaces an existing value; false = not stored if ( !keychain.set( "accountId", accountId ) ) trace( "keychain: value not stored" ); // get() returns "" when nothing is stored var id:String = keychain.get( "accountId" ); if ( id == "" ) showSignIn(); // your app // sign-out: remove what the app no longer needs; // true also when the key was already gone keychain.remove( "accountId" );
API 概览
| Keychain.getInstance() | 返回唯一实例。 |
| isSupported | 在 iOS 上为 True。在 macOS 上,当应用使用 keychain access group entitlement 签名时为 true;在其他平台为 false。 |
| set(key, value) | 存储一个字符串,并替换已有的值。成功时返回 true;若不受支持,或 key 或 value 为 null,则返回 false。 |
| get(key) | 返回已存储的值;若不存在则返回空字符串。 |
| has(key) | 如果该 key 存有值则为 True。 |
| remove(key) | 移除该值。一旦该 key 不再存在即返回 true。 |
| 存储 | 您应用的通用密码项,设备首次解锁后即可读取,在后台也可读取。 |
购买 KlugKeychain
一个 AIR 应用的年度许可,含 12 个月内的所有更新。付款后,您将通过电子邮件收到扩展、ActionScript 库和文档,通常在一个工作日内。许可与条款
税费在结账时由 Link (Stripe) 计算。您需要在结账时输入 AIR 应用的应用 ID。
要求
| AIR SDK | 51.x (HARMAN) |
| iOS | iOS 17 或更高版本 |
| macOS | macOS 11 或更高版本 |
| 签名 (macOS) | Keychain access group entitlement |