简体   繁体   English

将任何对象保存到Keychain

[英]Saving any object to Keychain

I want to save an object which might include sensitive data into the keychain. 我想将一个可能包含敏感数据的对象保存到钥匙串中。 To achieve this i will convert this object into a NSData instance. 为此,我将此对象转换为NSData实例。

I'm a bit confused which attributes i need to use. 我有点困惑我需要使用哪些属性。 Is it ok to use kSecClassGenericPassword as kSecClass even though it is not really a password? 是否可以使用kSecClassGenericPassword作为kSecClass,即使它不是真正的密码? Furthermore i've set the kSecAttrAccount . 此外,我设置了kSecAttrAccount I've read somewhere that i also need to add kSecAttrService . 我在某处读过我还需要添加kSecAttrService What implications can happen if i don't ? 如果我不这样做会有什么影响?

Checkout SSKeychain on GitHub. 在GitHub上查看SSKeychain It's a nice Open Source wrapper for the keychain that makes it super easy to use. 这是一个很好的开源包装钥匙链,使它非常容易使用。 If you want to understand more about how the keychain and the security on iOS/OS X work and you're registered to the Apple Developer Program, there are few WWDC session videos that you might check on developer.apple.com . 如果您想更多地了解iOS / OS X上的钥匙串和安全性如何工作并且您已注册到Apple开发人员计划,那么您可以在developer.apple.com上查看的WWDC会话视频很少。

在保存到钥匙串之前, Strongbox使用NSKeyedUnarchiver将任何类型符合NSCoding转换为NSData对象。

See this library: https://github.com/nicklockwood/FXKeychain 请参阅此库: https//github.com/nicklockwood/FXKeychain

- (BOOL)setObject:(id)object forKey:(id)key;
- (BOOL)setObject:(id)object forKeyedSubscript:(id)key;

Basic example: 基本示例:

#import "ViewController.h"
#import "FXKeychain.h"


@interface ViewController () <UITextFieldDelegate, UITextViewDelegate>

@property (nonatomic, strong) IBOutlet UITextField *keyField;
@property (nonatomic, strong) IBOutlet UITextView *dataField;

@end


@implementation ViewController

- (IBAction)save
{
    //save data
    [FXKeychain defaultKeychain][_keyField.text] = _dataField.text;
}

- (IBAction)load
{
    //load data
    _dataField.text = [FXKeychain defaultKeychain][_keyField.text];
}

- (IBAction)delete
{
    //clear field
    _dataField.text = @"";

    //delete data
    [[FXKeychain defaultKeychain] removeObjectForKey:_keyField.text];
}

- (IBAction)tap
{
    [_keyField resignFirstResponder];
    [_dataField resignFirstResponder];
}

- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
    [textField resignFirstResponder];
    return YES;
}

@end

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM