iOS使用ShareSDK 短信验证

1.注册,登录ShareSDK;

2.下载ShareSDk,并根据文档提示,集成到自己的项目中;(在xcode7之前类库后缀是dylib的,xcode7.1后改为了tdb)

3.进入后台,在短信验证里创建自己的应用,获取Appkey和AppSecret;

4.在Appdeligate中,注册短信验证;

[SMSSDK registerApp:AppKey withSecret:AppSecret];

 

5.点击获取验证码的按钮中,

  

//判断手机号的正则表达式

    NSString *regexPhoneNum = @"^1[3|4|5|7|8][0-9]\\d{8}$";

    NSPredicate *predPhoneNum = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", regexPhoneNum];

    

    BOOL isMatchPhoneNum = [predPhoneNum evaluateWithObject:self.PhoneNumber.text];

   //判断密码规则的正则表达式(8-20位字母和数字的组合)

        NSString *regexPassword = @"^(?![0-9]+$)(?![a-zA-Z]+$)[0-9A-Za-z]{8,20}$";

        NSPredicate *predPassword = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", regexPassword];

    

        BOOL isMatchPassword = [predPassword evaluateWithObject:self.Secret.text];

    

    if (!isMatchPhoneNum){

        //手机号码不匹配

        UIAlertView *alertPhoneNum=[[UIAlertView alloc] initWithTitle:@"柚学霸提示您" message:@"您输入的号码有误" delegate:self cancelButtonTitle:nil otherButtonTitles:@"确认", nil];

        [alertPhoneNum show];

        

   }else {

        //手机号匹配

       

       /*

        判断密码是否为空

        

        */

 

       if (_Secret.text==nil || _ConfireSecret.text==nil) {

         //密码或者确认密码为空

           

           UIAlertView *alertSecretNil=[[UIAlertView alloc] initWithTitle:@"柚学霸提示" message:@"密码不能为空" delegate:self cancelButtonTitle:@"知道了" otherButtonTitles:nil, nil];

           [alertSecretNil show];

           

       }else{

        //密码和确认密码不为空

           //判断密码是否符合要求

           

           if (isMatchPassword) {

             //密码符合要求

               

               //密码和确认密码是否一致

               if ([_Secret.text isEqualToString:_ConfireSecret.text]) {

                   //两次密码输入一致

                   //发送验证码

                   

                   //请求验证码

                   [SMSSDK getVerificationCodeByMethod:SMSGetCodeMethodSMS phoneNumber:self.PhoneNumber.text

                                                                         zone:@"86"

                                                             customIdentifier:nil

                                                                       result:^(NSError *error)

                                           {

                   

                                               if (!error)

                                               {

                                                   NSLog(@"验证码发送成功");

                                                   UIAlertView *alert=[[UIAlertView alloc] initWithTitle:@"柚学霸提示" message:@"验证码已发送,请注意查收" delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];

                                                   [alert show];

                   

                                               }

                                               else

                                               {

                                                   UIAlertView* alert = [[UIAlertView alloc] initWithTitle:NSLocalizedString(@"codesenderrtitle", nil)

                                                                                                   message:[NSString stringWithFormat:@"错误描述:%@",[error.userInfo objectForKey:@"getVerificationCode"]]

                                                                                                  delegate:self

                                                                                         cancelButtonTitle:NSLocalizedString(@"sure", nil)

                                                                                         otherButtonTitles:nil, nil];

                                                   [alert show];

                                               }

                                               

                                           }];

                   

               }else{

                   //两次密码输入不一致

                   

                   UIAlertView *alertSecretDifferent=[[UIAlertView alloc] initWithTitle:@"柚学霸提示" message:@"密码不一致,请重新输入" delegate:self cancelButtonTitle:@"知道了" otherButtonTitles:nil, nil];

                   [alertSecretDifferent show];

   

               }

  

               

           }else{

           //密码不符合要求

           

               UIAlertView *alertSecretWrong=[[UIAlertView alloc] initWithTitle:@"柚学霸提示" message:@"密码错误,请输入8-20位字母和数字的组合" delegate:self cancelButtonTitle:@"知道了" otherButtonTitles:nil, nil];

               [alertSecretWrong show];

           

           }

 

       }

 

   }

判断是否验证成功

- (IBAction)abideByAgreement:(id)sender

{

   // [self loadRegisterData];

    

    //判断是否验证成功

    [SMSSDK  commitVerificationCode:self.authCodeTF.text

     //传获取到的区号

                        phoneNumber:self.phoneNumTF.text

                               zone:@"86"

                             result:^(NSError *error)

     {

         

         if (!error)

         {

             NSLog(@"验证成功");

             

             LoginViewController *loginVC=[[LoginViewController alloc] initWithNibName:@"LoginViewController" bundle:nil];

             [self.navigationController pushViewController:loginVC animated:YES];

 

         }

         else

         {

             UIAlertView *alertSecretWrong=[[UIAlertView alloc] initWithTitle:@"柚霸提示" message:@"您输入的验证码有误" delegate:self cancelButtonTitle:@"知道了" otherButtonTitles:nil, nil];

             [alertSecretWrong show];

             NSLog(@"验证失败");    

         }    

     }];   

}

 

 

6.运行出来的效果图

 

posted @ 2015-10-30 14:02  脉脉不得语  阅读(634)  评论(1编辑  收藏  举报