您的位置:首页 > 理论基础 > 计算机网络

利用ASIHttpRequest实现一个简单的登录验证

2012-10-07 10:11 911 查看
原文地址:/article/1358354.html

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








下面上代码

[cpp] view
plaincopy

//

// 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

[cpp] view
plaincopy

//

// 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];

<p class="p1"><span class="s1"> </span><span class="s2">//</span>输出接收到的字符串</p><p class="p2"><span class="s4"> NSString</span><span class="s3"> *str = [</span><span class="s4">NSString</span><span class="s3"> </span>stringWithUTF8String<span class="s3">:[data </span>bytes<span class="s3">]];</span></p><p class="p3"> <span class="s5">NSLog</span>(<span class="s6">@"%@"</span>,str);</p><p class="p1"><span class="s1"> </span><span class="s2">//</span>判断是否登陆成功</p>

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

php端验证的代码,随便写了下,后面就是返回一个JSON格式的字符串。

[php] view
plaincopy

<?php

if($_POST['username'] == "admin" && $_POST['password'] == "admin")

{

echo '{"yes":"登陆成功"}';

}else

{

echo '{"error":"用户名或密码错误"}';

};

?>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: