#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:@"验证码错误"]; }); }
}];
}