IOS 使用shareSDK发送验证码

来源:互联网 发布:土木工程软件 编辑:程序博客网 时间:2024/06/02 03:40
////  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;}@endstatic 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
0 0