接着上篇《iOS+PHP注册登录系统 PHP部分(上)》进行学习
3.iOS部分
上一次我们写完了数据库部分和PHP部分这次我们来完成iOS部分。
首先先在storyboard中一阵狂拖,弄成如下图。
可以先在text Field中输入用户名和密码 方便以后调试。
3.1登录部分代码
创建一个新的UIViewController 名为registViewController(用于注册用户,ViewController用于登录)。
在ViewController.h中importregistViewController
#import "registViewController.h"
然后设置登录界面中的控件 用来写用户名的控件名设置为txtUser,密码的控件名设置为txtPwd,确定按钮的方法名称为
LoginClick,注册按钮的方法名为registButton。
然后开始写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]; } //用于请求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]; //加载一个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
在注册按钮能够跳转界面前,要先将stroyboard中的注册界面的stroyboard ID设置为vc2才可以进行跳转。
其中这里的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:@"http://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
到这里所有的代码都已经写完了,我是一个新手,如果有不足或者代码错误之处还请指出。谢谢各位读者。
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持武林网。
新闻热点
疑难解答