本工程是从 SSKeychain https://github.com/soffes/sskeychain/ 衍生而来。
本项目有以下四个改进
它允许用户指定要使用哪个密钥链,例如系统密钥链或在外部驱动器/SD 卡上的密钥链。
另一个新增功能是可以指定一个被授予访问密钥链项的信任应用数组。
这还允许您更改密钥链的密码。
另一个小的改进是它实际上使用 SecItemUpdate() 更新密钥链项。SSKeychain 实际上是删除并重新添加密钥链项,这可能导致未签名代码的程序在适当地赋予权限时出现异常行为。
AHKeychain *keychain = [AHKeychain loginKeychain];
要写入此密钥链,您必须以 root 权限运行您的应用程序
AHKeychain *keychain = [AHKeychain systemKeychain];
AHKeychain *keychain = [AHKeychain keychainAtPath:@"/Volumes/MyExternalHD/Library/Keychains/myextkc.keychain"];
AHKeychain *keychain = [AHKeychain alloc]initCreatingNewKeychain:@"Test Keychain"
password:@"realfakepsswd"];
*在登录密钥链或系统密钥链上调用此方法将失败
[keychain deleteKeychain];
AHKeychainItem *item = [AHkeychainItem alloc] init];
item.service = @"com.eeaapps.test";
item.account = @"myusername";
item.label = @"AHKeychain Test Keychain Item";
item.password = @"mysecretpass";
// also if you want to allow other app to access the keychain item
NSArray *trustedApps = [NSArray arrayWithObjects:@"/Applications/Mail.app",
@"/Applications/Preview.app",
nil];
item.trustedApplications = trustedApps;
[keychain saveItem:item error:&error];
AHKeychainItem *item = [AHkeychainItem alloc] init];
item.service = @"com.eeaapps.test";
item.account = @"myusername";
[keychain getItem:item error:&error];
NSLog(@"The Password is %@",item.password);
AHKeychainItem *item = [AHkeychainItem alloc] init];
item.service = @"com.eeaapps.test";
item.account = @"myusername";
[keychain deleteItem:item error:&error];
存在两个针对标准钥匙串的常量
kAHKeychainLoginKeychain
kAHKeychainSystemKeychain
您可以使用这些中的一个,或在以下方法中指定钥匙串文件的完整路径
[AHKeychain setPassword:@"mysecretpass"
service:@"com.eeaapps.testkc"
account:@"myusername"
keychain:kAHKeychainLoginKeychain
error:&error];
NSError *error;
NSString *password = [AHKeychain getPasswordForService:item.service
account:item.account
keychain:kAHKeychainLoginKeychain
error:&error];
NSLog(@"The Password is %@",item.password);
更多信息请参阅AHKeychain.h和AHKeychainItem.h。