#define AccountKey @"account"
#define PwdKey @"pwd"
#define RmbPwdKey @"rmb_pwd"
#define AutoLoginKey @"auto_login"
@property (weak, nonatomic) IBOutlet UITextField *accountField;
@property (weak, nonatomic) IBOutlet UITextField *pwdField;
@property (weak, nonatomic) IBOutlet UISwitch *rmbPwdSwitch;
@property (weak, nonatomic) IBOutlet UISwitch *autoLoginSwitch;
- 自动登录: 在viewDidLoad里读取上一次登录的信息
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
//读取上次的配置(记住密码、自动登录)
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
self.accountField.text = [defaults objectForKey:AccountKey];
self.rmbPwdSwitch.on = [[defaults objectForKey:RmbPwdKey] boolValue];
self.autoLoginSwitch.on = [[defaults objectForKey:AutoLoginKey] boolValue];
//处理密码
if (self.rmbPwdSwitch.isOn) {
self.pwdField.text = [defaults objectForKey:PwdKey];
}
//处理自动登录的情况
if (self.autoLoginSwitch.isOn) {
[self login]; // 点击了『登录』按钮
}
}
-(IBAction)login {
if (![self.accountField.text isEqualToString:@"admin"]) {
//帐号不存在
[MBProgressHUD showError:@"帐户不存在"];
return;
}
if (![self.pwdField.text isEqualToString:@"123"]) {
//帐号不存在
[MBProgressHUD showError:@"密码错误"];
return;
}
//显示一个蒙版
[MBProgressHUD showMessage:@"正在登录中..."];
//发送网络请求
//隐藏蒙版
[MBProgressHUD hideHUD];
//验证成功,跳转到下一个控制器
[self performSegueWithIdentifier:@"contacts" sender:self];
//存储数据
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
[defaults setObject:self.accountField.text forKey:AccountKey];
[defaults setObject:self.pwdField.text forKey:PwdKey];
[defaults setObject:[NSNumber numberWithBool:self.rmbPwdSwitch.isOn] forKey:RmbPwdKey];
[defaults setObject:[NSNumber numberWithBool:self.autoLoginSwitch.isOn] forKey:AutoLoginKey];
[defaults synchronize];
}