首页
/ UICKeyChainStore 技术文档

UICKeyChainStore 技术文档

2024-12-24 14:26:30作者:柏廷章Berta

本文档将详细介绍如何安装、使用以及API调用UICKeyChainStore项目,帮助用户更好地理解和应用该项目。

1. 安装指南

UICKeyChainStore 支持多种安装方式,以下是常用的安装方法:

使用 CocoaPods 安装

在 Podfile 中添加以下代码:

pod 'UICKeyChainStore

然后执行 pod install 命令。

使用 Carthage 安装

在 Cartfile 中添加以下代码:

github "kishikawakatsumi/UICKeyChainStore"

然后执行 carthage update 命令。

手动安装

从 GitHub 下载 UICKeyChainStore 的源代码,将其添加到你的项目,并确保链接正确的库。

2. 项目使用说明

UICKeyChainStore 是一个简单的 Keychain 包装器,使得使用 Keychain API 和使用 NSUserDefaults 一样简单。

基础使用

保存应用密码

UICKeyChainStore *keychain = [UICKeyChainStore keyChainStoreWithService:@"com.example.github-token"];
keychain[@"kishikawakatsumi"] = @"01234567-89ab-cdef-0123-456789abcdef";

保存互联网密码

UICKeyChainStore *keychain = [UICKeyChainStore keyChainStoreWithServer:[NSURL URLWithString:@"https://github.com"]
                                                          protocolType:UICKeyChainStoreProtocolTypeHTTPS];
keychain[@"kishikawakatsumi"] = @"01234567-89ab-cdef-0123-456789abcdef";

3. 项目API使用文档

以下是 UICKeyChainStore 的一些主要 API 使用方法。

实例化

创建应用密码的 Keychain

UICKeyChainStore *keychain = [UICKeyChainStore keyChainStoreWithService:@"com.example.github-token"];

创建互联网密码的 Keychain

UICKeyChainStore *keychain = [UICKeyChainStore keyChainStoreWithServer:[NSURL URLWithString:@"https://github.com"]
                                                          protocolType:UICKeyChainStoreProtocolTypeHTTPS];

添加项目

下标操作

keychain[@"kishikawakatsumi"] = @"01234567-89ab-cdef-0123-456789abcdef";

set 方法

[keychain setString:@"01234567-89ab-cdef-0123-456789abcdef" forKey:@"kishikawakatsumi"];

错误处理

if (![keychain setString:@"01234567-89ab-cdef-0123-456789abcdef" forKey:@"kishikawakatsumi"]) {
    // 发生错误
}

获取项目

下标操作(自动转换为字符串)

NSString *token = keychain[@"kishikawakatsumi"];

获取方法

作为字符串
NSString *token = [keychain stringForKey:@"kishikawakatsumi"];
作为数据
NSData *data = [keychain dataForKey:@"kishikawakatsumi"];

错误处理

首先,获取 failable(值或错误)对象

NSError *error;
NSString *token = [keychain stringForKey:@"" error:&error];
if (error) {
    NSLog(@"%@", error.localizedDescription);
}

删除项目

下标操作

keychain[@"kishikawakatsumi"] = nil;

删除方法

[keychain removeItemForKey:@"kishikawakatsumi"];

错误处理

if (![keychain removeItemForKey:@"kishikawakatsumi"]) {
    // 发生错误
}

配置(可访问性、共享、iCloud 同步)

可访问性

默认的可访问性匹配后台应用程序(= kSecAttrAccessibleAfterFirstUnlock)

UICKeyChainStore *keychain = [UICKeyChainStore keyChainStoreWithService:@"com.example.github-token"];

共享 Keychain 项目

UICKeyChainStore *keychain = [UICKeyChainStore keyChainStoreWithService:@"kishikawakatsumi.git"
                                                            accessGroup:@"12ABCD3E4F.shared"];

与 iCloud 同步 Keychain 项目

创建实例

UICKeyChainStore *keychain = [UICKeyChainStore keyChainStoreWithService:@"com.example.github-token"];
keychain.synchronizable = YES;

keychain[@"kishikawakatsumi"] = @"01234567-89ab-cdef-0123-456789abcdef"

Touch ID 集成

任何需要认证的操作必须在后台线程中运行。
如果在主线程中运行,UI 线程将锁定,系统尝试显示认证对话框。

添加 Touch ID 保护的项

如果你想存储 Touch ID 保护的 Keychain 项,请指定 accessibilityauthenticationPolicy 属性。

UICKeyChainStore *keychain = [UICKeyChainStore keyChainStoreWithService:@"com.example.github-token"];

dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_BACKGROUND, 0), ^{
    [keychain setAccessibility:UICKeyChainStoreAccessibilityWhenPasscodeSetThisDeviceOnly
          authenticationPolicy:UICKeyChainStoreAuthenticationPolicyUserPresence];

    keychain[@"kishikawakatsumi"] = @"01234567-89ab-cdef-0123-456789abcdef"
});

更新 Touch ID 保护的项

更新方法与添加时相同。
如果有可能该项已存在且受保护,请不要在主线程中运行,因为更新受保护的项需要认证。

获取 Touch ID 保护的项

获取方法与获取普通项相同。如果尝试获取的项受保护,将自动显示 Touch ID 或密码认证。
如果需要显示自定义认证提示信息,请指定 authenticationPrompt 属性。

删除 Touch ID 保护的项

删除方法与删除普通项相同。没有显示 Touch ID 或密码认证的方式来删除 Keychain。

4. 项目安装方式

请参考上述“安装指南”部分,选择适合您项目的安装方法。

登录后查看全文
热门项目推荐
相关项目推荐

项目优选

收起
docsdocs
暂无描述
Dockerfile
703
4.51 K
pytorchpytorch
Ascend Extension for PyTorch
Python
567
694
atomcodeatomcode
Claude Code 的开源替代方案。连接任意大模型,编辑代码,运行命令,自动验证 — 全自动执行。用 Rust 构建,极致性能。 | An open-source alternative to Claude Code. Connect any LLM, edit code, run commands, and verify changes — autonomously. Built in Rust for speed. Get Started
Rust
554
98
ops-mathops-math
本项目是CANN提供的数学类基础计算算子库,实现网络在NPU上加速计算。
C++
957
955
kernelkernel
openEuler内核是openEuler操作系统的核心,既是系统性能与稳定性的基石,也是连接处理器、设备与服务的桥梁。
C
412
338
RuoYi-Vue3RuoYi-Vue3
🎉 (RuoYi)官方仓库 基于SpringBoot,Spring Security,JWT,Vue3 & Vite、Element Plus 的前后端分离权限管理系统
Vue
1.6 K
940
openHiTLSopenHiTLS
旨在打造算法先进、性能卓越、高效敏捷、安全可靠的密码套件,通过轻量级、可剪裁的软件技术架构满足各行业不同场景的多样化要求,让密码技术应用更简单,同时探索后量子等先进算法创新实践,构建密码前沿技术底座!
C
1.08 K
566
AscendNPU-IRAscendNPU-IR
AscendNPU-IR是基于MLIR(Multi-Level Intermediate Representation)构建的,面向昇腾亲和算子编译时使用的中间表示,提供昇腾完备表达能力,通过编译优化提升昇腾AI处理器计算效率,支持通过生态框架使能昇腾AI处理器与深度调优
C++
128
210
flutter_flutterflutter_flutter
暂无简介
Dart
948
235
Oohos_react_native
React Native鸿蒙化仓库
C++
340
387