【IOS】利用ASIHTTPRequest

【原创作品, 欢迎转载,转载请在明显处注明!谢谢。    

   原文地址:http://blog.csdn.net/toss156/article/details/7638529

 

今天给大家带来一个简单的登陆验证,用的是ASIHttpRequest这个开源类库,使用的方法很简单,从网上下载下来以后,添加到项目中,并添加一下这些框架。


下面上代码

[cpp]  viewplain copy
  1. //  
  2. //  ViewController.h  
  3. //  NetDemo  
  4. //  
  5. //  Created by zhouhaifeng on 12-6-6.  
  6. //  Copyright (c) 2012年 zhouhaifeng. All rights reserved.  
  7. //  
  8.   
  9. #import   
  10. #import "ASIHttpHeaders.h"  
  11. #import "CJSONDeserializer.h"  
  12. #import "tooles.h"  
  13. @interface ViewController UIViewController  
  14.  
  15.     UITextField *username;  
  16.     UITextField *password;  
  17.   
  18.  
  19. @property (nonatomic,retain) UITextField *username;  
  20. @property (nonatomic,retain) UITextField *password;  
  21.   
  22. @end  
[cpp]  viewplain copy
  1. //  
  2. //  ViewController.m  
  3. //  NetDemo  
  4. //  
  5. //  Created by zhouhaifeng on 12-6-6.  
  6. //  Copyright (c) 2012年 zhouhaifeng. All rights reserved.  
  7. //  
  8.   
  9. #import "ViewController.h"  
  10. #define HOSTURL @"http://192.168.1.105/NetDemo/index.php";  
  11.   
  12.   
  13. @interface ViewController ()  
  14. -(voidlogin:(id)sender;  
  15. -(voidGetErr:(ASIHTTPRequest *)request;  
  16. -(voidGetResult:(ASIHTTPRequest *)request;  
  17. @end  
  18.   
  19. @implementation ViewController  
  20. @synthesize username,password;  
  21. (void)viewDidLoad  
  22.  
  23.     [super viewDidLoad];  
  24.     // Do any additional setup after loading the view, typically from nib.  
  25.     UILabel *txt1 [[UILabel alloc] initWithFrame:CGRectMake(30,100,70,30)];  
  26.     [txt1 setText:@"用户名"];  
  27.     [txt1 setBackgroundColor:[UIColor clearColor]];  
  28.     [self.view addSubview:txt1];  
  29.       
  30.     UILabel *txt2 [[UILabel alloc] initWithFrame:CGRectMake(30,140,70,30)];  
  31.     [txt2 setText:@"密   码"];  
  32.     [txt2 setBackgroundColor:[UIColor clearColor]];  
  33.     [self.view addSubview:txt2];  
  34.       
  35.     username [[UITextField alloc]initWithFrame:CGRectMake(120,100, 150, 30)];  
  36.     [username setBorderStyle:UITextBorderStyleRoundedRect];  
  37.     [self.view addSubview:username];  
  38.       
  39.     password [[UITextField alloc]initWithFrame:CGRectMake(120,140, 150, 30)];  
  40.     [password setBorderStyle:UITextBorderStyleRoundedRect];  
  41.     [password setSecureTextEntry:YES];  
  42.     [self.view addSubview:password];  
  43.       
  44.     UIButton *btn [UIButton buttonWithType:UIButtonTypeRoundedRect];  
  45.     [btn setTitle:@"提交" forState:UIControlStateNormal];  
  46.     [btn addTarget:self action:@selector(login:) forControlEvents:UIControlEventTouchUpInside];  
  47.     [btn setFrame:CGRectMake(90, 180, 150, 40)];  
  48.     [self.view addSubview:btn];  
  49.       
  50.   
  51.       
  52.  
  53.   
  54. -(voidlogin:(id)sender  
  55.  
  56.     //表单提交前的验证  
  57.     if (username.text == nil||password.text==nil  
  58.         [tooles MsgBox:@"用户名或密码不能为空!"];  
  59.         return 
  60.      
  61.     //隐藏键盘  
  62.     [username resignFirstResponder];  
  63.     [password resignFirstResponder];  
  64.     //  
  65.     [tooles showHUD:@"正在登陆...."];  
  66.     NSString *urlstr HOSTURL;  
  67.     NSURL *myurl [NSURL URLWithString:urlstr];  
  68.     ASIFormDataRequest *request [ASIFormDataRequest requestWithURL:myurl];  
  69.     //设置表单提交项  
  70.     [request setPostValue:username.text forKey:@"username"];      
  71.     [request setPostValue:username.text forKey:@"password"];  
  72.     [request setDelegate:self];  
  73.     [request setDidFinishSelector:@selector(GetResult:)];  
  74.     [request setDidFailSelector:@selector(GetErr:)];  
  75.     [request startAsynchronous];  
  76.       
  77.       
  78.  
  79.   
  80.    //获取请求结果  
  81. (void)GetResult:(ASIHTTPRequest *)request{      
  82.       
  83.     [tooles removeHUD];  
  84.     NSData *data =[request responseData];  
  85.     NSDictionary *dictionary [[CJSONDeserializer deserializer] deserializeAsDictionary:data error:nil];  
  86.     class="p1">class="s1"   class="s2">//输出接收到的字符串

        NSString
     *str [NSString stringWithUTF8String:[data bytes]];    NSLog(@"%@",str);    //判断是否登陆成功  
  87.     if ([dictionary objectForKey:@"yes"])  
  88.         [tooles MsgBox:[dictionary objectForKey:@"yes"]];  
  89.         return 
  90.     }else if ([dictionary objectForKey:@"error"!= [NSNull null])  
  91.         [tooles MsgBox:[dictionary objectForKey:@"error"]];  
  92.         return 
  93.      
  94.       
  95.  
  96.   
  97.   //连接错误调用这个函数  
  98. (voidGetErr:(ASIHTTPRequest *)request{  
  99.       
  100.     [tooles removeHUD];  
  101.       
  102.     [tooles MsgBox:@"网络错误,连接不到服务器"];  
  103.  
  104.   
  105. (void)viewDidUnload  
  106.  
  107.     [super viewDidUnload];  
  108.     // Release any retained subviews of the main view.  
  109.  
  110.   
  111. (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation  
  112.  
  113.     return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown);  
  114.  
  115.   
  116. -(voiddealloc  
  117.  
  118.     [username release];  
  119.     [password release];  
  120.     [super dealloc];  
  121.  
  122.   
  123. @end  

 


php端验证的代码,随便写了下,后面就是返回一个JSON格式的字符串。
[php]  viewplain copy
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值