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

时间:2022-09-19 16:52:57

前言

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

方法示例

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

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
//
// 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文件实现如下:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
//
// 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 调用

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
//
// 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保存用户信息的方法示例

总结

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

原文链接:http://www.jianshu.com/p/85e122a2bf22