第一步、获取短信SDK
点击下载最新版SDK,解压后得到以下文件结构:
1、SMSSDK:短信验证码SDK,包括静态库和本地化文件。使用时直接将这个文件夹拖入工程。
2、SMSSDKDemo:示例Demo 。
3、如果想要集成SMSSDK 提供的UI,直接把SMSSDKUI.xcodeproj拖到程序。
第二步、导入SDK
将SMSSDK这个文件夹拖入工程。步骤如下:
第三步、添加依赖库文件
必要:
- 列表内容
- 列表内容
- libz.dylib
- libicucore.dylib
- MessageUI.framework
- JavaScriptCore.framework
- libstdc++.dylib
可选:
- AddressBook.framework (通讯录功能需要)
- AddressBookUI.framework(通讯录功能需要)
- SMSSDKAddFramework
第四步、添加初始化代码
#import <SMS_SDK/SMSSDK.h>
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
添加
[SMSSDK registerApp:appKey
withSecret:appSecret];
appKey 和 appSecret的获取:
-
到Mob官网注册成为Mob开发者;
-
到应用管理后台新建应用。新建应用步骤,请参考:网址
第五步、使用SDK提供的API
[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 <SMS_SDK/SMSSDK.h>
@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];
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;
}
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(@"验证码有误");
[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--;
if (self.sumTime >= 0) {
[self.sendVerityBut setTitle:[NSString stringWithFormat:@"%ds",self.sumTime] forState:UIControlStateNormal];
} else {
[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];
UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"Cancel" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
}];
UIAlertAction *okAction = [UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleDestructive handler:^(UIAlertAction * _Nonnull action) {
}];
[self.alertC addAction:cancelAction];
[self.alertC addAction:okAction];
[self presentViewController:self.alertC animated:YES completion:nil];
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
- 60
- 61
- 62
- 63
- 64
- 65
- 66
- 67
- 68
- 69
- 70
- 71
- 72
- 73
- 74
- 75
- 76
- 77
- 78
- 79
- 80
- 81
- 82
- 83
- 84
- 85
- 86
- 87
- 88
- 89
- 90
- 91
- 92
- 93
- 94
- 95
- 96
- 97
- 98
- 99
- 100
- 101
- 102
- 103
- 104
- 105
- 106
- 107
- 108
- 109
- 110
- 111
- 112
- 113
- 114
- 115
- 116
- 117
- 118
- 119
- 120
- 121
- 122
- 123
- 124
- 125
- 126
- 127
- 128
- 129
- 130
- 131
- 132
- 133
- 134
- 135
- 136
- 137
- 138
- 139
- 140
- 141
- 142
- 143
- 144
- 145
- 146
- 147
- 148
- 149
- 150
- 151
- 152
- 153
- 154
- 155
- 156
- 157
- 158
- 159
- 160
- 161
- 162
- 163
- 164
- 165
- 166
- 167
- 168
- 169
- 170
- 171
- 172
- 173
- 174
- 175
- 176
- 177
- 178
- 179
- 180
- 181
- 182
- 183
- 184
- 185
- 186
- 187
- 188
- 189
- 190
- 191
- 192
- 193
- 194
- 195
- 196
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
- 60
- 61
- 62
- 63
- 64
- 65
- 66
- 67
- 68
- 69
- 70
- 71
- 72
- 73
- 74
- 75
- 76
- 77
- 78
- 79
- 80
- 81
- 82
- 83
- 84
- 85
- 86
- 87
- 88
- 89
- 90
- 91
- 92
- 93
- 94
- 95
- 96
- 97
- 98
- 99
- 100
- 101
- 102
- 103
- 104
- 105
- 106
- 107
- 108
- 109
- 110
- 111
- 112
- 113
- 114
- 115
- 116
- 117
- 118
- 119
- 120
- 121
- 122
- 123
- 124
- 125
- 126
- 127
- 128
- 129
- 130
- 131
- 132
- 133
- 134
- 135
- 136
- 137
- 138
- 139
- 140
- 141
- 142
- 143
- 144
- 145
- 146
- 147
- 148
- 149
- 150
- 151
- 152
- 153
- 154
- 155
- 156
- 157
- 158
- 159
- 160
- 161
- 162
- 163
- 164
- 165
- 166
- 167
- 168
- 169
- 170
- 171
- 172
- 173
- 174
- 175
- 176
- 177
- 178
- 179
- 180
- 181
- 182
- 183
- 184
- 185
- 186
- 187
- 188
- 189
- 190
- 191
- 192
- 193
- 194
- 195
- 196