【IOS】利用ASIHTTPRequest 实现一个简单的登陆验证
来源:互联网 发布:mac 搭建vps 编辑:程序博客网 时间:2024/05/17 02:13
【原创作品, 欢迎转载,转载请在明显处注明! 谢谢。
原文地址:http://blog.csdn.net/toss156/article/details/7638529】
今天给大家带来一个简单的登陆验证,用的是ASIHttpRequest 这个开源类库,使用的方法很简单,从网上下载下来以后,添加到项目中,并添加一下这些框架。
下面上代码
//// ViewController.h// NetDemo//// Created by zhouhaifeng on 12-6-6.// Copyright (c) 2012年 zhouhaifeng. All rights reserved.//#import <UIKit/UIKit.h>#import "ASIHttpHeaders.h"#import "CJSONDeserializer.h"#import "tooles.h"@interface ViewController : UIViewController<ASIHTTPRequestDelegate>{ UITextField *username; UITextField *password;}@property (nonatomic,retain) UITextField *username;@property (nonatomic,retain) UITextField *password;@end
//// ViewController.m// NetDemo//// Created by zhouhaifeng on 12-6-6.// Copyright (c) 2012年 zhouhaifeng. All rights reserved.//#import "ViewController.h"#define HOSTURL @"http://192.168.1.105/NetDemo/index.php";@interface ViewController ()-(void) login:(id)sender;-(void) GetErr:(ASIHTTPRequest *)request;-(void) GetResult:(ASIHTTPRequest *)request;@end@implementation ViewController@synthesize username,password;- (void)viewDidLoad{ [super viewDidLoad];// Do any additional setup after loading the view, typically from a nib. UILabel *txt1 = [[UILabel alloc] initWithFrame:CGRectMake(30,100,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,100, 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]; }-(void) login:(id)sender{ //表单提交前的验证 if (username.text == nil||password.text==nil ) { [tooles MsgBox:@"用户名或密码不能为空!"];return; } //隐藏键盘 [username resignFirstResponder]; [password resignFirstResponder]; // [tooles showHUD:@"正在登陆...."];NSString *urlstr = HOSTURL;NSURL *myurl = [NSURL URLWithString:urlstr];ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL:myurl];//设置表单提交项[request setPostValue:username.text forKey:@"username"];[request setPostValue:username.text forKey:@"password"];[request setDelegate:self];[request setDidFinishSelector:@selector(GetResult:)];[request setDidFailSelector:@selector(GetErr:)];[request startAsynchronous]; } //获取请求结果- (void)GetResult:(ASIHTTPRequest *)request{[tooles removeHUD]; NSData *data =[request responseData]; NSDictionary *dictionary = [[CJSONDeserializer deserializer] deserializeAsDictionary:data error:nil];//输出接收到的字符串
NSString *str = [NSString stringWithUTF8String:[data bytes]];
NSLog(@"%@",str);
//判断是否登陆成功
if ([dictionary objectForKey:@"yes"]) {[tooles MsgBox:[dictionary objectForKey:@"yes"]];return; }else if ([dictionary objectForKey:@"error"] != [NSNull null]) {[tooles MsgBox:[dictionary objectForKey:@"error"]];return;}} //连接错误调用这个函数- (void) GetErr:(ASIHTTPRequest *)request{ [tooles removeHUD]; [tooles MsgBox:@"网络错误,连接不到服务器"];}- (void)viewDidUnload{ [super viewDidUnload]; // Release any retained subviews of the main view.}- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation{ return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown);}-(void) dealloc{ [username release]; [password release]; [super dealloc];}@end
<?phpif($_POST['username'] == "admin" && $_POST['password'] == "admin"){echo '{"yes":"登陆成功"}';}else{echo '{"error":"用户名或密码错误"}';};?>
- 【IOS】利用ASIHTTPRequest 实现一个简单的登陆验证
- 【IOS】利用ASIHTTPRequest 实现一个简单的登陆验证
- 【IOS】利用ASIHTTPRequest 实现一个简单的登陆验证
- 【IOS】利用ASIHTTPRequest 实现一个简单的登陆验证
- 利用ASIHttpRequest实现一个简单的登录验证
- extJs+json实现的一个登陆验证的简单例子
- 利用servlet进行简单的登陆验证
- 利用Xpath实现一个简单的登录验证程序
- ios实现登陆验证
- 用户登陆与验证的简单实现
- 用户登陆与验证的简单实现
- jsp实现客户登陆的简单验证
- python实现简单的登陆验证
- SpringBoot + thymeleaf 实现简单的登陆验证
- 利用uinavigattion实现简单的登陆界面
- IOS---利用ASIHTTPRequest实现异步队列
- 【MVC】使用Servlet 作为控制器实现一个简单的登陆验证
- Hibernate实现简单登陆验证
- VT值
- vc 坐标系统与影射模式
- Android更新ADT到R17以后,不能用第三方jar包了
- c#中ref与out
- 使用OpenCV官方提供文件配置、静态编译并运行OpenCV-2.3.x/2.4.x实例
- 【IOS】利用ASIHTTPRequest 实现一个简单的登陆验证
- pthread_create(linux下C语言多线程)
- UNIX Domain Socket IPC
- 数据库自增标示清零
- VC++学习笔记(四)
- Linux相关-------Makefile常用函数表
- 旁观者清:Windows Phone应用突破10万的深度分析
- 硬盘基础
- apk文件编译到系统文件中的方法