登录、注册页面设计
要求
1.设计实现注册数据持久化
2.设计必须查看用户的真实性
主要知识点
1.bmob云端数据库
2.正则表达式
3.UIAlertController运用
bomb基本操作
1.bmob基本操作必须导入第三方BmobSDK库
2.在工程里面的APPDelegate中设置
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
self.window.rootViewController=[TheloginViewController new];
[Bmob registerWithAppKey:@"80c5ece81e09f103a877e5"];这里将自己bmob云端上的ID复制过来 即可进行基本操作
return YES;
}
3.实现简单的对云端数据增删改查
//增加信息
BmobObject *thelogin = [BmobObject objectWithClassName:@"The_login"];
[thelogin setObject:[NSNumber numberWithLong:18798795632] forKey:@"MobilePhone"];
[thelogin setObject:@"666666" forKey:@"pwd"];
//异步保存到服务器
[thelogin saveInBackgroundWithResultBlock:^(BOOL isSuccessful, NSError *error) {
if (isSuccessful) {
NSLog(@"%@",thelogin);
}else{
NSLog(@"%@",error);
}
}];
//删除
BmobObject *bmobObject = [BmobObject objectWithoutDatatWithClassName:@"The_login" objectId:@"baaf9cfa1b"];
[bmobObject deleteInBackgroundWithBlock:^(BOOL isSuccessful, NSError *error) {
if (isSuccessful) {
//删除成功后的动作
NSLog(@"successful");
} else {
NSLog(@"%@",error);
}
}];
//改
BmobObject *theloginchange=[BmobObject objectWithoutDatatWithClassName:@"The_login" objectId:@"af0e0c4b46"];
[theloginchange setObject:@"888888" forKey:@"pwd"];
[theloginchange updateInBackgroundWithResultBlock:^(BOOL isSuccessful, NSError *error) {
if (isSuccessful) {
NSLog(@"更新成功");
NSLog(@"%@",theloginchange);
}else{
NSLog(@"%@",error);
}
}];
//查找The_login表
BmobQuery *bquery = [BmobQuery queryWithClassName:@"The_login"];
[bquery getObjectInBackgroundWithId:@"030594f03f" block:^(BmobObject *object,NSError *error){
if (error){
NSLog(@"错误");
}else{
if (object) {
NSString *playerName = [object objectForKey:@"MobilePhone"];
NSLog(@"%@",playerName);
}
}
}];
//查找多行数据
BmobQuery *bquery1 = [BmobQuery queryWithClassName:@"The_login"];
[bquery1 findObjectsInBackgroundWithBlock:^(NSArray *array, NSError *error) {
for (BmobObject *obj in array) {
NSLog(@"obj.playerName = %@", [obj objectForKey:@"MobilePhone"]);
NSLog(@"obj.objectId = %@", [obj objectId]);
NSLog(@"obj.createdAt = %@", [obj createdAt]);
NSLog(@"obj.updatedAt = %@", [obj updatedAt]);
}
}];
//批量操作
BmobObjectsBatch *batch = [[BmobObjectsBatch alloc] init] ;
//在The_login表中创建一条数据
[batch saveBmobObjectWithClassName:@"The_login" parameters: @{@"MobilePhone":@18795656653,@"pwd":@"ztj456"}];
//在The_login表中更新
[batch updateBmobObjectWithClassName:@"The_login" objectId:@"4fc51e18ee" parameters:@{@"pwd": @"5632et"}];
//在The_login表中删除
[batch deleteBmobObjectWithClassName:@"The_login" objectId:@"6d4a676902"];
[batch batchObjectsInBackgroundWithResultBlock:^(BOOL isSuccessful, NSError *error) {
NSLog(@"batch error %@",[error description]);
}];
4.在这些基本操作过后开始进入正题登录注册设计
//注册
BmobUser *bUser = [[BmobUser alloc] init];
[bUser setMobilePhoneNumber:@"18798799916"];
[bUser setUsername:@"666"];
[bUser setPassword:@"123456"];
[bUser signUpInBackgroundWithBlock:^ (BOOL isSuccessful, NSError *error){
if (isSuccessful){
NSLog(@"注册成功");
} else {
NSLog(@"%@",error);
}
}];
//登录
[BmobUser loginInbackgroundWithAccount:@"18798799915" andPassword:@"123456" block:^(BmobUser *user, NSError *error) {
if (user){
NSLog(@"登录成功");
} else {
NSLog(@"%@",error);
}
}];
5.这里有一个细节,注册是将数据异步请求到云端,而我们注册时往往是先进行判断,判断注册信息的真实性,所以我采用了bomb云端发送验证码方法来验证真实性
//请求验证码
[BmobSMS requestSMSCodeInBackgroundWithPhoneNumber:@"18798799912" andTemplate:@"注册验证" resultBlock:^(int number, NSError *error) {
if (error) {
NSLog(@"%@",error);
} else {
//获得smsID
NSLog(@"sms ID:%d",number);
}
}];
//匹配验证
[BmobSMS verifySMSCodeInBackgroundWithPhoneNumber:@"18798799912" andSMSCode:self.smsCode resultBlock:^(BOOL isSuccessful, NSError *error) {
if (isSuccessful) {
NSLog(@"%@",@"验证成功");
} else {
NSLog(@"%@",error);
}
}];
6.正则表达式中关于手机号和密码的判断
#pragma mark 手机号码规范
+(BOOL)validateMobile:(NSString *)mobile
{
NSString *Mobile=@"^((13[0-9])|(15[^4,\\D])|(18[0,0-9]))\\d{8}$";
NSPredicate *pri=[NSPredicate predicateWithFormat:@"SELF MATCHES %@",Mobile];
if ([pri evaluateWithObject:mobile]) {
return 1;
}
else
{
return 0;
}
}
#pragma mark 密码规范 判断密码是否包含数字和字母
+(BOOL)validatepassWord:(NSString *)passWord
{
NSString *Mobile=@"[0-9]*(([a-zA-Z]+[0-9]+)|([0-9]+[a-zA-Z]+))+[a-zA-Z]*";
NSPredicate *pri=[NSPredicate predicateWithFormat:@"SELF MATCHES %@",Mobile];
if ([pri evaluateWithObject:passWord]) {
return 1;
}
else
{
return 0;
}
}
7最后给是if else进行循环判断
#pragma mark 注册判断(手机号、密码)
-(void)Registeredjudgment
{
if (![Predicate validateMobile:self.thetextBlock.Phonenumber.text]) {
//当手机号码输出错误时,弹出手机号码输出错误警告
UIAlertController *alertcontroller=[UIAlertController alertControllerWithTitle:@"警告" message:@"手机号输入错误" preferredStyle:UIAlertControllerStyleAlert];
UIAlertAction *sure=[UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDestructive handler:^(UIAlertAction * _Nonnull action) {
self.thetextBlock.Phonenumber.text=nil;
self.thetextBlock.Password.text=nil;
self.thetextBlock.Password1.text=nil;
self.thetextBlock.strength1.alpha=0;
self.thetextBlock.strength2.alpha=0;
self.thetextBlock.strength3.alpha=0;
self.thetextBlock.strengthlabel.text=nil;
}];
[alertcontroller addAction:sure];
[alertcontroller addAction:[UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
}]];
[self presentViewController:alertcontroller animated:YES completion:nil];
}
else
{
//当手机号输入正确后 判断密码是否符合要求
if (![Predicate validatepassWord:self.thetextBlock.Password.text]) {
UIAlertController *alertcontroller=[UIAlertController alertControllerWithTitle:@"提示" message:@"密码必须包含字母和数字" preferredStyle:UIAlertControllerStyleAlert];
UIAlertAction *sure=[UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDestructive handler:^(UIAlertAction * _Nonnull action) {
self.thetextBlock.Password.text=nil;
self.thetextBlock.Password1.text=nil;
self.thetextBlock.strength1.alpha=0;
self.thetextBlock.strength2.alpha=0;
self.thetextBlock.strength3.alpha=0;
self.thetextBlock.strengthlabel.text=nil;
}];
[alertcontroller addAction:sure];
[alertcontroller addAction:[UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
}]];
[self presentViewController:alertcontroller animated:YES completion:nil];
}
else
//当电话号码正确后,判断两次密码是否匹配
if ([self.thetextBlock.Password.text isEqualToString:self.thetextBlock.Password1.text]) {
//当两次密码输入相同时,判断验证码为是否为空
if (self.thetextBlock.Verification.text.length==0) {
UIAlertController *alertcontroller=[UIAlertController alertControllerWithTitle:@"提示" message:@"请输入手机验证码" preferredStyle:UIAlertControllerStyleAlert];
UIAlertAction *sure=[UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDestructive handler:^(UIAlertAction * _Nonnull action) {
}];
[alertcontroller addAction:sure];
[alertcontroller addAction:[UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
}]];
[self presentViewController:alertcontroller animated:YES completion:nil];
}else
{
//注册用户验证
[BmobSMS verifySMSCodeInBackgroundWithPhoneNumber:self.thetextBlock.Phonenumber.text andSMSCode:self.thetextBlock.Verification.text resultBlock:^(BOOL isSuccessful, NSError *error) {
if (isSuccessful) {
//验证成功
//将数据异步请求到云端
BmobUser *bUser = [[BmobUser alloc] init];
[bUser setMobilePhoneNumber:self.thetextBlock.Phonenumber.text];
[bUser setUsername:self.thetextBlock.Phonenumber.text];
[bUser setPassword:self.thetextBlock.Password.text];
[bUser signUpInBackgroundWithBlock:^ (BOOL isSuccessful, NSError *error){
if (isSuccessful){
NSLog(@"数据云端保存完毕");
} else {
NSLog(@"%@",error);
}
}];
//注册成功窗口
UIAlertController*alertcontroller2=[UIAlertController alertControllerWithTitle:@"注册成功"message:nil preferredStyle:UIAlertControllerStyleAlert];
[alertcontroller2 addAction:[UIAlertAction actionWithTitle:@"返回登录页面" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
//调用方法返回登录控制器
TheloginViewController *logon=[[TheloginViewController alloc]init];
logon.mobileP=self.thetextBlock.Phonenumber.text;
[UIApplication sharedApplication].keyWindow.rootViewController=logon;
}]];
[self presentViewController:alertcontroller2 animated:YES completion:nil];
} else {
NSLog(@"验证失败");
}
}
];
}
}
else
{
//当两次密码输入不匹配时
UIAlertController *alertcontroller=[UIAlertController alertControllerWithTitle:@"警告" message:@"两次输入密码不匹配" preferredStyle:UIAlertControllerStyleAlert];
UIAlertAction *sure=[UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDestructive handler:^(UIAlertAction * _Nonnull action) {
self.thetextBlock.Password.text=nil;
self.thetextBlock.Password1.text=nil;
self.thetextBlock.strength1.alpha=0;
self.thetextBlock.strength2.alpha=0;
self.thetextBlock.strength3.alpha=0;
self.thetextBlock.strengthlabel.text=nil;
}];
[alertcontroller addAction:sure];
[alertcontroller addAction:[UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
}]];
[self presentViewController:alertcontroller animated:YES completion:nil];
}
}
}
#pragma mark 获取验证码按钮方法
-(void)button
{
if (self.a==YES) {
//短信发送成功后调用的方法
[self change1];
}else{
//判断手机号是否正确
if ([Predicate validateMobile:self.thetextBlock.Phonenumber.text]) {
//判断密码是否正确
if ([Predicate validatepassWord:self.thetextBlock.Password.text]) {
//判断两次密码是否匹配
if ([self.thetextBlock.Password.text isEqualToString:self.thetextBlock.Password1.text]) {
//发送短信消息
[self change];
}
else{
//非法请求
[self change2];
}
}
else{
[self change2];
}
}
else{
[self change2];
}
}
}
#pragma mark 请求发送验证码
-(void)change
{
//请求发送验证码
[BmobSMS requestSMSCodeInBackgroundWithPhoneNumber:self.thetextBlock.Phonenumber.text andTemplate:@"注册验证" resultBlock:^(int number, NSError *error) {
if (error) {
NSLog(@"%@",error);
} else {
UIAlertController *alertcontroller=[UIAlertController alertControllerWithTitle:@"提示" message:@"发送成功" preferredStyle:UIAlertControllerStyleAlert];
[alertcontroller addAction:[UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
}]];
[self presentViewController:alertcontroller animated:YES completion:nil];
self.a=YES;
}
}];
}
#pragma mark 获取按钮在非法注册时调用方法
-(void)change2
{
UIAlertController *alertcontroller=[UIAlertController alertControllerWithTitle:@"提示" message:@"请输入正确的手机号、密码、和匹配密码!(密码必须包含字母和数字)请不要浪费短信功能(1条4分钱)" preferredStyle:UIAlertControllerStyleAlert];
[alertcontroller addAction:[UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
}]];
[self presentViewController:alertcontroller animated:YES completion:nil];
}
#pragma mark 获取按钮在短息发送成功后非法要求
-(void)change1
{
UIAlertController *alertcontroller=[UIAlertController alertControllerWithTitle:@"提示" message:@"短信发送成功!请不要浪费短信功能(1条4分钱)" preferredStyle:UIAlertControllerStyleAlert];
[alertcontroller addAction:[UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
}]];
[self presentViewController:alertcontroller animated:YES completion:nil];
}