ITPub博客

首页 > IT职业 > IT职场 > 大型项目Objective-C - NSURLSession接入短信验证码应用实例分享

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

原创 IT职场 作者:中昱维信 时间:2020-11-09 10:44:32 0 删除 编辑

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


来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/31548553/viewspace-2732921/ ,如需转载,请注明出处,否则将追究法律责任。

请登录后发表评论 登录
全部评论
打工人,打工就是人上人:www.veesing.com/

注册时间:2018-08-03

  • 博文量
    22
  • 访问量
    25452