Mob 短信验证

#import “LoginViewController.h”

@interface LoginViewController ()
{
int _leftSeconds; // 剩余的秒数
}
@property (weak, nonatomic) IBOutlet UITextField *phoneTF;
@property (weak, nonatomic) IBOutlet UITextField *codeTF;
@property (weak, nonatomic) IBOutlet UIButton *codeGetBtn;

  • (IBAction)getSMSCode:(id)sender;
  • (IBAction)login:(id)sender;
  • (IBAction)weChatLogin:(id)sender;

MOBSSk SDK

  • (IBAction)getSMSCode:(id)sender {
    // 如果用户没有输入手机号,错误提示
    if (self.phoneTF.text.length == 0) {
    [self.view showMBAlertWithMessage:@“手机号不可为空”];
    return;
    }

    // 判断是否是正确的手机号,不是给错误提示
    if (![self.phoneTF.text isCorrectPhoneNumber]) {
    [self.view showMBAlertWithMessage:@“请输入正确的手机号”];
    return;
    }

    // 将倒计时设置为60秒
    _leftSeconds = 30;
    // 改变button的标题
    [self.codeGetBtn setTitle:@“30” forState:UIControlStateNormal];

    // 改变按钮的UI,做成倒计时
    [NSTimer scheduledTimerWithTimeInterval:1.0 repeats:YES block:^(NSTimer * _Nonnull timer) {
    // 如果剩余秒数不为0,每隔一秒钟显示数字减1
    if (self->_leftSeconds != 0) {

          NSString *title = [NSString stringWithFormat:@"%02d",--self->_leftSeconds];
          
          [self.codeGetBtn setTitle:title forState:UIControlStateNormal];
      }
      // 如果为0
      else {
          [self.codeGetBtn setTitle:@"重新获取" forState:UIControlStateNormal];
          [timer invalidate];
          timer = nil;
      }
    

    }];

    // 请求接口,获取短信验证码
    //不带自定义模版
    [SMSSDK getVerificationCodeByMethod:SMSGetCodeMethodSMS phoneNumber:self.phoneTF.text zone:@“86” result:^(NSError *error) {

      if (!error)
      {
          // 请求成功
      }
      else
      {
          // error
          dispatch_async(dispatch_get_main_queue(), ^{
              [self.view showMBAlertWithMessage:@"获取短信验证码失败"];
          });
      }
    

    }];

}

  • (IBAction)login:(id)sender {
    // 如果手机号与验证码为空,给出错误提示
    if (self.phoneTF.text.length == 0 || self.codeTF.text.length == 0) {
    [self.view showMBAlertWithMessage:@“请输入手机号和验证码”];
    return;
    }

    // 验证手机号与验证码是否正确
    [SMSSDK commitVerificationCode:self.codeTF.text phoneNumber:self.phoneTF.text zone:@“86” result:^(NSError *error) {

      if (!error)
      {
          // 验证成功
          AppDelegate *appDele = (AppDelegate *)[UIApplication sharedApplication].delegate;
          // 切换窗口的根视图控制器
          appDele.window.rootViewController = appDele.tabBarCtl;
          
      }
      else
      {
          // error
          dispatch_async(dispatch_get_main_queue(), ^{
              [self.view showMBAlertWithMessage:@"验证码错误"];
          });
      }
    

    }];
    }

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值