总共分六步:
当然在所有步骤之前你先要去shareSDK网站下载关于发送短信验证码的包,拖到工程里,然后就可以开始了
一.导入库
二.
#import "AppDelegate.h"
#pragma mark - 1.导入
#import <SMS_SDK/SMSSDK.h>
这里写在AppDelegate里是为了进程序就注册,以免后面忘记了.
三.
在
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
里面写
#pragma mark - 2.注册需要验证短信功能的应用
//参数为注册应用给的码
[SMSSDK registerApp:@"cc19e25ba266" withSecret:@"239de32606b97ec343fc3e83121fae37"];
四.在ViewController里导入头文件
#import "ViewController.h"
#pragma mark - 3.导入
#import <SMS_SDK/SMSSDK.h>
ps:上边的引入第三方类库为啥用尖括号呢,我也不知道为啥,用双引号没有索引,生敲也行,亲测可用!也希望知道原因的回复下,涨涨姿势哈!
五.发送
#pragma mark - 4.发送验证码
- (void)sendAction
{
[SMSSDK getVerificationCodeByMethod:SMSGetCodeMethodSMS phoneNumber:@"接收短信的电话号码" zone:@"86" customIdentifier:nil result:^(NSError *error) {
if (!error) {
NSLog(@"获取验证码成功");//弹出个alert之类的
}else{
NSLog(@"验证码获取失败");
}
}];
}
六.验证
#pragma mark - 5.验证用户输入的验证码与发送到用户手机的验证码是否匹配
- (void)confirmAction
{
[SMSSDK commitVerificationCode:_receivedField.text phoneNumber:@"手机收到的验证码" zone:@"86" result:^(NSError *error) {
if (!error) {
NSLog(@"验证成功");//进行其他操作
}else{
NSLog(@"验证失败");
}
}];
}