1、new一个model,account和service作为键值。密钥类型: 一般密码,网络密码, 证书, 密钥, 身份证书(带私钥的证书)不同类型的钥匙串项对应的属性不同
kSecAttrAccessGroup
{
acct = "syuuid_account";
class = genp;
svce = "syuuid_service";
sync = syna;
}
2、追加查询条件。指定结果数量 首条结果、返回值类型, 可以同时指定多种返回值类型,返回属性字典
{
acct = "syuuid_account";
class = genp;
"m_Limit" = "m_LimitOne";
"r_Attributes" = 1;
"r_Data" = 1;
svce = "syuuid_service";
sync = syna;
}
3、执行查询, SecItemCopyMatching
{
acct = "syuuid_account";
agrp = "9JVMQ7S4HD.com.domain.www.GitWorkFlowTest";
cdat = "2017-06-08 09:53:01 +0000";
mdat = "2017-06-08 09:53:01 +0000";
musr = <>;
pdmn = ak;
svce = "syuuid_service";
sync = 0;
tomb = 0;
"v_Data" = <32394233 30394346 2d393437 432d3437 34312d41 4530312d 42333743 45354230 46394630>;
}
4、把查询结果转为model
YYKeychainItem:{
service:syuuid_service,
service:syuuid_account,
service:29B309CF-947C-4741-AE01-B37CE5B0F9F0,
service:2017-06-08 09:53:01 +0000,
service:2017-06-08 09:53:01 +0000,
service:9JVMQ7S4HD.com.domain.www.GitWorkFlowTest,
}
5、插入新的数据, kSecValueData, 写入值类型kSecValueData, kSecValueRef, kSecValuePersistentRef
{
acct = 111;
class = genp;
svce = 111;
sync = syna;
"v_Data" = <70617373 776f7264 313131>;
}
SecItemUpdate
SecItemDelete
SecItemCopyMatching
6、project -》Capablities-》打开Keychain Sharing开关,添加xxx.entitlements文件。从BuildSettings中的Signning,Code Signing Entitlements中可以看到,该添加的文件会自动被加入代码签名。
如下代码,默认没有设置kSecAttrAccessGroup属性,自动添加以$(AppIdentifierPrefix)com.domain.www.GitWorkFlowTest作为kSecAttrAccessGroup 标识符。 所以只要添加了xxx.entitlements, Keychain Access Groups 中的group标识符,不同的bundle identifier的app用同一个开发者证书打包,都可以访问到Keychain里面的数据。
+ (void)readALLDataInKeychine {
NSError *error = nil;
// [YYKeychain setPassword:@"11111" forService:@"service1111" account:@"account1111" error:&error];
// if (error ){
// NSLog(@" %@", error);
// }
// [YYKeychain setPassword:@"22222" forService:@"service2222" account:@"service2222" error:&error];
// if (error ){
// NSLog(@" %@", error);
// }
YYKeychainItem *item = [YYKeychainItem new];
NSArray *result = [YYKeychain selectItems:item error:&error];
if (error ){
NSLog(@" %@", error);
}
[result enumerateObjectsUsingBlock:^(NSObject* obj, NSUInteger idx, BOOL * _Nonnull stop) {
NSLog(@"读取到的keychine%@", [obj modelToJSONObject]);
}];
}