登录
NSUserDefaults *use = [NSUserDefaults standardUserDefaults];
NSLog(@"clientId=%@",[use valueForKey:@"clientId"]);
AFHTTPRequestOperationManager *manager=[AFHTTPRequestOperationManager manager];
NSURL *url=[NSURL URLWithString:@"服务器号"];
NSMutableURLRequest *request=[NSMutableURLRequest requestWithURL:url];
[request setHTTPMethod:@"POST"];
NSString *bodyStr = [NSString stringWithFormat:@"phone=%@&password=%@&clientid=%@",_tektextField.text,_passWordTextfield.text,[use valueForKey:@"clientId"]];
[request setHTTPBody:[bodyStr dataUsingEncoding:NSUTF8StringEncoding]];
manager.responseSerializer.acceptableContentTypes = [NSSet setWithObject:@"text/html"];
__weak typeof(self) weskSelf=self;
NSOperation *operation = [manager HTTPRequestOperationWithRequest:request success:^(AFHTTPRequestOperation *operation, id responseObject) {
if ([responseObject[@"code"]isEqualToString:@"100"]) {
// //用户id
// [[NSUserDefaults standardUserDefaults]setObject:responseObject[@"userid"] forKey:@"用户"];
// //马上同步
// [[NSUserDefaults standardUserDefaults] synchronize];
// NSUserDefaults *user = [NSUserDefaults standardUserDefaults];
// NSLog(@"用户%@",[user valueForKey:@"用户"]);
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"登录成功" message:nil delegate:self cancelButtonTitle:nil otherButtonTitles:nil, nil];
alert.delegate=self;
[alert show];
[alert dismissWithClickedButtonIndex:3 animated:YES];
[self.navigationController popViewControllerAnimated:YES];
// UIStoryboard *main=[UIStoryboard storyboardWithName:@"Main" bundle:nil];
// HomepageViewController *hm=[main instantiateViewControllerWithIdentifier:@"HomepageViewController"];
// [self.navigationController pushViewController:hm animated:YES];
NSLog(@"%@",responseObject);
self.nameDic=[NSMutableDictionary dictionaryWithDictionary:responseObject[@"result"]];
//登陆成功 将用户id储存到本地 可以在工程的任何地方使用
//用户id
[[NSUserDefaults standardUserDefaults]setObject:self.nameDic[@"userid"] forKey:@"用户"];
//马上同步(必须有这一步)
[[NSUserDefaults standardUserDefaults] synchronize];
//在需要的时候可以这样取出用户id
NSUserDefaults *user = [NSUserDefaults standardUserDefaults];
NSLog(@"用户%@",[user valueForKey:@"用户"]);
[self.hud show:NO];//菊花停止
[self.hud removeFromSuperview];
}else{
[self.hud show:NO];//菊花停止
[self.hud removeFromSuperview];
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"登录失败" message:@"用户名或密码错误" delegate:self cancelButtonTitle:nil otherButtonTitles:nil, nil];
[alert show];
[alert dismissWithClickedButtonIndex:3 animated:YES];
}
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
[self.hud show:NO];//菊花停止
[self.hud removeFromSuperview];
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"登陆失败" message:@"请检查网络重试" delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
[alert show];
// [alert dismissWithClickedButtonIndex:3 animated:YES];
NSLog(@"Error: %@", error);
}];
[manager.operationQueue addOperation:operation];
}
posted on 2015-07-23 10:21 yucaijiang 阅读(238) 评论(0) 编辑 收藏 举报