iOS开发之SMSSDK的简单运用

第一步、获取短信SDK

点击下载最新版SDK,解压后得到以下文件结构:

这里写图片描述

1、SMSSDK:短信验证码SDK,包括静态库和本地化文件。使用时直接将这个文件夹拖入工程。

2、SMSSDKDemo:示例Demo 。

3、如果想要集成SMSSDK 提供的UI,直接把SMSSDKUI.xcodeproj拖到程序。

第二步、导入SDK

将SMSSDK这个文件夹拖入工程。步骤如下:

SMS_SKD drag

第三步、添加依赖库文件

必要:

  • 列表内容
  • 列表内容
  • libz.dylib
  • libicucore.dylib
  • MessageUI.framework
  • JavaScriptCore.framework
  • libstdc++.dylib

可选:

  • AddressBook.framework (通讯录功能需要)
  • AddressBookUI.framework(通讯录功能需要)
  • SMSSDKAddFramework

第四步、添加初始化代码

  • 在appDelegate 添加
#import <SMS_SDK/SMSSDK.h>
   
   
  • 1
  • 1
 - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
   
   
  • 1
  • 1
添加
//初始化应用,appKey和appSecret从后台申请得
   [SMSSDK registerApp:appKey
            withSecret:appSecret];
   
   
  • 1
  • 2
  • 3
  • 1
  • 2
  • 3

appKey 和 appSecret的获取:

  1. Mob官网注册成为Mob开发者;

  2. 应用管理后台新建应用。新建应用步骤,请参考:网址

第五步、使用SDK提供的API

    [SMSSDK getVerificationCodeByMethod:SMSGetCodeMethodSMS phoneNumber:@"12345678911" zone:@"86" customIdentifier:nil result:^(NSError *error) {
        if (!error) {
            NSLog(@"获取验证码成功");
        } else {
            NSLog(@"获取验证码失败%@",error.description);
        }
    }];
   
   
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7

以上转自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];

    // 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];
}
   
   
  • 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
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值