pass2word例子---------------启动密码存储

来源:互联网 发布:大连软件职业学院地址 编辑:程序博客网 时间:2024/06/10 07:06

设置密码

[objc] view plaincopy在CODE上查看代码片派生到我的代码片
  1. #pragma mark-- switch change  
  2. - (void)switchChanged:(id)sender  
  3. {  
  4.     if (_passwordProtect.on == NO)  
  5.     {  
  6.         _passwordProtectAlertView = [[UIAlertView alloc] initWithTitle:@"警告" message:@"确定取消主密码保护吗? 唤醒应用不再需要密码!" delegate:self cancelButtonTitle:@"确定" otherButtonTitles:@"取消", nil nil];  
  7.         [_passwordProtectAlertView show];  
  8.     }  
  9.     else  
  10.     {  
  11.         _setPasswordAlertView = [[UIAlertView alloc] initWithTitle:@"请输入密码:" message:@"\n" delegate:self cancelButtonTitle:@"取消"  
  12.                otherButtonTitles:@"确定", nil nil];  
  13.         _setPasswordAlertView.alertViewStyle = UIAlertViewStyleSecureTextInput;  
  14.           
  15.         [_setPasswordAlertView show];  
  16.           
  17.     }  
  18. }  
  19.   
  20. #pragma mark-- alert delecate  
  21. - (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex  
  22. {  
  23.     if (alertView == _passwordProtectAlertView)  
  24.     {  
  25.         switch (buttonIndex)  
  26.         {  
  27.                 //确定  
  28.             case 0:  
  29.                 _islk = NO;  
  30.                 break;  
  31.                 //取消  
  32.             case 1:  
  33.                 [_passwordProtect setOn:YES animated:YES];  
  34.                 break;  
  35.             default:  
  36.                 break;  
  37.         }  
  38.     }  
  39.     else if(alertView == _setPasswordAlertView )  
  40.     {  
  41.         if (buttonIndex == 0)  
  42.         {  
  43.             [_passwordProtect setOn:NO animated:YES];  
  44.         }  
  45.         else  
  46.         {  
  47.             NSString *pwd = [_setPasswordAlertView textFieldAtIndex:0].text;  
  48.             if ([pwd length])  
  49.             {  
  50.                 //save password  
  51.                 [[NSUserDefaults standardUserDefaults] setObject:pwd forKey:@"mainpassword"];  
  52.                 [[NSUserDefaults standardUserDefaults] synchronize];  
  53.                 _islk = YES;  
  54.   
  55.             }  
  56.             else  
  57.                 [_passwordProtect setOn:NO animated:YES];  
  58.         }  
  59.     }  
  60.     [[NSUserDefaults standardUserDefaults] setBool:_islk forKey:@"islocked"];  
  61.     [[NSUserDefaults standardUserDefaults] synchronize];  
  62. }  



获取密码

[objc] view plaincopy在CODE上查看代码片派生到我的代码片
  1. - (IBAction)login:(id)sender  
  2. {  
  3.     NSString *pswd = [[NSUserDefaults standardUserDefaults] objectForKey:@"mainpassword"];  
  4.     if([_passwordTextField.text isEqualToString:pswd])  
  5.         [self.presentingViewController dismissViewControllerAnimated:NO completion:nil];  
  6.     else  
  7.     {  
  8.         UIAlertView *alertV = [[UIAlertView alloc] initWithTitle:@"警告" message:@"密码错误" delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil, nil nil];  
  9.         [alertV show];  
  10.     }  
  11.     _passwordTextField.text = @"";  
  12. }  
0 0
原创粉丝点击