接着《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中的代码
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 |
//
// 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才可以进行跳转。

复制代码 代码如下: nsstring *urlstring = [nsstring stringwithformat:@"http://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中的代码。
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 |
//
// 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
|
到这里所有的代码都已经写完了,我是一个新手,如果有不足或者代码错误之处还请指出。谢谢各位读者。
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持服务器之家。








发表评论
◎欢迎参与讨论,请在这里发表您的看法、交流您的观点。