注册功能实现
来源:互联网 发布:蝴蝶效应 知乎 编辑:程序博客网 时间:2024/05/16 03:57
做项目之后发现很多小的东西都是需要认真考虑的,并不像我们想象的那么简单。这次我准备展示一下我的注册界面的获取验证码功能,以及注册功能的实现过程。
第一版:点按钮获取验证码:
(1)给按钮添加事件方法[self.iden addTarget:self action:@selector(getPhoneCode) forControlEvents:UIControlEventTouchUpInside];(2)添加的方法,获取验证码,调用后台的方法 - (void)getPhoneCode{ /** 设置text响应事件 */ [self.user resignFirstResponder]; /** 设置请求后台方法的地址 */ NSString *strURl = USERREGISTER; /** 对地址进行编码,下面主要是格式的设置 */ strURl = [strURl stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]; //设置请求管理队列管理器,使用常规的AFN网络访问,向manager发起请求 AFHTTPRequestOperationManager *mangager_userReg = [AFHTTPRequestOperationManager manager]; /** 返回格式为二进制格式 */ mangager_userReg.responseSerializer = [AFHTTPResponseSerializer serializer]; /** 请求格式为二进制格式 */ mangager_userReg.requestSerializer = [AFHTTPRequestSerializer serializer]; /** 设置请求时间 */ mangager_userReg.requestSerializer.timeoutInterval = 5; /** 设置字典参数 */ NSDictionary *dict = @{@"jbPhone":self.userName}; /** 以post方式提交请求 */ [mangager_userReg POST:strURl parameters:dict constructingBodyWithBlock:^(id<AFMultipartFormData> formData) { } success:^(AFHTTPRequestOperation *operation, id responseObject) { /** 请求成功进行解析 */ NSString *str = [[NSString alloc] initWithData:responseObject encoding:NSUTF8StringEncoding]; // NSLog(@"服务器返回数据:%@", str); /** 解析json过程 */ SBJsonParser *json = [[SBJsonParser alloc] init]; /** 以Json格式接收并且转换成字典表格式 */ NSDictionary *dict = [json objectWithString:str]; DDLogWarn(@"注册验证码 dict = %@", dict); /** 在返回值中取出自己需要的值,并赋给resultCode */ self.resultCode = [dict objectForKey:@"code"]; /** 给小表time添加一个方法,倒计时的方法 */ self.time = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(waitCode:) userInfo:nil repeats:YES]; [self.time fire]; } failure:^(AFHTTPRequestOperation *operation, NSError *error) { //网络失败 MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:self.view animated:YES]; hud.labelText = @"网络开小差啦~"; hud.dimBackground = NO; hud.mode = MBProgressHUDModeText; hud.animationType = MBProgressHUDAnimationFade; [hud hide:YES afterDelay:1.2]; }];}(3)小表倒计时的方法/** 验证码事件变化 */- (void)waitCode:(NSTimer *)timer{ static int count = 60; if (count == 1) { [self.iden setTitle:@"获取验证码" forState:UIControlStateNormal]; self.iden.backgroundColor = [UIColor colorWithRed:1 green:0.54 blue:0.29 alpha:1]; [self.time invalidate]; count = 60; return; } count--; [self.iden setTitle:[NSString stringWithFormat:@"%d秒后重新获取", count] forState:UIControlStateNormal ]; }
这一版的问题很多,包括:按钮随时都能用,不能验证手机号是不是正确等,那么下面有了我们的第二版,在这一般中加入了代理方法,下面来看我们的第二版:
第二版:加入代理方法,进行验证手机号
加入了text代理方法,结束编辑之后,验证电话号是不是正确,添加代理方法:
-(void)textFieldDidEndEditing:(UITextField *)textField{ if (textField == self.user) { NSString *str = textField.text; /** 电话号码正则表达式 */ NSString *regex = @"^((13[0-9])|(147)|(15[^4,\\D])|(18[0,5-9]))\\d{8}$"; NSPredicate *pred = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", regex]; BOOL isMatch = [pred evaluateWithObject:str]; NSLog(@"%@",textField.text); //NSString *mobileNum =self.user.text ; /** 输入完成,验证是否是电话号码 */ if (isMatch) { self.iden.backgroundColor = [UIColor colorWithRed:1 green:0.54 blue:0.29 alpha:1]; [self.iden setEnabled:YES]; } /** 最后一位验证是否符合 */ else if(str.length == 11 && !isMatch) { /** 电话号码不正确 */ self.iden.enabled=false; self.iden.backgroundColor = [UIColor colorWithRed:0.8 green:0.8 blue:0.8 alpha:1]; MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:self.view animated:YES]; hud.labelText = @"请输入正确手机号"; hud.dimBackground = NO; hud.mode = MBProgressHUDModeText; hud.animationType = MBProgressHUDAnimationFade; [hud hide:YES afterDelay:1.2]; } /** 还没有输入完成或是修改的时候按钮不可用 */ else { self.iden.enabled=false; self.iden.backgroundColor = [UIColor colorWithRed:0.8 green:0.8 blue:0.8 alpha:1]; } }else { return; } }
第二版在测试的时候依然出现了问题:没有已注册信息的提示,需要判断;需要获取验证码按钮是填写正确验证码之后自己即可使用,不要事件触发。然后我们在第三版中更换了代理方法,下面是我们的第三版。
第三版注册,改变了text框的代理方法:
/** 再输入的时候进行验证电话号码 */- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string{ if (textField == self.user) { /** 因为已经输入的和最后输入的一个字符需要拼接在一起才是你输入完整的框 */ NSString *str = [textField.text stringByAppendingString:string]; NSString *regex = @"^((13[0-9])|(147)|(15[^4,\\D])|(18[0,5-9]))\\d{8}$"; NSPredicate *pred = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", regex]; BOOL isMatch = [pred evaluateWithObject:str]; NSLog(@"%@",textField.text); //NSString *mobileNum =self.user.text ; /** 输入完成,验证是否是电话号码 */ if (isMatch) { self.iden.backgroundColor = [UIColor colorWithRed:1 green:0.54 blue:0.29 alpha:1]; [self.iden setEnabled:YES]; } /** 最后一位验证是否符合 */ else if(str.length == 11 && !isMatch) { self.iden.enabled=false; self.iden.backgroundColor = [UIColor colorWithRed:0.8 green:0.8 blue:0.8 alpha:1]; MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:self.view animated:YES]; hud.labelText = @"请输入正确手机号"; hud.dimBackground = NO; hud.mode = MBProgressHUDModeText; hud.animationType = MBProgressHUDAnimationFade; [hud hide:YES afterDelay:1.2]; } /** 还没有输入完成或是修改的时候按钮不可用 */ else { self.iden.enabled=false; self.iden.backgroundColor = [UIColor colorWithRed:0.8 green:0.8 blue:0.8 alpha:1]; } } else { return YES; } return YES;}/** 验证手机号 */ /** 解析传回来的值记性解析 */ NSString *str = [[NSString alloc] initWithData:responseObject encoding:NSUTF8StringEncoding]; NSLog(@"返回数据:%@",str); /** 分情况进行判断 */ if ([str isEqualToString:@"{\"result\": true}"]) { MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:[UIApplication sharedApplication].keyWindow animated:YES]; hud.labelText = @"注册成功"; hud.dimBackground = NO; hud.mode = MBProgressHUDModeText; hud.animationType = MBProgressHUDAnimationFade; [hud hide:YES afterDelay:1.2]; [self.navigationController popToRootViewControllerAnimated:YES]; } /** 用户已注册的情况 */ else if([str isEqualToString:@"{\"result\":1}"]) { MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:self.view animated:YES]; hud.labelText = @"用户已经注册"; hud.dimBackground = NO; hud.mode = MBProgressHUDModeText; hud.animationType = MBProgressHUDAnimationFade; [hud hide:YES afterDelay:1.2]; } /** 其他情况 */ else { MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:self.view animated:YES]; hud.labelText = @"注册失败"; hud.dimBackground = NO; hud.mode = MBProgressHUDModeText; hud.animationType = MBProgressHUDAnimationFade; [hud hide:YES afterDelay:1.2]; }
总结:
现在,我们的注册完美了,达到了我们的要求,这个过程不仅仅是这三步,其中还有很多调整的过程。真实需要考虑很多的情况,需要一点儿点儿的去完善,当我们的代码能够应对各种情况的时候,我们的软件就会被人喜欢了,这也是我们的终极目标!
0 0
- 注册功能实现
- HTML实现注册功能
- 注册 功能的实现
- 注册功能的实现
- 注册功能的实现
- ios 实现注册 登录功能
- JavaBean实现注册验证功能
- android之实现注册功能
- 开发第一步:实现注册功能
- Ajax实现用户注册功能
- Servlet注册功能的实现
- Android 实现登录注册功能
- php实现登录注册功能
- 表单实现登录注册功能
- Android 实现登录注册功能
- servlet实现客户注册功能
- 实现用户的注册功能
- django实现登陆注册功能
- Bluetooth SIG profiles
- Android 自定义 DialogFragment 宽度问题
- hihocoder 1233 Boxes(bfs+状态压缩+hash表)
- linux中对文件名包含空格时处理异常的解决方法
- js的时间戳和php的时间戳转换需要注意的地方
- 注册功能实现
- ZOJ.2829 Beautiful Number【水】 2015/09/22
- 友善之臂第一个裸板驱动LED程序的详细分析(新手向)
- 【学习笔记】系列二:Linux安装配置apache
- document.addEventListener理解
- Android 关闭多个Activity
- 函数执行过程的博大精深,表示不懂啊
- Jquery控制文字横向移动
- orcacle 解决违反约束问题