大型项目Objective-C – NSURLSession接入短信验证码应用实例分享

微信扫一扫,分享到朋友圈

大型项目Objective-C – NSURLSession接入短信验证码应用实例分享

分享自己在工具类项目开发中接入短信验证码的应用实例,方式是: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);

`

项目中加入短信验证码几乎是一个必选项,即使是短信验证码,也需要多加防护哦!

Objective-C – NSURLSession.m和文档说明下载

微信扫一扫,分享到朋友圈

大型项目Objective-C – NSURLSession接入短信验证码应用实例分享

为什么阿里巴巴不建议MySQL使用Text类型?

上一篇

TensorFlow Ranking 框架在海外推荐业务中的实践与应用 在爱奇艺海外推荐业务,引入TensorFlow Ra...

下一篇

你也可能喜欢

大型项目Objective-C – NSURLSession接入短信验证码应用实例分享

长按储存图像,分享给朋友