今天給大家帶來一個簡單的登陸驗證,用的是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
#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;
@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"]];
}else if ([dictionary objectForKey:@"error"] != [NSNull null]) {
[tooles MsgBox:[dictionary objectForKey:@"error"]];
//連接配接錯誤調用這個函數
- (void) GetErr:(ASIHTTPRequest *)request{
[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];
php端驗證的代碼,随便寫了下,後面就是傳回一個JSON格式的字元串。
<?php
if($_POST['username'] == "admin" && $_POST['password'] == "admin")
{
echo '{"yes":"登陸成功"}';
}else
echo '{"error":"使用者名或密碼錯誤"}';
};
?>