日本综合一区二区|亚洲中文天堂综合|日韩欧美自拍一区|男女精品天堂一区|欧美自拍第6页亚洲成人精品一区|亚洲黄色天堂一区二区成人|超碰91偷拍第一页|日韩av夜夜嗨中文字幕|久久蜜综合视频官网|精美人妻一区二区三区

RELATEED CONSULTING
相關(guān)咨詢
選擇下列產(chǎn)品馬上在線溝通
服務(wù)時間:8:30-17:00
你可能遇到了下面的問題
關(guān)閉右側(cè)工具欄

新聞中心

這里有您想知道的互聯(lián)網(wǎng)營銷解決方案
【IOS】利用ASIHTTPRequest實現(xiàn)一個簡單的登陸驗證

創(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)上下載下來以后,添加到項目中,并添加一下這些框架。【IOS】利用ASIHTTPRequest 實現(xiàn)一個簡單的登陸驗證【IOS】利用ASIHTTPRequest 實現(xiàn)一個簡單的登陸驗證【IOS】利用ASIHTTPRequest 實現(xiàn)一個簡單的登陸驗證


下面上代碼

[cpp] view plaincopy
  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] view plaincopy
  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. -(void) login:(id)sender;  
  15. -(void) GetErr:(ASIHTTPRequest *)request;  
  16. -(void) GetResult:(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 a 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. -(void) login:(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.     //設(shè)置表單提交項  
  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.    //獲取請求結(jié)果  
  81. - (void)GetResult:(ASIHTTPRequest *)request{      
  82.       
  83.     [tooles removeHUD];  
  84.     NSData *data =[request responseData];  
  85.     NSDictionary *dictionary = [[CJSONDeserializer deserializer] deserializeAsDictionary:data error:nil];  
  86.         //輸出接收到的字符串

        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.   //連接錯誤調(diào)用這個函數(shù)  
  98. - (void) GetErr:(ASIHTTPRequest *)request{  
  99.       
  100.     [tooles removeHUD];  
  101.       
  102.     [tooles MsgBox:@"網(wǎng)絡(luò)錯誤,連接不到
  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. -(void) dealloc  
  117. {  
  118.     [username release];  
  119.     [password release];  
  120.     [super dealloc];  
  121. }  
  122.   
  123. @end  


php端驗證的代碼,隨便寫了下,后面就是返回一個JSON格式的字符串。
[php] view plaincopy
  1.     if($_POST['username'] == "admin" &&  $_POST['password'] == "admin")  
  2.     {  
  3.         echo '{"yes":"登陸成功"}';  
  4.     }else  
  5.     {  
  6.         echo '{"error":"用戶名或密碼錯誤"}';  
  7.     };  
  8. ?>  

本文名稱:【IOS】利用ASIHTTPRequest實現(xiàn)一個簡單的登陸驗證
本文來源:
http://www.dlmjj.cn/article/gheeij.html