1 //
2 //ViewController.m3 //KeyChain4 //
5 //Created by apple on 14-12-26.6 //Copyright (c) 2014年 ll. All rights reserved.7 //8
9 #import "ViewController.h"
10 static NSString * const KEY_IN_KEYCHAIN = @"com.wuqian.app.allinfo";//字典在keychain中的key
11 static NSString * const KEY_PASSWORD = @"com.wuqian.app.password"; //密码在字典中的key
12
13 @interfaceViewController ()14 {15 UITextField * _field; //输入密码
16 UILabel *_psw; //显示密码
17 }18
19 @end
20
21 @implementationViewController22
23 - (void)viewDidLoad {24 [super viewDidLoad];25
26 self.view.backgroundColor =[UIColor whiteColor];27
28 UILabel * labelName = [[UILabel alloc] initWithFrame:CGRectMake(0, 30, 100, 30)];29 labelName.text = @"密码是:";30
31
32 _field = [[UITextField alloc] initWithFrame:CGRectMake(100, 80, 200, 30)];33 _field.placeholder = @"请输入密码";34 _field.borderStyle =UITextBorderStyleRoundedRect;35
36 _psw = [[UILabel alloc] initWithFrame:CGRectMake(100, 30, 200, 30)];37 _psw.backgroundColor =[UIColor yellowColor];38
39 UIButton *btn =[UIButton buttonWithType:UIButtonTypeSystem];40 btn.frame =CGRectMake(100, 160, 200, 30);41 btn.backgroundColor = [UIColor colorWithRed:0 green:0.4 blue:0.1 alpha:0.8];42 btn.tintColor =[UIColor redColor];43 [btn setTitle:@"submit"forState:UIControlStateNormal];44 //[btn setTitle:@"正在提交" forState:UIControlStateSelected];
//btn.layer.cornerRadius=8; 圆角
//btn.layer.masksToBounds = YES;
//btn.layer.borderWidth = 5;
//btn.layer.borderColor=(__bridge CGColorRef)([UIColor redColor]);
45
46 [btn addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside];47 //UIGestureRecognizer *tap = [[UIGestureRecognizer alloc] initWithTarget:self action:@selector(tap:)];48 //[self.view addGestureRecognizer:tap];
49
50
51
52 [self.view addSubview:btn];53 [self.view addSubview:_field];54 [self.view addSubview:labelName];55 [self.view addSubview:_psw];56 //Do any additional setup after loading the view, typically from a nib.
57 }58
59 - (void)didReceiveMemoryWarning {60 [super didReceiveMemoryWarning];61 //Dispose of any resources that can be recreated.
62 }63 //- (void)tap:(UIGestureRecognizer*)gr64 //{65 //
66 //
67 //[_field resignFirstResponder];68 //}
69
70 - (void)btnClick:(id)sender71 {72 [ViewController savePassWord:_field.text];73 _psw.text =[ViewController readPassWord];74
75 if (![_field isExclusiveTouch]) {76 //Setting this property to YES causes the receiver to block the delivery of touch events to other views in the same window. The default value of this property is NO.
77 [_field resignFirstResponder];//收回键盘
78
79 }80
81 }82
83 + (void)savePassWord:(NSString *)password84 {85 NSMutableDictionary *usernamepasswordKVPairs =[[NSMutableDictionary alloc] init];86 [usernamepasswordKVPairs setObject:password forKey:KEY_PASSWORD];87 [KeyChain save:KEY_IN_KEYCHAIN data:usernamepasswordKVPairs];88 }89
90 + (id)readPassWord91 {92 NSMutableDictionary *usernamepasswordKVPairs = (NSMutableDictionary *)[KeyChain load:KEY_IN_KEYCHAIN];93
94 return[usernamepasswordKVPairs objectForKey:KEY_PASSWORD];95
96 }97
98 + (void)deletePassWord99 {100 [KeyChain delete:KEY_IN_KEYCHAIN];101
102 }103
104 @end