#import <UIKit/UIKit.h> #import "ASIHttpHeaders.h" #import "tooles.h" @interface 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"; @interface 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