点击下载最新版SDK,解压后得到以下文件结构:
1、SMSSDK:短信验证码SDK,包括静态库和本地化文件。使用时直接将这个文件夹拖入工程。
2、SMSSDKDemo:示例Demo 。
3、如果想要集成SMSSDK 提供的UI,直接把SMSSDKUI.xcodeproj拖到程序。
将SMSSDK这个文件夹拖入工程。步骤如下:
必要:
可选:
#import
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
添加
//初始化应用,appKey和appSecret从后台申请得
[SMSSDK registerApp:appKey
withSecret:appSecret];
appKey 和 appSecret的获取:
到Mob官网注册成为Mob开发者;
到应用管理后台新建应用。新建应用步骤,请参考:网址
[SMSSDK getVerificationCodeByMethod:SMSGetCodeMethodSMS phoneNumber:@"12345678911" zone:@"86" customIdentifier:nil result:^(NSError *error) {
if (!error) {
NSLog(@"获取验证码成功");
} else {
NSLog(@"获取验证码失败%@",error.description);
}
}];
以上转自iOS短信SDK集成文档
以下是一个简单示例,实现给手机发送验证码,并且检测验证码是否正确
#import "secondViewController.h"
#import
@interface secondViewController ()
@property(nonatomic,strong)UITextField *phoneTF;//电话号码
@property(nonatomic,strong)UITextField *verityTF;//验证码
@property(nonatomic,strong)UIButton *sendVerityBut;//发送验证码
@property(nonatomic,strong)UIButton *verityCodeBut;//验证码是否正确
@property(nonatomic,strong)NSTimer *timer;//用来倒计时的
@property(nonatomic,assign)int sumTime;//倒计时用的总时间
@property(nonatomic,strong)UIAlertController *alertC;//提示框
@end
/*
手机号码输入框 发送按钮
验证码输入框 验证按钮
*/
@implementation secondViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
// UIAlertView
//将初始总时间设置为60妙
self.sumTime = 60;
[self.view addSubview:self.phoneTF];
[self.view addSubview:self.verityTF];
[self.view addSubview:self.sendVerityBut];
[self.view addSubview:self.verityCodeBut];
[self.sendVerityBut addTarget:self action:@selector(VerityAction) forControlEvents:UIControlEventTouchUpInside];
[self.verityCodeBut addTarget:self action:@selector(verityCodeAction:) forControlEvents:UIControlEventTouchUpInside];
}
#pragma mark - 发送按钮
- (void)VerityAction {
//判断用户是否输入手机号码 手机号码是否正确
if(self.phoneTF.text.length == 0) {
[self alertActionWithTitle:@"提示" message:@"请输入手机号码"];
return;
}
if (self.phoneTF.text.length != 11) {
NSLog(@"电话号码输入有误");
[self alertActionWithTitle:@"提示" message:@"电话号码输入有误"];
return;
}
//每次将要发送验证码的时候需要将总时间变为60秒
self.sumTime = 60;
//关闭按钮的交互
self.sendVerityBut.enabled = NO;
//打开计时器
[self.timer setFireDate:[NSDate distantPast]];
//说明手机号码正确,可以进行发送验证码的操作
[self sendVerityAction];
}
//发送验证码
- (void)sendVerityAction {
[SMSSDK getVerificationCodeByMethod:SMSGetCodeMethodSMS phoneNumber:self.phoneTF.text zone:@"86" customIdentifier:nil result:^(NSError *error) {
if (error) {
NSLog(@"请检查手机号码是否正确");
[self alertActionWithTitle:@"提示" message:@"请检查手机号码是否正确"];
} else {
NSLog(@"验证码已发送");
[self alertActionWithTitle:@"提示" message:@"验证码已发送"];
}
}];
}
#pragma mark - 检测按钮
//判断验证码是否正确
- (void)verityCodeAction:(UIButton*)sender {
if (self.verityTF.text.length == 0) {
NSLog(@"请输入验证码");
[self alertActionWithTitle:@"提示" message:@"请输入验证码"];
return;
}
//将验证码发送到服务器,判断验证码是否正确
[self checkVerityCode];
}
- (void)checkVerityCode {
[SMSSDK commitVerificationCode:self.verityTF.text phoneNumber:self.phoneTF.text zone:@"86" result:^(NSError *error) {
if (error) {
NSLog(@"验证码有误");
//说明60秒已经结束并且还没有得到验证码,这时候我们应该将定时器暂停或者销毁
[self.sendVerityBut setTitle:@"再次发送" forState:UIControlStateNormal];
//打开用户交互
self.sendVerityBut.enabled = YES;
//销毁定时器
[self.timer invalidate];
self.timer = nil;
} else {
NSLog(@"验证成功");
}
}];
}
#pragma mark - 懒加载
- (UITextField*)phoneTF {
if (!_phoneTF) {
_phoneTF = [[UITextField alloc] initWithFrame:CGRectMake(50, 200, 200, 40)];
_phoneTF.borderStyle = UITextBorderStyleRoundedRect;
_phoneTF.placeholder = @"请输入手机号码";
//需要设置数字键盘
_phoneTF.keyboardType = UIKeyboardTypeNumberPad;
}
return _phoneTF;
}
- (UITextField *)verityTF {
if (!_verityTF) {
_verityTF = [[UITextField alloc] initWithFrame:CGRectMake(50, 280, 200, 40)];
_verityTF.borderStyle = UITextBorderStyleRoundedRect;
_verityTF.placeholder = @"请输入验证码";
}
return _verityTF;
}
- (UIButton*)sendVerityBut {
if (!_sendVerityBut) {
_sendVerityBut = [[UIButton alloc] initWithFrame:CGRectMake(260, 200, 80, 40)];
_sendVerityBut.backgroundColor = [UIColor lightGrayColor];
_sendVerityBut.layer.cornerRadius = 10;
[_sendVerityBut setTitle:@"发送" forState:UIControlStateNormal];
}
return _sendVerityBut;
}
- (UIButton *)verityCodeBut {
if (!_verityCodeBut) {
_verityCodeBut = [[UIButton alloc] initWithFrame:CGRectMake(260, 280, 80, 40)];
_verityCodeBut.backgroundColor = [UIColor lightGrayColor];
_verityCodeBut.layer.cornerRadius = 10;
[_verityCodeBut setTitle:@"检测" forState:UIControlStateNormal];
}
return _verityCodeBut;
}
- (NSTimer*)timer {
if (!_timer) {
_timer = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(timerAction) userInfo:nil repeats:YES];
//暂停
[_timer setFireDate:[NSDate distantFuture]];
}
return _timer;
}
//计时器回调方法
- (void)timerAction {
//把按钮的标题变为倒计时
self.sumTime--;//每次执行倒计时方法,让总时间-1
// NSString *butTitle = @"";//按钮标题
if (self.sumTime >= 0) {
[self.sendVerityBut setTitle:[NSString stringWithFormat:@"%ds",self.sumTime] forState:UIControlStateNormal];
} else {
//说明60秒已经结束并且还没有得到验证码,这时候我们应该将定时器暂停或者销毁
[self.sendVerityBut setTitle:@"再次发送" forState:UIControlStateNormal];
//打开用户交互
self.sendVerityBut.enabled = YES;
//销毁定时器
[self.timer invalidate];
self.timer = nil;
}
}
#pragma mark - 当视图消失
- (void)viewDidDisappear:(BOOL)animated {
self.sumTime = 60;
[self.timer invalidate];
self.timer = nil;
[self.sendVerityBut setTitle:@"发送" forState:UIControlStateNormal];
self.phoneTF.text = nil;
self.verityTF.text = nil;
}
#pragma mark - 提示框
- (void)alertActionWithTitle:(NSString*)title message:(NSString*)message {
self.alertC = [UIAlertController alertControllerWithTitle:title message:message preferredStyle:UIAlertControllerStyleAlert];
//create the actions
UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"Cancel" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
}];
UIAlertAction *okAction = [UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleDestructive handler:^(UIAlertAction * _Nonnull action) {
}];
//Add the actions
[self.alertC addAction:cancelAction];
[self.alertC addAction:okAction];
[self presentViewController:self.alertC animated:YES completion:nil];
}