前四步是SMS的官方開發文檔
第一步 獲取短信SDK
點擊下載最新版SDK,解壓後得到以下文件結構:
1、SMS_SDK:短信驗證碼SDK,包括靜態庫和本地化文件。使用時直接將這個文件夾拖入工程。
2、SMS_SDKDemo:示例Demo 。
第二步 導入SDK
第三步 添加依賴庫文件
必要:
libicucore.dylib
libz.dylib
MessageUI.framework
可選:
AddressBook.framework (通訊錄功能需要)
AddressBookUI.framework(通訊錄功能需要)
第四步 添加初始化代碼
1、在appDelegate 添加
2 、在
添加
appKey 和 appSecret的獲取:
(1)到Mob官網註冊成爲Mob開發者;
(2)到應用管理後臺新建應用。
完成上述步驟後
1.在你輸入手機號碼的界面寫一個方法來獲取短信驗證碼
[SMS_SDKgetVerificationCodeBySMSWithPhone:self.phoneNumberzone:@"86"result:^(SMS_SDKError *error) {
if (!error)
{
CaptchaViewController *VC=[[CaptchaViewControlleralloc]init];
VC.phoneNumber=self.phoneNumber;
[self.navigationControllerpushViewController:VCanimated:NO];
}
}];
-(void)nextPage
{ //_captchaStr爲輸入的驗證碼
_captchaStr=[NSStringstringWithFormat:@"%@",self.captchaView.textField.text];
NSDictionary *dic = [HttpTool postWithPath:@"https://api.sms.mob.com/sms/verify" params:@{@"appkey":kSMSAppKey,
@"phone":self.phoneNumber,
@"zone":@"86",
@"code":_captchaStr
}];
if ([dic[@"status"]isEqual:@200])
{
RegistViewController *VC=[RegistViewControllernew];
VC.phoneNumber=self.phoneNumber;
[self.navigationControllerpushViewController:VCanimated:NO];
}
else
{
UIAlertView *alert = [[UIAlertViewalloc]initWithTitle:@"提示"message:@"驗證碼錯誤"delegate:selfcancelButtonTitle:nilotherButtonTitles:@"確認",nil];
[alert show];
self.captchaView.textField.text=nil;
}
}
#import "HttpTool.h"
@implementation HttpTool
+ (NSDictionary *)getWithPath:(NSString *)path params:(NSDictionary *)params
{
NSDictionary *dataDic = [[NSDictionaryalloc]init];
NSMutableString *paramsString = [NSMutableStringstring];
NSArray *allKeys = [params allKeys];
for (NSString *keyin allKeys)
{
[paramsString appendFormat:@"%@=%@&",key,[paramsobjectForKey:key]];
}
NSString *pathString = [path stringByAppendingString:[NSString stringWithFormat:@"?%@",paramsString]];
NSURL *url = [NSURLURLWithString:pathString];
NSURLRequest *request = [[NSURLRequestalloc]initWithURL:urlcachePolicy:NSURLRequestReloadIgnoringCacheDatatimeoutInterval:10];
NSData *data = [NSURLConnectionsendSynchronousRequest:requestreturningResponse:nilerror:nil];
if (data != nil)
{
dataDic = [NSJSONSerializationJSONObjectWithData:dataoptions:kNilOptionserror:nil];
}
return dataDic;
}
+ (NSDictionary *)postWithPath:(NSString *)path params:(NSDictionary *)params
{
NSDictionary *dataDic = [[NSDictionaryalloc]init];
NSURL *url = [NSURLURLWithString:path];
NSMutableURLRequest *request = [[NSMutableURLRequestalloc]initWithURL:urlcachePolicy:NSURLRequestUseProtocolCachePolicytimeoutInterval:10];
[request setHTTPMethod:@"post"];
NSMutableString *paramsString = [NSMutableStringstring];
NSArray *allKeys = [params allKeys];
for (NSString *keyin allKeys)
{
[paramsString appendFormat:@"%@=%@&",key,[paramsobjectForKey:key]];
}
NSData *paramsData = [paramsStringdataUsingEncoding:NSUTF8StringEncoding];
[request setHTTPBody:paramsData];
NSData *data = [NSURLConnectionsendSynchronousRequest:requestreturningResponse:nilerror:nil];
if (data != nil)
{
dataDic = [NSJSONSerializationJSONObjectWithData:dataoptions:kNilOptionserror:nil];
}
return dataDic;
}
@end