导入第三方
配置info.plist文件

添加依赖库

创建提示框
-(void)showMBAlertWithMessage:(NSString*)msg;
// 显示提示框
-(void)showMBAlertWithMessage:(NSString*)msg {
MBProgressHUD *hud = [[MBProgressHUD alloc] initWithView:self.view];
hud.mode = MBProgressHUDModeText; // 设置文本样式
hud.labelText= msg; // 设置显示的提示文本
hud.removeFromSuperViewOnHide = YES;
[self.view addSubview:hud];
[hud show:YES];
[hud hide:YES afterDelay:3.0];
}
创建类别
/// 判断是否是一个正确的手机号
-(BOOL)isCorrectPhoneNumber;
/// 判断是否是一个正确的手机号
-(BOOL)isCorrectPhoneNumber{
if(self.length==0||self.length!=11) {
returnNO;
}
if([self hasPrefix:@"13"] || [self hasPrefix:@"15"] || [self hasPrefix:@"17"] || [self hasPrefix:@"18"]) {
returnYES;
}
return NO;
}
-(void)abc{
// 先判断手机号输入为空
if(self.phoneTF.text.length==0) {
[self showMBAlertWithMessage:@"手机号不可为空"];
return;
}
// 输入错误的手机号
if (![self.phoneTF.text isCorrectPhoneNumber]) {
[self showMBAlertWithMessage:@"请输入正确的手机号"];
return;
}
// 将“获取验证码按钮”文本改变,并开启一个定时器
[self.btn setTitle:@"30" forState:UIControlStateNormal];
__blockintseconds =29; // 剩余时间的秒数
self.timer = [NSTimer scheduledTimerWithTimeInterval:1.0 repeats:YES block:^(NSTimer * _Nonnull timer) {
//
if(seconds ==0) {
// 停止定时器
[timer invalidate];
timer =nil;
// 改变按钮的标题
dispatch_async(dispatch_get_main_queue(), ^{
[self.btn setTitle:@"重新获取"forState:UIControlStateNormal];
});
}
else{
// 改变按钮的标题
dispatch_async(dispatch_get_main_queue(), ^{
NSString*timeStr = [NSString stringWithFormat:@"%02d",seconds--];
[self.btn setTitle:timeStr forState:UIControlStateNormal];
});
}
}];
// 如果输入的手机号格式正确,调用接口
//不带自定义模版
[SMSSDK getVerificationCodeByMethod:SMSGetCodeMethodSMS phoneNumber:self.phoneTF.text zone:@"86" result:^(NSError *error) {
if(!error)
{
// 请求成功
}
else
{
// error
dispatch_async(dispatch_get_main_queue(), ^{
[self showMBAlertWithMessage:@"获取验证码失败"];
});
}
}];
}
-(void)wo{
// 手机号登录
// 如果手机号或者验证码输入为空,提示
if(self.phoneTF.text.length==0||self.two.text.length==0) {
[self showMBAlertWithMessage:@"手机号及验证码不可为空"];
return;
}
// 调用提交验证码的接口
[SMSSDK commitVerificationCode:self.two.text phoneNumber:self.phoneTF.text zone:@"86" result:^(NSError *error) {
if(!error)
{
// 验证成功
NSLog(@"成功");
}
else
{
// error
dispatch_async(dispatch_get_main_queue(), ^{
[self showMBAlertWithMessage:@"短信验证码错误!"];
});
}
}];
}
网友评论