欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  移动技术

iOS中利用KeyChain保存用户信息的方法示例

程序员文章站 2023-12-19 14:10:16
前言 说到保存用户名和密码,以前有用过本地的数据库来保存,也接触过用userdefault来保存,后来在一个项目中发现了一个新的方法——用keychain来保存。下面话不...

前言

说到保存用户名和密码,以前有用过本地的数据库来保存,也接触过用userdefault来保存,后来在一个项目中发现了一个新的方法——用keychain来保存。下面话不多说了,直接通过示例代码来介绍吧。

方法示例

一、新建一个lykeychaintool类,导入系统security框架 ,lykeychaintool.h文件实现如下:

//
// lykeychaintool.h
// keychaintest
//
// created by liyu on 2017/6/2.
// copyright © 2017年 liyu. all rights reserved.
//

#import <foundation/foundation.h>
#import <security/security.h>

@interface lykeychaintool : nsobject

/**
 * 储存字符串到钥匙串
 * @param svalue 对应的value
 * @param skey 对应的key
 */
+ (void)savekeychainvalue:(nsstring *)svalue key:(nsstring *)skey;

/**
 * 从钥匙串获取字符串
 * @param skey 对应的key
 * @return 返回储存的value
 */
+ (nsstring *)readkeychainvalue:(nsstring *)skey;

/**
 * 从钥匙串删除字符串
 * @param skey 对应的key
 */
+ (void)deletekeychainvalue:(nsstring *)skey;

@end

二、lykeychaintool.m文件实现如下:

//
// lykeychaintool.m
// keychaintest
//
// created by liyu on 2017/6/2.
// copyright © 2017年 liyu. all rights reserved.
//

#import "lykeychaintool.h"

@implementation lykeychaintool

+ (nsmutabledictionary *)getkeychainquery:(nsstring *)service {
 return [nsmutabledictionary dictionarywithobjectsandkeys:
   (__bridge_transfer id)ksecclassgenericpassword,
   (__bridge_transfer id)ksecclass,service,
   (__bridge_transfer id)ksecattrservice,service,
   (__bridge_transfer id)ksecattraccount,
   (__bridge_transfer id)ksecattraccessibleafterfirstunlock,
   (__bridge_transfer id)ksecattraccessible,
   nil];
}

+ (void)savekeychainvalue:(nsstring *)svalue key:(nsstring *)skey {
 nsmutabledictionary * keychainquery = [self getkeychainquery:skey];
 secitemdelete((__bridge_retained cfdictionaryref)keychainquery);
 [keychainquery setobject:[nskeyedarchiver archiveddatawithrootobject:svalue] forkey:(__bridge_transfer id)ksecvaluedata];
 secitemadd((__bridge_retained cfdictionaryref)keychainquery, null);
}

+ (nsstring *)readkeychainvalue:(nsstring *)skey {
 nsstring *ret = nil;
 nsmutabledictionary *keychainquery = [self getkeychainquery:skey];
 [keychainquery setobject:(id)kcfbooleantrue forkey:(__bridge_transfer id)ksecreturndata];
 [keychainquery setobject:(__bridge_transfer id)ksecmatchlimitone forkey:(__bridge_transfer id)ksecmatchlimit];
 cfdataref keydata = null;
 if (secitemcopymatching((__bridge cfdictionaryref)keychainquery, (cftyperef *)&keydata) == noerr) {
  @try {
   ret = (nsstring *)[nskeyedunarchiver unarchiveobjectwithdata:(__bridge nsdata *)keydata];
  } @catch (nsexception *e) {
   nslog(@"unarchive of %@ failed: %@", skey, e);
  } @finally {
  }
 }
 if (keydata)
  cfrelease(keydata);
 return ret;
}

+ (void)deletekeychainvalue:(nsstring *)skey {
 nsmutabledictionary *keychainquery = [self getkeychainquery:skey];
 secitemdelete((__bridge cfdictionaryref)keychainquery);
}

@end

三、viewcontroller 调用

//
// viewcontroller.m
// keychaintest
//
// created by liyu on 2017/6/2.
// copyright © 2017年 liyu. all rights reserved.
//

#import "viewcontroller.h"
#import "lykeychaintool.h"

@interface viewcontroller ()

@property (weak, nonatomic) iboutlet uitextfield *usernametextfield;

@property (weak, nonatomic) iboutlet uitextfield *passwordtextfield;

@end

@implementation viewcontroller

- (void)viewdidload {
 [super viewdidload];

}

- (ibaction)savebtn:(id)sender {
 [lykeychaintool savekeychainvalue:self.usernametextfield.text key:@"username"];
 [lykeychaintool savekeychainvalue:self.passwordtextfield.text key:@"password"];
}

- (ibaction)readebtn:(id)sender {
 self.usernametextfield.text = [nsstring stringwithformat:@"读取到用户名:%@",[lykeychaintool readkeychainvalue:@"username"]];
 self.passwordtextfield.text = [nsstring stringwithformat:@"读取到用户密码:%@",[lykeychaintool readkeychainvalue:@"password"]];
}

- (ibaction)deletebtn:(id)sender {
 [lykeychaintool deletekeychainvalue:@"username"];
 [lykeychaintool deletekeychainvalue:@"password"];
}

- (void)didreceivememorywarning {
 [super didreceivememorywarning];

}


@end

四、效果如下图:

iOS中利用KeyChain保存用户信息的方法示例

总结

以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作能带来一定的帮助,如果有疑问大家可以留言交流,谢谢大家对的支持。

上一篇:

下一篇: