案例步驟:
1> 搭建基本UI界面
2> 監(jiān)聽(tīng)用戶textFiled的EditingChanged
[self.nameText addTarget:self action:@selector(name:) forControlEvents:UIControlEventEditingChanged];
[self.pwdText addTarget:self action:@selector(pwd:) forControlEvents:UIControlEventEditingChanged];
3> 將用戶的信息添加到請(qǐng)求中,使用的是POST請(qǐng)求,(這里面這涉及到connection的基本操作)
4> 使用connection發(fā)送異步請(qǐng)求
5> 在異步請(qǐng)求中的block回調(diào)中拿到服務(wù)器響應(yīng)的數(shù)據(jù)判斷是否登錄成功
6> 處理自動(dòng)登錄業(yè)務(wù)
- 監(jiān)聽(tīng)switch的點(diǎn)擊,獲取switch.on保存到用戶的偏好設(shè)置里面
- 注意:(在保存Switch.on為yes的情況下)登錄成功的同時(shí)保存用戶數(shù)據(jù)到沙盒中,如果登錄是失敗的,但是用戶的switch.on也為yes的話,我們要把沙盒中的switch.on數(shù)據(jù)寫(xiě)為no
- 在viewDidLoad中取出沙盒中的數(shù)據(jù),判斷是否為自動(dòng)登錄,如果是直接取出沙盒中的數(shù)據(jù)賦給我們的textField;
案例代碼
- (void)viewDidLoad
{
[self.nameText addTarget:self action:@selector(name:) forControlEvents:UIControlEventEditingChanged];
[self.pwdText addTarget:self action:@selector(pwd:) forControlEvents:UIControlEventEditingChanged];
//從沙盒中取出來(lái)
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:@"請(qǐng)輸入姓名"];
// }
// if (self.pwdText.text.length == 0) {
// [SVProgressHUD showInfoWithStatus:@"請(qǐng)輸入密碼"];
// }
if (self.nameText.text.length != 0 && self.pwdText.text.length != 0) {
[self login];
}else
{
[SVProgressHUD showInfoWithStatus:@"請(qǐng)輸入姓名或密碼"];
}
}
- (void)login
{
[SVProgressHUD showWithStatus:@"正在登錄" maskType:SVProgressHUDMaskTypeBlack];
NSLog(@"login");
//1.創(chuàng)建url
NSURL *url = [NSURL URLWithString:@"http://120.25.226.186:32812/login"];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
NSString *userName = self.nameText.text;
//轉(zhuǎn)碼
// 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) {
//回調(diào)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:@"登錄失敗,請(qǐng)核對(duì)賬號(hào)密碼" maskType:SVProgressHUDMaskTypeBlack];
if (self.outoLogin.on) {
[self.defaults setBool:NO forKey:@"outoLogin"];
}
}else
{
[SVProgressHUD showSuccessWithStatus:@"登錄成功" maskType:SVProgressHUDMaskTypeBlack];
//判斷是witch的狀態(tài)
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(@"__");
}
//監(jiān)聽(tīng)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 簡(jiǎn)書(shū).lovepeijun