iOS+PHP注冊(cè)登錄系統(tǒng) iOS部分(下)
接著上篇《iOS+PHP注冊(cè)登錄系統(tǒng) PHP部分(上)》進(jìn)行學(xué)習(xí)
3.iOS部分
上一次我們寫(xiě)完了數(shù)據(jù)庫(kù)部分和PHP部分這次我們來(lái)完成iOS部分。
首先先在storyboard中一陣狂拖,弄成如下圖。
可以先在text Field中輸入用戶名和密碼 方便以后調(diào)試。

3.1登錄部分代碼
創(chuàng)建一個(gè)新的UIViewController 名為registViewController(用于注冊(cè)用戶,ViewController用于登錄)。
在ViewController.h中importregistViewController
#import "registViewController.h"
然后設(shè)置登錄界面中的控件 用來(lái)寫(xiě)用戶名的控件名設(shè)置為txtUser,密碼的控件名設(shè)置為txtPwd,確定按鈕的方法名稱為
LoginClick,注冊(cè)按鈕的方法名為registButton。
然后開(kāi)始寫(xiě)ViewController.m中的代碼
//
// ViewController.m
// iosLogin
//
// Created by 曹晗 on 16/2/25.
// Copyright :emoji: 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];
}
//用于請(qǐng)求PHP 獲得JSON
- (NSDictionary *)getJsonData:(NSString *)user_name userpwd:(NSString *)user_pwd {
NSError *error;
NSString *urlString = [NSString stringWithFormat:@"http://192.168.1.106/iosLogin/index.php?action=login&user_name=%@&user_pwd=%@",user_name,user_pwd];
//加載一個(gè)NSURL對(duì)象
NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:urlString]];
//將請(qǐng)求的url數(shù)據(jù)放到NSData對(duì)象中
NSData *response = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];
//IOS5自帶解析類NSJSONSerialization從response中解析出數(shù)據(jù)放到字典中
NSDictionary *jsonDic = [NSJSONSerialization JSONObjectWithData:response options:NSJSONReadingMutableLeaves error:&error];
NSLog(@"接收到的數(shù)據(jù)為%@",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:@"賬號(hào)或密碼錯(cuò)誤"];
}
if ([loginFlag isEqual:@"1"]) {
[alert setMessage:@"登陸成功"];
}
[alert show];
}
@end
在注冊(cè)按鈕能夠跳轉(zhuǎn)界面前,要先將stroyboard中的注冊(cè)界面的stroyboard ID設(shè)置為vc2才可以進(jìn)行跳轉(zhuǎn)。

其中這里的192.168.1.106可以寫(xiě)localhost也可以寫(xiě)自己的ip地址。
寫(xiě)到這里就可以先進(jìn)行調(diào)試一下登錄了。后面的注冊(cè)用戶代碼也和這里差不多。
3.2注冊(cè)界面代碼
先在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.
}
//這個(gè)是注冊(cè)按鈕
- (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];
}
//這個(gè)是返回按鈕
- (IBAction)returnButton:(id)sender {
[self dismissModalViewControllerAnimated:YES];
}
- (NSDictionary *)getJsonData:(NSString *)user_name userpwd:(NSString *)user_pwd {
NSError *error;
NSString *urlString = [NSString stringWithFormat:@"http://192.168.1.106/iosLogin/index.php?action=regist&user_name=%@&user_pwd=%@",user_name,user_pwd];
//加載一個(gè)NSURL對(duì)象
NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:urlString]];
//將請(qǐng)求的url數(shù)據(jù)放到NSData對(duì)象中
NSData *response = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];
//IOS5自帶解析類NSJSONSerialization從response中解析出數(shù)據(jù)放到字典中
NSDictionary *jsonDic = [NSJSONSerialization JSONObjectWithData:response options:NSJSONReadingMutableLeaves error:&error];
NSLog(@"接收到的數(shù)據(jù)為%@",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:@"注冊(cè)成功"];
}
[alert show];
}
@end
到這里所有的代碼都已經(jīng)寫(xiě)完了,我是一個(gè)新手,如果有不足或者代碼錯(cuò)誤之處還請(qǐng)指出。謝謝各位讀者。
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
iOS實(shí)現(xiàn)自定義表單實(shí)例代碼
表單對(duì)大家來(lái)說(shuō)應(yīng)該都不陌生,下面這篇文章主要給大家介紹了關(guān)于iOS如何實(shí)現(xiàn)自定義表單的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧。2018-04-04
iOS自動(dòng)進(jìn)行View標(biāo)記的方法詳解
這篇文章主要給大家介紹了關(guān)于iOS自動(dòng)進(jìn)行View標(biāo)記的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)各位iOS開(kāi)發(fā)者們具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2019-04-04
iOS TableView頭視圖根據(jù)偏移量下拉縮放效果
這篇文章主要為大家詳細(xì)介紹了iOS TableView頭視圖根據(jù)偏移量下拉縮放效果,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-05-05
IOS中(Xcode) DEBUG模式(RELEASE模式)控制NSLog輸出,NSLog輸出方式
這篇文章主要介紹了IOS中(Xcode) DEBUG模式(RELEASE模式)控制NSLog輸出,NSLog輸出方式的相關(guān)資料,需要的朋友可以參考下2016-11-11
淺述iOS11 Xcode 9 按住command 單擊 恢復(fù)到從前(直接跳轉(zhuǎn)到定義)
這篇文章主要介紹了 iOS11 Xcode 9 按住command 單擊 恢復(fù)到從前(直接跳轉(zhuǎn)到定義)的相關(guān)資料,需要的朋友可以參考下2017-10-10

