iOS开发专题汇集iOS 开发成长中心iOS学习

iOS 钥匙串存取数据(SAMkeychain)

2016-11-15  本文已影响5994人  LYSNote

iOS 中常见的数据本地化有很多种,例如:

使用钥匙串存取数据,首先必须要获取设备钥匙串
<br >
第三方获取设备钥匙串

CocoPods导入第三方SAMKeychain库.png

存取数据

+ (BOOL)setPasswordData:(NSData *)password forService:(NSString *)serviceName account:(NSString *)account;
+ (BOOL)setPasswordData:(NSData *)password forService:(NSString *)serviceName account:(NSString *)account error:(NSError **)error __attribute__((swift_error(none)));
+ (BOOL)setPassword:(NSString *)password forService:(NSString *)serviceName account:(NSString *)account;
+ (BOOL)setPassword:(NSString *)password forService:(NSString *)serviceName account:(NSString *)account error:(NSError **)error __attribute__((swift_error(none)));

获取数据

+ (NSData *)passwordDataForService:(NSString *)serviceName account:(NSString *)account;
+ (NSData *)passwordDataForService:(NSString *)serviceName account:(NSString *)account error:(NSError **)error __attribute__((swift_error(none)));
+ (NSString *)passwordForService:(NSString *)serviceName account:(NSString *)account;
+ (NSString *)passwordForService:(NSString *)serviceName account:(NSString *)account error:(NSError **)error __attribute__((swift_error(none)));

删除数据

+ (BOOL)deletePasswordForService:(NSString *)serviceName account:(NSString *)account;
+ (BOOL)deletePasswordForService:(NSString *)serviceName account:(NSString *)account error:(NSError **)error __attribute__((swift_error(none)));

获取所有帐号信息

+ (NSArray<NSDictionary<NSString *, id> *> *)allAccounts;
+ (NSArray<NSDictionary<NSString *, id> *> *)allAccounts:(NSError *__autoreleasing *)error __attribute__((swift_error(none)));

+ (NSArray<NSDictionary<NSString *, id> *> *)accountsForService:(NSString *)serviceName;
+ (NSArray<NSDictionary<NSString *, id> *> *)accountsForService:(NSString *)serviceName error:(NSError *__autoreleasing *)error __attribute__((swift_error(none)));

可能感兴趣的人会发现这个第三方还有一个类SAMKeychainQuery

@property (nonatomic, copy) NSString *account;
@property (nonatomic, copy) NSString *service;
@property (nonatomic, copy) NSData *passwordData;
@property (nonatomic, copy) id<NSCoding> passwordObject;
@property (nonatomic, copy) NSString *password;

以上属性是用来配置筛选的信息,通过下面方法

- (NSArray<NSDictionary<NSString *, id> *> *)fetchAll:(NSError **)error;

筛选出和配置信息相符合的数据,返回的是一个

返回结果格式.png


钥匙串的访问,常配合唯一标识的获取与存取,可查看
http://www.jianshu.com/p/b4531e3d8717
上一篇下一篇

猜你喜欢

热点阅读