-
Notifications
You must be signed in to change notification settings - Fork 0
/
KeychainViewController.m
162 lines (137 loc) · 5.19 KB
/
KeychainViewController.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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
//
// KeychainViewController.m
// RuntimeTest
//
// Created by user on 2020/4/15.
// Copyright © 2020 zilong. All rights reserved.
//
#import "KeychainViewController.h"
#import <Security/Security.h>
#import "KeychainTool.h"
#define SCREEN_WIDTH ([UIScreen mainScreen].bounds.size.width)
#define SCREEN_HEIGHT ([UIScreen mainScreen].bounds.size.height)
@interface KeychainViewController ()
@property (nonatomic, strong) UITextField *phoneField;
@property (nonatomic, strong) UITextField *keyField;
@property (nonatomic, strong) UIButton *addBtn;
@property (nonatomic, strong) UIButton *queryBtn;
@property (nonatomic, strong) UIButton *updateBtn;
@property (nonatomic, strong) UIButton *deleteBtn;
@property (nonatomic, strong) KeychainTool *keychainTool;
@end
@implementation KeychainViewController
- (void)loadView {
[super loadView];
[self.view addSubview:self.phoneField];
[self.view addSubview:self.keyField];
[self.view addSubview:self.addBtn];
[self.view addSubview:self.queryBtn];
[self.view addSubview:self.updateBtn];
[self.view addSubview:self.deleteBtn];
}
- (void)viewDidLoad {
[super viewDidLoad];
self.view.backgroundColor = [UIColor whiteColor];
// Do any additional setup after loading the view.
}
#pragma mark - Action
- (void)addAction {
BOOL success = [[KeychainTool sharedKeychain] addKeychainWithKey:self.phoneField.text serviceKey:@"test" value:[self.keyField.text dataUsingEncoding:NSUTF8StringEncoding]];
if (success) {
// 成功
} else {
// 失败
}
}
- (void)queryAction {
KeychainToolStatus *queryStatus = [[KeychainTool sharedKeychain] queryKeychainWithKey:self.phoneField.text serviceKey:@"test"];
if (queryStatus.executeStatus == KeyChainExecuteStatusSuccess) {
// 查询成功
NSLog(@"查询成功 %@", [[NSString alloc] initWithData:queryStatus.executeData encoding:NSUTF8StringEncoding]);
} else {
// 查询失败
NSLog(@"查询失败");
}
}
- (void)updateAction {
BOOL success = [[KeychainTool sharedKeychain] updateKeychainWithKey:self.phoneField.text serviceKey:@"test" changeValue:[self.keyField.text dataUsingEncoding:NSUTF8StringEncoding]];
if (success) {
// 成功
} else {
// 失败
}
}
- (void)deleteAction {
BOOL success = [[KeychainTool sharedKeychain] deleteKeychainWithKey:self.phoneField.text serviceKey:@"test"];
if (success) {
// 成功
} else {
// 失败
}
}
#pragma mark - Get
- (UITextField *)phoneField {
if (!_phoneField) {
_phoneField = [[UITextField alloc] initWithFrame:CGRectMake(30, 40, SCREEN_WIDTH - 60, 40)];
_phoneField.backgroundColor = [UIColor grayColor];
_phoneField.textColor = [UIColor blackColor];
_phoneField.placeholder = @"号码";
}
return _phoneField;
}
- (UITextField *)keyField {
if (!_keyField) {
_keyField = [[UITextField alloc] initWithFrame:CGRectMake(30, 100, SCREEN_WIDTH - 60, 40)];
_keyField.backgroundColor = [UIColor grayColor];
_keyField.textColor = [UIColor blackColor];
_keyField.placeholder = @"密码";
}
return _keyField;
}
- (UIButton *)addBtn {
if (!_addBtn) {
_addBtn = [[UIButton alloc] initWithFrame:CGRectMake(30, 170, 60, 40)];
_addBtn.backgroundColor = [UIColor cyanColor];
[_addBtn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
[_addBtn setTitle:@"添加" forState:UIControlStateNormal];
[_addBtn addTarget:self action:@selector(addAction) forControlEvents:UIControlEventTouchUpInside];
}
return _addBtn;
}
- (UIButton *)queryBtn {
if (!_queryBtn) {
_queryBtn = [[UIButton alloc] initWithFrame:CGRectMake(120, 170, 60, 40)];
_queryBtn.backgroundColor = [UIColor cyanColor];
[_queryBtn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
[_queryBtn setTitle:@"查询" forState:UIControlStateNormal];
[_queryBtn addTarget:self action:@selector(queryAction) forControlEvents:UIControlEventTouchUpInside];
}
return _queryBtn;
}
- (UIButton *)updateBtn {
if (!_updateBtn) {
_updateBtn = [[UIButton alloc] initWithFrame:CGRectMake(30, 240, 60, 40)];
_updateBtn.backgroundColor = [UIColor cyanColor];
[_updateBtn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
[_updateBtn setTitle:@"更新" forState:UIControlStateNormal];
[_updateBtn addTarget:self action:@selector(updateAction) forControlEvents:UIControlEventTouchUpInside];
}
return _updateBtn;
}
- (UIButton *)deleteBtn {
if (!_deleteBtn) {
_deleteBtn = [[UIButton alloc] initWithFrame:CGRectMake(120, 240, 60, 40)];
_deleteBtn.backgroundColor = [UIColor cyanColor];
[_deleteBtn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
[_deleteBtn setTitle:@"删除" forState:UIControlStateNormal];
[_deleteBtn addTarget:self action:@selector(deleteAction) forControlEvents:UIControlEventTouchUpInside];
}
return _deleteBtn;
}
- (KeychainTool *)keychainTool {
if (!_keychainTool) {
_keychainTool = [[KeychainTool alloc] init];
}
return _keychainTool;
}
@end