分享自己在工具类项目开发中接入短信验证码的应用实例,方式是:Objective-C - NSURLSession,以各大云市场的短信接口为例,示例如下:
`
import <Foundation/Foundation.h>
dispatch_semaphore_t sema = dispatch_semaphore_create(0);
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:@"https://vip.veesing.com/smsApi/verifyCode"]
cachePolicy:NSURLRequestUseProtocolCachePolicy
timeoutInterval:10.0];
NSDictionary *headers = @{
@"Content-Type": @"application/x-www-form-urlencoded;charset=utf-8"
};
[request setAllHTTPHeaderFields:headers];
NSMutableData postData = [[NSMutableData alloc] initWithData:[@"appId=41KYR0EB*" dataUsingEncoding:NSUTF8StringEncoding]];
[postData appendData:[@"&appKey=IIWCKKSR7NOQ**" dataUsingEncoding:NSUTF8StringEncoding]];
[postData appendData:[@"&phone=1561894**" dataUsingEncoding:NSUTF8StringEncoding]];
[postData appendData:[@"&templateId=1043" dataUsingEncoding:NSUTF8StringEncoding]];
[postData appendData:[@"&variables=1234" dataUsingEncoding:NSUTF8StringEncoding]];
[request setHTTPBody:postData];
[request setHTTPMethod:@"POST"];
NSURLSession *session = [NSURLSession sharedSession];
NSURLSessionDataTask *dataTask = [session dataTaskWithRequest:request
completionHandler:^(NSData data, NSURLResponse response, NSError *error) {
if (error) {
NSLog(@"%@", error);
} else {
NSHTTPURLResponse *httpResponse = (NSHTTPURLResponse *) response;
NSError *parseError = nil;
NSDictionary *responseDictionary = [NSJSONSerialization JSONObjectWithData:data options:0 error:&parseError];
NSLog(@"%@",responseDictionary);
dispatch_semaphore_signal(sema);
}
}];
[dataTask resume];
dispatch_semaphore_wait(sema, DISPATCH_TIME_FOREVER);
`
项目中加入短信验证码几乎是一个必选项,即使是短信验证码,也需要多加防护哦!