这里使用的SMS_SDK.framework(Mob官网去下载) 来做的短信验证, 操作非常简单, 自己去注册账号拿到appkey 然后就是需要导入一些库文件 需要导入的库有Security.framework Foundation.framework, UIKit.framework, CoreGraphics.framework, libicucore.dylib, libz.lib, AddressBook.framework, AddressBookUI.framework, MessageUI.framework 和 SMS_SDK.framework
//首先来确定一下手机号是否正确以及设置短信的限制时间
if ([_field.text length] == 11) {
[SMS_SDK getVerifyCodeByPhoneNumber:_field.text AndZone:@"86" result:^(enum SMS_GetVerifyCodeResponseState state) {
if (1 == state) {
self.times = 15;
self.timer = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(buttonActionShow) userInfo:nil repeats:YES];
[NSTimer scheduledTimerWithTimeInterval:15 target:self selector:@selector(xianzhi:) userInfo:nil repeats:NO];
} else {
NSLog(@"filed");
}
}];
} else {
UIAlertView *aler = [[UIAlertView alloc] initWithTitle:nil message:@"输入电话号码有误" delegate:self cancelButtonTitle:nil otherButtonTitles:@"确定", nil];
[aler show];
}
// 验证是否正确
[SMS_SDK commitVerifyCode:self.phone.text result:^(enum SMS_ResponseState state) {
if (state == 1) {
UIAlertView *aler = [[UIAlertView alloc] initWithTitle:nil message:@"验证成功" delegate:self cancelButtonTitle:nil otherButtonTitles:@"确定", nil];
[aler show];
} else {
UIAlertView *aler = [[UIAlertView alloc] initWithTitle:nil message:@"输入验证码有误" delegate:self cancelButtonTitle:nil otherButtonTitles:@"确定", nil];
[aler show];
}
}];
// xianzhi的方法 - - 这里写拼音了
- (void)xianzhi:(id)sender
{
[_button setEnabled:YES];
[_timer invalidate];
_timer = nil;
[_button setAlpha:1];
[_button setTitle:@"重新获取" forState:UIControlStateNormal];
}
// 定时器倒数
self.times--;
self.button.enabled = NO;
[self.button setTitle:[NSString stringWithFormat:@"%ld秒",self.times] forState:UIControlStateNormal];