iOS 钥匙串存取数据(SAMkeychain)

作者: LYSNote | 来源:发表于2016-11-15 11:05 被阅读5994次

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

    • FMDB
    • Core Data
    • NSUserDefaults
    • SQLite
    • UIPasteboard
    • Keychain(这次主要介绍这种)

    使用钥匙串存取数据,首先必须要获取设备钥匙串
    <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

    相关文章

      网友评论

      • 叫我天涯:用企业证书签名的APP、使用 钥匙串共享,重新签名后,钥匙串共享失效,重签名的证书和描述文件以及bundleId完全一样,依然钥匙串共享失败。这是为啥?
      • 楠南nelson:没分享库地址
        CoderFM:@楠南nelson 差评😀

      本文标题:iOS 钥匙串存取数据(SAMkeychain)

      本文链接:https://www.haomeiwen.com/subject/pqikpttx.html