您现在的位置: IOS教程網 >> IOS系統 >  >> iOS 程式開發 >> iOS 開發綜合

iOS + PHP 註冊登入系統實作(iOS 部分·下)

編輯:iOS 開發綜合

接續上一篇《iOS + PHP 註冊登入系統 PHP 部分(上)》繼續學習。

3. iOS 部分

上次我們完成了資料庫部分與 PHP 部分,這次來完成 iOS 部分。首先在 storyboard 中一陣狂拖,弄成如下圖,並可先在 textField 中輸入使用者名稱與密碼,方便日後調試。

3.1 登入部分程式碼

建立一個新的 UIViewController,命名為 registViewController(用於註冊使用者,ViewController 用於登入)。在 ViewController.h 中 import registViewController:

#import "registViewController.h"

接著設定登入介面中的控件:用來寫使用者名稱的控件命名為 txtUser,密碼的控件命名為 txtPwd,確定按鈕的方法名為 LoginClick,註冊按鈕的方法名為 registButton。然後開始寫 ViewController.m 中的程式碼。

 // // ViewController.m // iosLogin // // Created by 曹晗 on 16/2/25. // Copyright 2016年 CaoHan. All rights reserved. // #import "ViewController.h" @interface ViewController () @property (weak, nonatomic) IBOutlet UITextField *txtUser; @property (weak, nonatomic) IBOutlet UITextField *txtPwd; @end @implementation ViewController - (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view, typically from a nib. } - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated. } - (IBAction)LoginClick:(id)sender { //前後去空格 NSString *userName = [_txtUser.text stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]]; NSString *userPwd = [_txtPwd.text stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]]; NSDictionary *jsonDic = [self getJsonData:userName userpwd:userPwd]; NSString* loginFlag = [jsonDic objectForKey:@"loginFlag"]; NSLog(@"%@",loginFlag); [self aletrInfo:loginFlag]; } - (IBAction)registButton:(id)sender { UIStoryboard *storboard = self.storyboard; registViewController *vc2 = [storboard instantiateViewControllerWithIdentifier:@"vc2"]; [self presentViewController:vc2 animated:YES completion:nil]; } //用於請求PHP 獲得JSON - (NSDictionary *)getJsonData:(NSString *)user_name userpwd:(NSString *)user_pwd { NSError *error; NSString *urlString = [NSString stringWithFormat:@"https://192.168.1.106/iosLogin/index.php?action=login&user_name=%@&user_pwd=%@",user_name,user_pwd]; //加載一個NSURL對象 NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:urlString]]; //將請求的url數據放到NSData對象中 NSData *response = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil]; //IOS5自帶解析類NSJSONSerialization從response中解析出數據放到字典中 NSDictionary *jsonDic = [NSJSONSerialization JSONObjectWithData:response options:NSJSONReadingMutableLeaves error:&error]; NSLog(@"接收到的數據為%@",jsonDic); return jsonDic; } //彈出信息 - (void)aletrInfo:(NSString *)loginFlag{ UIAlertView *alert = [[UIAlertView alloc]init]; [alert setTitle:@"提示"]; [alert setDelegate:nil]; [alert addButtonWithTitle:@"確定"]; if ([loginFlag isEqual: @"0"]) { [alert setMessage:@"賬號或密碼錯誤"]; } if ([loginFlag isEqual:@"1"]) { [alert setMessage:@"登陸成功"]; } [alert show]; } @end 

在註冊按鈕能夠跳轉界面前,要先將 storyboard 中註冊界面的 storyboard ID 設為 vc2 才可以進行跳轉。

複製程式碼如下:

NSString *urlString = [NSString stringWithFormat:@"https://192.168.1.106/iosLogin/index.php?action=login&user_name=%@&user_pwd=%@",user_name,user_pwd];

其中這裡的 192.168.1.106 可以寫 localhost,也可以寫自己的 IP 位址。寫到這裡就可以先調試一下登入了,後面的註冊使用者程式碼也和這裡差不多。

3.2 註冊界面程式碼

先在 registViewCongroller.h 中 import ViewController.h:

#import "ViewController.h"

然後是 registViewController.m 中的程式碼。

 // // registViewController.m // iosLogin // // Created by 曹晗 on 16/2/27. // Copyright 2016年 CaoHan. All rights reserved. // #import "registViewController.h" @interface registViewController () @property (weak, nonatomic) IBOutlet UITextField *txtUser; @property (weak, nonatomic) IBOutlet UITextField *txtPwd; @end @implementation registViewController - (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view. } - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated. } //這個是注冊按鈕 - (IBAction)registButton:(id)sender { NSString *userName = [_txtUser.text stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]]; NSString *userPwd = [_txtPwd.text stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]]; NSDictionary *jsonDic = [self getJsonData:userName userpwd:userPwd]; NSString* registFlag = [jsonDic objectForKey:@"registFlag"]; NSLog(@"%@",registFlag); [self aletrInfo:registFlag]; } //這個是返回按鈕 - (IBAction)returnButton:(id)sender { [self dismissModalViewControllerAnimated:YES]; } - (NSDictionary *)getJsonData:(NSString *)user_name userpwd:(NSString *)user_pwd { NSError *error; NSString *urlString = [NSString stringWithFormat:@"https://192.168.1.106/iosLogin/index.php?action=regist&user_name=%@&user_pwd=%@",user_name,user_pwd]; //加載一個NSURL對象 NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:urlString]]; //將請求的url數據放到NSData對象中 NSData *response = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil]; //IOS5自帶解析類NSJSONSerialization從response中解析出數據放到字典中 NSDictionary *jsonDic = [NSJSONSerialization JSONObjectWithData:response options:NSJSONReadingMutableLeaves error:&error]; NSLog(@"接收到的數據為%@",jsonDic); return jsonDic; } - (void)aletrInfo:(NSString *)registFlag{ UIAlertView *alert = [[UIAlertView alloc]init]; [alert setTitle:@"提示"]; [alert setDelegate:nil]; [alert addButtonWithTitle:@"確定"]; if ([registFlag isEqual: @"0"]) { [alert setMessage:@"用戶名已存在"]; } if ([registFlag isEqual:@"1"]) { [alert setMessage:@"注冊成功"]; } [alert show]; } @end 

到這裡所有程式碼都寫完了。我是一個新手,若有不足或程式碼錯誤之處,還請不吝指正,謝謝各位讀者。

以上就是本文的全部內容,希望對大家的學習有所幫助。

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