1.准备工作
去www.mob.com官网注册,并创建一个应用,获得appKey和app_secret,未通过审核的应用每天有20条免费短信发送
浏览下官方文档
http://wiki.mob.com/%e7%9f%ad%e4%bf%a1sdk%e6%97%a0ui%e9%9b%86%e6%88%90/
下载最新SDK
http://www.mob.com/#/downloadDetail/SMS/ios
2.解压把SMS_SDK整个目录拖进工程中
3.编译报错,修改路径
4.编译23个错误
5.导入依赖库,编译成功
6.工程代码
AppDelegate.m
#import "AppDelegate.h"
#import <SMS_SDK/SMSSDK.h>
//SMSSDK申请的key
#define appkey @"官网后台申请的appKey"
#define app_secrect @"官网后台申请的app_secrect"
@interface AppDelegate ()
@end
@implementation AppDelegate
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
[SMSSDK registerApp:appkey
withSecret:app_secrect];
return YES;
}
ViewController.m
#import "ViewController.h"
#import <SMS_SDK/SMSSDK.h>
@interface ViewController ()
@property (weak, nonatomic) IBOutlet UITextField *iphone;
@property (weak, nonatomic) IBOutlet UITextField *verifyCodeField;
- (IBAction)checkCode:(id)sender;
- (IBAction)getCode:(id)sender;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
}
//点击按钮获取验证码
- (IBAction)getCode:(id)sender {
[SMSSDK getVerificationCodeByMethod:SMSGetCodeMethodSMS phoneNumber:self.iphone.text zone:@"86" customIdentifier:nil result:^(NSError *error) {
if (!error) {
NSLog(@"获取验证码成功");
} else {
NSLog(@"错误信息:%@",error);
};
}];
}
//点击提交按钮,检验验证码是否正确
- (IBAction)checkCode:(id)sender {
[SMSSDK commitVerificationCode:self.verifyCodeField.text phoneNumber:self.iphone.text zone:@"86" result:^(NSError *error) {
if (!error) {
NSLog(@"验证成功");
}
else
{
NSLog(@"错误信息:%@",error);
}
}];
}
@end
ViewController设计如下
最后手机成功获得验证码,校验也成功。
遇到的问题:
如果是ios9,需要适配ios9的。