你好,歡迎來到IOS教程網

 Ios教程網 >> IOS編程開發 >> IOS開發綜合 >> [IOS]九城問答系統客戶端登陸驗證的設計

[IOS]九城問答系統客戶端登陸驗證的設計

編輯:IOS開發綜合
前言: 最近要做一個九城問答系統手機客戶端的應用,接手到項目當然知道最重要的就是要解決跟服務器通信獲取數據的一個問題,首先還是從驗證登陸解決起吧,小小嘗試了一下終於成功完成了。下面就寫一下做的登陸驗證的一個過程,一起學習,共同提高! 主要技術點: 開源框架類ASIHttpRequest,toast彈出框,MD5加密算法 問答系統登陸驗證API: appKey : F0D71CBA8E2D....470A1F33AD457   1,用戶校驗接口 接口url: http://uc.gongzuohou.cn/index.php/api/user/login 請求方式: POST 請求參數: 上行: uname string #用戶名 upwd string #用戶密碼 md5(upwd) sign string #校驗碼 生成方式:md5(uname . upasswd . appKey)   下行: errno:狀態碼 0表示成功,>1 表示失敗 error:返回內容 uid :用戶ID ,僅在用戶登錄成功後後才返回 uname:用戶名 ,僅在用戶登錄成功後後才返回   有了這些前期的准備就可以開始著手做了。 實現步驟: 1.導入framework框架   2.導入ASIHttpRequest開源類庫   3.導入MD5和toast的類庫   4.ViewController.h:  
#import <UIKit/UIKit.h>  
#import "ASIHttpHeaders.h"  
#import "tooles.h"  
  
@interface MainViewController : UIViewController<NSURLConnectionDataDelegate>  
{  
    UITextField *username;  
    UITextField *password;  
}  
  
@property (nonatomic,retain) UITextField *username;  
@property (nonatomic,retain) UITextField *password;  
@property (nonatomic,retain) NSString *md5pwd;  
@property (nonatomic,retain) NSString *md5str;  
@property(retain,nonatomic)NSData *data;  
@end  

 

5.ViewController.m:
#import "MainViewController.h"  
#define URL @"http://uc.gongzuohou.cn/index.php/api/user/login";  
#import "MyMD5.h"  
#define appKey @"F0D71CBA8E2D.....470A1F33AD457";  
@interface MainViewController ()  
-(void) login:(id)sender;    
-(void) GetErr:(ASIHTTPRequest *)request;    
-(void) GetResult:(ASIHTTPRequest *)request;    
  
@end  
  
@implementation MainViewController  
@synthesize username,password;    
  
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil  
{  
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];  
    if (self) {  
          
          
    }  
    return self;  
}  
  
-(void) login:(id)sender    
{    
    //表單提交前的驗證    
    if (username.text == nil||password.text==nil ) {    
        [tooles MsgBox:@"用戶名或密碼不能為空!"];    
        return;    
    }    
    //隱藏鍵盤    
    [username resignFirstResponder];    
    [password resignFirstResponder];    
      
    [tooles showHUD:@"正在登陸...."];    
    NSString *urlstr = URL;    
    NSURL *myurl = [NSURL URLWithString:urlstr];    
    ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL:myurl];  
    NSString * name = username.text;  
    _md5pwd = [MyMD5 md5:password.text];  
    NSString * appkey1 = appKey;  
    NSString * s = [[NSString alloc] initWithFormat:@"%@%@%@",name,_md5pwd,appkey1];  
    _md5str = [MyMD5 md5:s];  
      
    NSLog(@"%@",_md5pwd);  
    //設置表單提交項    
    [request setPostValue:username.text forKey:@"uname"];        
    [request setPostValue:_md5pwd forKey:@"upwd"];  
    [request setPostValue:_md5str forKey:@"sign"];  
    [request setDelegate:self];    
    [request setDidFinishSelector:@selector(GetResult:)];    
    [request setDidFailSelector:@selector(GetErr:)];    
    [request startAsynchronous];    
}    
  
//獲取請求結果    
- (void)GetResult:(ASIHTTPRequest *)request{  
      
    [tooles removeHUD];  
      
      
      
    //接收字符串數據  
    NSString *str = [request responseString];  
    NSLog(@"-------\n%@",str);  
      
      
    //接收二進制數據  
    _data = [request responseData];  
    NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:_data options:NSJSONReadingMutableContainers  error:nil];  
    NSLog(@"------\n%@",dic);  
      
    //返回的用戶名  
    NSString * sname = [dic objectForKey:@"username"];  
    NSLog(@"name:%@",sname);  
      
    int flag = -1;  
    flag = [[dic objectForKey:@"errno"] integerValue];  
    if (flag == 0) {  
        [tooles MsgBox:@"登陸驗證成功"];  
    }  
    else if (flag == 1)  
    {  
        [tooles MsgBox:@"用戶名或密碼錯誤"];  
    }  
}  
  
//連接錯誤調用這個函數    
- (void) GetErr:(ASIHTTPRequest *)request{    
      
    [tooles removeHUD];    
      
    [tooles MsgBox:@"網絡錯誤,連接不到服務器"];    
}    
  
- (void)viewDidLoad    
{    
    [super viewDidLoad];    
    // Do any additional setup after loading the view, typically from a nib.  
      
      
      
    UILabel *txt1 = [[UILabel alloc] initWithFrame:CGRectMake(30,90,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,90, 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];    
      
      
}    
  

 

  @end    
  1. 上一頁:
  2. 下一頁:
蘋果刷機越獄教程| IOS教程問題解答| IOS技巧綜合| IOS7技巧| IOS8教程
Copyright © Ios教程網 All Rights Reserved