[IOS]九城问答系统客户端登陆验证的设计
前言:最近要做一个九城问答系统手机客户端的应用,接手到项目当然知道最重要的就是要解决跟服务器通信获取数据的一个问题,首先还是从验证登陆解决起吧,小小尝试了一下终于成功完成了。下面就写一下做的登陆验证的一个过程,一起学习,共同提高!
主要技术点:
开源框架类ASIHttpRequest,toast弹出框,MD5加密算法
问答系统登陆验证API:
appKey : F0D71CBA8E2D....470A1F33AD457
1,用户校验接口
接口url: http://uc.gongzuohou.cn/index.php/api/user/login
请求方式: POST
请求参数:
上行:
uname string #用户名
upwd string #用户密码 md5(upwd)
sign string #校验码 生成方式:md5(uname . upasswd . appKey)
下行:
errno:状态码 0表示成功,>1 表示失败
error:返回内容
uid :用户ID ,仅在用户登录成功后后才返回
uname:用户名 ,仅在用户登录成功后后才返回
有了这些前期的准备就可以开始着手做了。
实现步骤:
1.导入framework框架
2.导入ASIHttpRequest开源类库
3.导入MD5和toast的类库
4.ViewController.h:
#import <UIKit/UIKit.h> #import "ASIHttpHeaders.h" #import "tooles.h" @inte易做图ce MainViewController : UIViewController<NSURLConnectionDataDelegate> { UITextField *username; UITextField *password; } @property (nonatomic,retain) UITextField *username; @property (nonatomic,retain) UITextField *password; @property (nonatomic,retain) NSString *md5pwd; @property (nonatomic,retain) NSString *md5str; @property(retain,nonatomic)NSData *data; @end
5.ViewController.m:
#import "MainViewController.h" #define URL @"http://uc.gongzuohou.cn/index.php/api/user/login"; #import "MyMD5.h" #define appKey @"F0D71CBA8E2D.....470A1F33AD457"; @inte易做图ce MainViewController () -(void) login:(id)sender; -(void) GetErr:(ASIHTTPRequest *)request; -(void) GetResult:(ASIHTTPRequest *)request; @end @implementation MainViewController @synthesize username,password; - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil { self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]; if (self) { } return self; } -(void) login:(id)sender { //表单提交前的验证 if (username.text == nil||password.text==nil ) { [tooles MsgBox:@"用户名或密码不能为空!"]; return; } //隐藏键盘 [username resignFirstResponder]; [password resignFirstResponder]; [tooles showHUD:@"正在登陆...."]; NSString *urlstr = URL; NSURL *myurl = [NSURL URLWithString:urlstr]; ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL:myurl]; NSString * name = username.text; _md5pwd = [MyMD5 md5:password.text]; NSString * appkey1 = appKey; NSString * s = [[NSString alloc] initWithFormat:@"%@%@%@",name,_md5pwd,appkey1]; _md5str = [MyMD5 md5:s]; NSLog(@"%@",_md5pwd); //设置表单提交项 [request setPostValue:username.text forKey:@"uname"]; [request setPostValue:_md5pwd forKey:@"upwd"]; [request setPostValue:_md5str forKey:@"sign"]; [request setDelegate:self]; [request setDidFinishSelector:@selector(GetResult:)]; [request setDidFailSelector:@selector(GetErr:)]; [request startAsynchronous]; } //获取请求结果 - (void)GetResult:(ASIHTTPRequest *)request{ [tooles removeHUD]; //接收字符串数据 NSString *str = [request responseString]; NSLog(@"-------\n%@",str); //接收二进制数据 _data = [request responseData]; NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:_data options:NSJSONReadingMutableContainers error:nil]; NSLog(@"------\n%@",dic); //返回的用户名 NSString * sname = [dic objectForKey:@"username"]; NSLog(@"name:%@",sname); int flag = -1; flag = [[dic objectForKey:@"errno"] integerValue]; if (flag == 0) { [tooles MsgBox:@"登陆验证成功"]; } else if (flag == 1) { [tooles MsgBox:@"用户名或密码错误"]; } } //连接错误调用这个函数 - (void) GetErr:(ASIHTTPRequest *)request{ [tooles removeHUD]; [tooles MsgBox:@"网络错误,连接不到服务器"]; } - (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view, typically from a nib. UILabel *txt1 = [[UILabel alloc] initWithFrame:CGRectMake(30,90,70,30)]; [txt1 setText:@"用户名"]; [txt1 setBackgroundColor:[UIColor clearColor]]; [self.view addSubview:txt1]; UILabel *txt2 = [[UILabel alloc] initWithFrame:CGRectMake(30,140,70,30)]; [txt2 setText:@"密 码"]; [txt2 setBackgroundColor:[UIColor clearColor]]; [self.view addSubview:txt2]; username = [[UITextField alloc]initWithFrame:CGRectMake(120,90, 150, 30)]; [username setBorderStyle:UITextBorderStyleRoundedRect]; [self.view addSubview:username]; password = [[UITextField alloc]initWithFrame:CGRectMake(120,140, 150, 30)]; [password setBorderStyle:UITextBorderStyleRoundedRect]; [password setSecureTextEntry:YES]; [self.view addSubview:password]; UIButton *btn = [UIButton buttonWithType:UIButtonTypeRoundedRect]; [btn setTitle:@"提交" forState:UIControlStateNormal]; [btn addTarget:self action:@selector(login:) forControlEvents:UIControlEventTouchUpInside]; [btn setFrame:CGRectMake(90, 180, 150, 40)]; [self.view addSubview:btn]; }
@end
补充:移动开发 , IOS ,