前一章用MKNetworkit和Express模拟了登陆验证的功能,但是在实际应用中会出现一个问题,就是MKNetworkit并不支持同步,也就是说在登陆验证的时候,可能服务器数据还未返回,就已经开始了下一步操作,比如说跳转到下个界面。
比较简单地解决方法就是用Rest web Service基础的同步get,post方法实现登陆验证。
直接上代码:
-(void)loginpost:(NSString*)user pwd:(NSString*)password{
[GMDCircleLoader setOnView:self.view withTitle:@"Loading..." animated:YES];//一个第三方的活动指示器
// [self.act startAnimating];
// [self.view addSubview:self.act];
NSURL* host = [NSURL URLWithString:@"http://127.0.0.1:3000/login?"];
NSString* path = [[NSString alloc]initWithFormat:@"user=%@&password=%@",user,password];
NSData* pathdata = [path dataUsingEncoding:NSUTF8StringEncoding];//post内容序列化
NSMutableURLRequest* req = [[NSMutableURLRequest alloc]initWithURL:host cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:10];
[req setHTTPMethod:@"POST"];
[req setHTTPBody:pathdata];
NSError* e=nil;
NSData* recivedata = [NSURLConnection
sendSynchronousRequest:req
returningResponse:nil error:&e];//同步请求
if(e){//连接服务器错误
[NSTimer scheduledTimerWithTimeInterval:3.0
target:self selector:@selector(connectfailed:)
userInfo:[e localizedDescription]
repeats:NO];
return;
}
//成功返回数据
if(recivedata){
NSError* er = nil;
NSDictionary* dict = [NSJSONSerialization JSONObjectWithData:recivedata options:NSJSONReadingAllowFragments error:&er];
if(!er){
NSString* rescode = [dict valueForKey:@"resultcode"];
if([rescode isEqualToString:@"true"]){
[NSTimer scheduledTimerWithTimeInterval:3.0 target:self selector:@selector(logined) userInfo:nil repeats:NO];
}else{
[NSTimer scheduledTimerWithTimeInterval:3.0 target:self selector:@selector(loginedfailed) userInfo:nil repeats:NO];
}
}
}
}
登陆验证的时候会有三种情况,一种根本连不上数据库,一种用户名或者密码错误,一种登陆成功。这里加上活动指示器,是为了防止和服务器通信时间过长导致的主界面“假死”。
//链接失败
-(void)connectfailed:(NSTimer*)timer{
[GMDCircleLoader hideFromView:self.view animated:YES];
// [self.act stopAnimating];
// [self.act removeFromSuperview];
NSLog(@"conenctd failed");
UIAlertView* alter = [[UIAlertView alloc]initWithTitle:@"错误" message:[timer userInfo] delegate:nil cancelButtonTitle:@"OK" otherButtonTitles: nil];
[alter show];
}
//登陆成功
-(void)logined{
[GMDCircleLoader hideFromView:self.view animated:YES];
// [self.act stopAnimating];
// [self.act removeFromSuperview];
NSLog(@"login success!");
[self performSegueWithIdentifier:@"showdetail" sender:self];
}
//用户名或密码错误
-(void)loginedfailed{
[GMDCircleLoader hideFromView:self.view animated:YES];
// [self.act stopAnimating];
// [self.act removeFromSuperview];
NSLog(@"login failed!");
UIAlertView* alter = [[UIAlertView alloc]initWithTitle:@"登陆失败" message:@"用户名或密码错误" delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];
[alter show];
}
这里用了个第三方的活动指示器,具体链接:
https://github.com/gabemdev/GMDCircleLoader
注释掉的都是自带的UIActivityIndicatorView,使用方法也很简单,直接在
viewDidLoad中初始化:
self.size = self.view.frame.size;
self.act = [[UIActivityIndicatorView alloc]initWithFrame:CGRectMake(self.size.width/2-50, self.size.height/3, 100, 50)];
self.act.activityIndicatorViewStyle=UIActivityIndicatorViewStyleGray;//指示器类型
//设置活动指示器的颜色
elf.act.color=[UIColor redColor];
//hidesWhenStopped默认为YES,就是只有在指示器运行的时候才显示
self.act.hidesWhenStopped=YES;