《ECKeychain》类是管理苹果 Keychain 中项的一个简单抽象层。
它包含了一套用于在 Keychain 中执行最常见操作的方法(设置、获取和删除与账户和服务相关联的项)。
使用 CocoaPods 将 ECKeychain
导入您的项目的推荐方法。您只需将以下内容添加到您的 Podfile
中,然后运行 $ pod install
。
pod 'ECKeychain'
如果您无法使用 CocoaPods,您仍然可以下载 ECKeychain
并手动将源文件添加到您的项目中的 ECKeychain
文件夹中。
ECKeychain
implementations are designed as a group of class methods, so they can be called from various parts of the app without needing to create instances of the class.
NSString *password = @"This is a password!";
NSData *keychainData = [password dataUsingEncoding:NSUTF8StringEncoding];
BOOL result = [ECKeychain setData:keychainData
forAccount:@"account"
inService:@"service"
withAccessGroup:nil
synchronizable:NO];
if (result) {
NSLog(@"Password was successfully stored in Keychain.");
}
NSData *keychainData = [ECKeychain dataForAccount:@"account"
inService:@"service"
withAccessGroup:nil];
if (keychainData) {
NSString *password = [[NSString alloc] initWithData:keychainData
encoding:NSUTF8StringEncoding];
if (password) {
NSLog(@"Password retrieved from Keychain was: %@.", password);
}
}
BOOL result = [ECKeychain deleteDataForAccount:@"account"
inService:@"service"
withAccessGroup:nil];
if (result) {
NSLog(@"Password was succcessfully deleted from Keychain.");
}
《ECKeychain》根据 MIT 许可证提供。有关更多信息,请参阅 LICENSE 文件。