案例步骤:
1> 搭建基本UI界面
2> 监听用户textFiled的EditingChanged
[self.nameText addTarget:self action:@selector(name:) forControlEvents:UIControlEventEditingChanged];
[self.pwdText addTarget:self action:@selector(pwd:) forControlEvents:UIControlEventEditingChanged];
3> 将用户的信息添加到请求中,使用的是POST请求,(这里面这涉及到connection的基本操作)
4> 使用connection发送异步请求
5> 在异步请求中的block回调中拿到服务器响应的数据判断是否登录成功
6> 处理自动登录业务
- 监听switch的点击,获取switch.on保存到用户的偏好设置里面
- 注意:(在保存Switch.on为yes的情况下)登录成功的同时保存用户数据到沙盒中,如果登录是失败的,但是用户的switch.on也为yes的话,我们要把沙盒中的switch.on数据写为no
- 在viewDidLoad中取出沙盒中的数据,判断是否为自动登录,如果是直接取出沙盒中的数据赋给我们的textField;
案例代码
- (void)viewDidLoad
{
[self.nameText addTarget:self action:@selector(name:) forControlEvents:UIControlEventEditingChanged];
[self.pwdText addTarget:self action:@selector(pwd:) forControlEvents:UIControlEventEditingChanged];
//从沙盒中取出来
self.outoLogin.on = [self.defaults boolForKey:@"outoLogin"];
// NSLog(@"%bool",[defaults objectForKey:@"outoLogin"]);
if (self.outoLogin.isOn) {
NSString *userName = [self.defaults objectForKey:@"userName"];
NSString *pwd = [self.defaults objectForKey:@"pwd"];
self.nameText.text = userName;
self.pwdText.text = pwd;
}
}
- (IBAction)outoLogin:(UISwitch *)sender {
[self.defaults setBool:sender.on forKey:@"outoLogin"];
}
- (NSUserDefaults *)defaults
{
if (_defaults == nil) {
_defaults = [NSUserDefaults standardUserDefaults];
}
return _defaults;
}
- (IBAction)loginBtn:(id)sender {
// if (self.nameText.text.length == 0) {
// [SVProgressHUD showInfoWithStatus:@"请输入姓名"];
// }
// if (self.pwdText.text.length == 0) {
// [SVProgressHUD showInfoWithStatus:@"请输入密码"];
// }
if (self.nameText.text.length != 0 && self.pwdText.text.length != 0) {
[self login];
}else
{
[SVProgressHUD showInfoWithStatus:@"请输入姓名或密码"];
}
}
- (void)login
{
[SVProgressHUD showWithStatus:@"正在登录" maskType:SVProgressHUDMaskTypeBlack];
NSLog(@"login");
//1.创建url
NSURL *url = [NSURL URLWithString:@"http://120.25.226.186:32812/login"];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
NSString *userName = self.nameText.text;
//转码
// userName = [userName stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
NSString *pwd = self.pwdText.text;
NSLog(@"%@_%@",userName,pwd);
request.HTTPMethod = @"POST";
NSString *bodyStr = [NSString stringWithFormat:@"username=%@&pwd=%@",userName, pwd];
request.HTTPBody = [bodyStr dataUsingEncoding:NSUTF8StringEncoding];
[NSURLConnection sendAsynchronousRequest:request queue:[[NSOperationQueue alloc]init] completionHandler:^(NSURLResponse * _Nullable response, NSData * _Nullable data, NSError * _Nullable connectionError) {
//回调block
NSString *dataStr = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
//截取字符串(判断是否登录成功)
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
if ([dataStr containsString:@"error"]) {
[SVProgressHUD showErrorWithStatus:@"登录失败,请核对账号密码" maskType:SVProgressHUDMaskTypeBlack];
if (self.outoLogin.on) {
[self.defaults setBool:NO forKey:@"outoLogin"];
}
}else
{
[SVProgressHUD showSuccessWithStatus:@"登录成功" maskType:SVProgressHUDMaskTypeBlack];
//判断是witch的状态
if (self.outoLogin.on) {
[self.defaults setValue:userName forKey:@"userName"];
[self.defaults setValue:pwd forKey:@"pwd"];
}
}
});
}];
}
#pragma mark - NSURLConnectionDataDelegate
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
{
NSLog(@"__");
}
//监听textFiled
- (void)name:(UITextField *)name
{
self.nameText.text = name.text;
// NSLog(@"%@",name.text);
}
- (void)pwd:(UITextField *)pwd
{
self.pwdText.text = pwd.text;
// NSLog(@"%@",pwd.text);
}
- Posted by 简书.lovepeijun