新聞中心
創(chuàng)新互聯(lián)是一家集網(wǎng)站建設(shè),蒙城企業(yè)網(wǎng)站建設(shè),蒙城品牌網(wǎng)站建設(shè),網(wǎng)站定制,蒙城網(wǎng)站建設(shè)報價,網(wǎng)絡(luò)營銷,網(wǎng)絡(luò)優(yōu)化,蒙城網(wǎng)站推廣為一體的創(chuàng)新建站企業(yè),幫助傳統(tǒng)企業(yè)提升企業(yè)形象加強(qiáng)企業(yè)競爭力??沙浞譂M足這一群體相比中小企業(yè)更為豐富、高端、多元的互聯(lián)網(wǎng)需求。同時我們時刻保持專業(yè)、時尚、前沿,時刻以成就客戶成長自我,堅持不斷學(xué)習(xí)、思考、沉淀、凈化自己,讓我們?yōu)楦嗟钠髽I(yè)打造出實用型網(wǎng)站。
【原文地址:http://blog.csdn.net/toss156/article/details/7638529】
今天給大家?guī)硪粋€簡單的登陸驗證,用的是ASIHttpRequest 這個開源類庫,使用的方法很簡單,從網(wǎng)上下載下來以后,添加到項目中,并添加一下這些框架。
下面上代碼
- //
- // ViewController.h
- // NetDemo
- //
- // Created by zhouhaifeng on 12-6-6.
- // Copyright (c) 2012年 zhouhaifeng. All rights reserved.
- //
- #import
- #import "ASIHttpHeaders.h"
- #import "CJSONDeserializer.h"
- #import "tooles.h"
- @interface ViewController : UIViewController
- {
- 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];
- //設(shè)置表單提交項
- [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];
- }
- //獲取請求結(jié)果
- - (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;
- }
- }
- //連接錯誤調(diào)用這個函數(shù)
- - (void) GetErr:(ASIHTTPRequest *)request{
- [tooles removeHUD];
- [tooles MsgBox:@"網(wǎng)絡(luò)錯誤,連接不到
- }
- - (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
- if($_POST['username'] == "admin" && $_POST['password'] == "admin")
- {
- echo '{"yes":"登陸成功"}';
- }else
- {
- echo '{"error":"用戶名或密碼錯誤"}';
- };
- ?>
本文名稱:【IOS】利用ASIHTTPRequest實現(xiàn)一個簡單的登陸驗證
本文來源:http://www.dlmjj.cn/article/gheeij.html