IOS 使用shareSDK发送验证码
2015-08-01 11:12
441 查看
[code]// // LoginMobileController.m // kykx // 手机发送验证码 // Created by zhangyg on 15/7/9. // Copyright (c) 2015年 ytint. All rights reserved. // #import "LoginMobileController.h" #import <SMS_SDK/SMS_SDK.h> #import <SMS_SDK/CountryAndAreaCode.h> #import "CommenData.h" #import "DocPackage.h" #import "AFHTTPRequestOperation.h" @interface LoginMobileController () { NSTimer* _timer1; NSTimer* _timer2; NSTimer* _timer3; } @end static int count = 0; @implementation LoginMobileController -(void)viewDidLoad{ [super viewDidLoad]; //将返回按钮的文字设置不在界面显示 [[UIBarButtonItem appearance]setBackButtonTitlePositionAdjustment:UIOffsetMake(NSIntegerMin, NSIntegerMin) forBarMetrics:UIBarMetricsDefault]; [self.navigationController.navigationBar setTintColor:[UIColor whiteColor]]; self.mobile.delegate=self; self.icode.delegate=self; NSLog(@"%@",self.showWho); if([self.showWho isEqualToString:@"1"]){ //修改密码 }else{ //注册 self.navigationItem.title=@"注册"; } //发送验证码 UITapGestureRecognizer *sendcodeGesture=[[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(sendCodeAction)]; self.sendCode.userInteractionEnabled=YES; [self.sendCode addGestureRecognizer:sendcodeGesture]; //下一步 UITapGestureRecognizer *nextGesture=[[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(nextAction)]; self.nextPass.userInteractionEnabled=YES; [self.nextPass addGestureRecognizer:nextGesture]; } -(NSInteger)mobileIsRepeat{ //注册手机号是否重复 NSString *stringurl = [NSString stringWithFormat:@"%@cdpt/api/user_is_exist?access_token=0&user_name=%@", BaseURLString,self.mobile.text]; NSURL *url = [NSURL URLWithString:stringurl]; NSURLRequest *request=[[NSURLRequest alloc]initWithURL:url cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:2]; NSError *error; NSData *data=[NSURLConnection sendSynchronousRequest:request returningResponse:nil error:&error]; if (error) { NSLog(@"error=%@",error); [CommenData showNetError:self.navigationController.view]; }else{ NSDictionary *dict=[NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingAllowFragments error:nil]; NSLog(@"%@",dict); NSNumber *code=[dict code]; if ([code intValue]==200) { if ([[dict infoString] intValue]==0) { //手机号码没有注册 return 1; }else{ //手机号码已经注册 return 0; } }else{ [CommenData showMsgError:self.navigationController.view showMsg:[dict msg]]; } } return -1; } -(void)sendCodeAction{ NSString *regex = @"1[0-9]{10}"; NSPredicate *pred = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", regex]; BOOL isMatch = [pred evaluateWithObject:self.mobile.text]; if (!isMatch){ //手机号码不正确 [CommenData showMsgError:self.navigationController.view showMsg:@"手机号码不正确"]; return; } if([self.showWho isEqualToString:@"1"]){ //修改密码 if ([self mobileIsRepeat]!=0) { if([self mobileIsRepeat]==1){ [CommenData showMsgError:self.navigationController.view showMsg:@"该手机号码没有注册"]; } return; } }else{ //注册 if ([self mobileIsRepeat]!=1) { if([self mobileIsRepeat]==0){ [CommenData showMsgError:self.navigationController.view showMsg:@"该手机号码已经注册"]; } return; } } [SMS_SDK getVerificationCodeBySMSWithPhone:self.mobile.text zone:@"86" result:^(SMS_SDKError *error) { if (!error) { //更新时间 [_timer2 invalidate]; count = 0; NSTimer* timer2=[NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(updateTime) userInfo:nil repeats:YES]; _timer2=timer2; } else { [CommenData showMsgError:self.navigationController.view showMsg:error.errorDescription]; } }]; } -(void)updateTime { count++; if (count>=60) { [_timer2 invalidate]; self.sendCode.text=@"重新获取验证码"; self.sendCode.userInteractionEnabled=YES; return; } //NSLog(@"更新时间"); self.sendCode.text=[NSString stringWithFormat:@"%i秒后可重新发送",60-count]; self.sendCode.userInteractionEnabled=NO; } -(void)nextAction{ //验证号码 if(self.icode.text.length!=4) { [CommenData showMsgError:self.navigationController.view showMsg:@"验证码错误"]; } else { [SMS_SDK commitVerifyCode:self.icode.text result:^(enum SMS_ResponseState state) { if (1==state) { NSLog(@"验证成功"); //下一步 修改密码 UIStoryboard *story=[UIStoryboard storyboardWithName:@"Main" bundle:[NSBundle mainBundle]]; self.loginPasswordController=[story instantiateViewControllerWithIdentifier:@"loginPasswordPage"]; if([self.showWho isEqualToString:@"1"]){ //修改密码 self.loginPasswordController.showWho=@"1"; }else{ //注册 self.loginPasswordController.showWho=@"2"; } self.loginPasswordController.mobile=self.mobile.text; [self.navigationController pushViewController:self.loginPasswordController animated:YES]; } else if(0==state) { NSLog(@"验证失败"); [CommenData showMsgError:self.navigationController.view showMsg:@"验证码失败"]; } }]; } } //点击空白处隐藏键盘 -(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event { [self.mobile resignFirstResponder]; [self.icode resignFirstResponder]; } //改变输入框背景图片 -(void)textFieldDidBeginEditing:(UITextField *)textField{ if (textField.tag==5) { [self.mobile setBackground:[UIImage imageNamed:@"inputlan2.png"]]; [self.icode setBackground:[UIImage imageNamed:@"inputhui2.png"]]; }else if(textField.tag==6){ [self.icode setBackground:[UIImage imageNamed:@"inputlan2.png"]]; [self.mobile setBackground:[UIImage imageNamed:@"inputhui2.png"]]; } } -(void)textFieldDidEndEditing:(UITextField *)textField{ if (textField.tag==5) { [self.mobile setBackground:[UIImage imageNamed:@"inputhui2.png"]]; }else if(textField.tag==6){ [self.icode setBackground:[UIImage imageNamed:@"inputhui2.png"]]; } } @end
相关文章推荐
- IOS 页面跳转
- 简单的跑马灯效果
- iOS自定义cell导致数据加载混乱
- IOS navigation 返回按钮 不显示文字
- 斯坦福大学iOS8公开课笔记03—自动引用计数(ARC)、static、属性观察器、惰性实例化
- iOS开发之遍历Model类的属性并完善使用Runtime给Model类赋值
- ios协议里的属性
- IOS 实现多种分类 添加删除排序功能(LXReorderableCollectionViewFlowLayout)
- 双系统IOS\windows7 换成Windows10后果
- iOS 宏
- 如何用代码开启“个人热点”功能?如何用代码把iOS设备设置成一个无线路由?
- iOS设计模式 - 装饰
- ios 当键盘出现遮挡视图 视图上移Bug
- iOS
- iOS开发 - Quartz2D绘制小黄人
- iOS开发 - Quartz2D绘图
- IOS中延时执行的几种方式的比较和汇总
- ios开发之给Xcode安装颜色主题及自动注释插件
- Foundation框架提供的一些基本对象的用法之数字对象
- 【转】iOS延迟加载