Objective-C对接营销短信API接口代码示例

#import <Foundation/Foundation.h>

int main(int argc, const char * argv[]) {
    @autoreleasepool {
        // 定义请求的基础信息
        NSString *host = @"api.ihuyi.com";
        NSString *path = @"/sms-yx/v1/batchSend";
        NSString *urlString = [NSString stringWithFormat:@"http://%@%@", host, path];
        NSURL *url = [NSURL URLWithString:urlString];

        // 定义请求的数据
        NSDictionary *values = @{
            @"api_id": @"sms-yx-xxxxxxxx", //APIID(用户中心【文本短信】-【会员营销短信】-【产品总览】查看)
            @"signature": @"xxxxxxxxx", //请求验证加密签名(非短信签名);签名生成方式:仅公共参数以ASCII码从小到大排序值,key=value,多值以“&”隔开,拼接之后md5 32位小写; 如:md5(api_id=xxxx&api_key=xxxx&request_id=xxxxxxxx&timestamp=xxxxxxx)APIKEY(用户中心【文本短信】-【会员营销短信】-【产品总览】查看)2、动态密码(生成动态密码方式请看该文档末尾的说明)
            @"timestamp": @1623643787, //东八时区;10位时间戳,时间允许相差±60S
            @"request_id": @"xxxxxxxxxxxx", //请求方请求ID,建议使用唯一ID,比如使用uuid;我方系统会2小时内去重验证处理,防止网络重复攻击;
            @"product_id": @1001, //产品ID
            @"phone": @[@"18800000000", @"18800000001"], //手机号数组(最多1万个号码)
            @"sign_name": @"xxxxxxxx", //短信签名(template_id未填写则必填)
            @"content": @"尊敬的会员您好:${name},您的订单号是:${order_no},拒收请回复R", //template_id为空时必填;短信内容,如:您的短信群发功能已开通,请在3个工作日之内至平台进行企业认证! 短信内容和模板ID必须传入1个;当短信内容和模板ID都传入时,传入内容生效,模板ID属性失效;
            @"template_id": @1, //模板ID(内容为空则必填)
            @"template_var": @[@{@"${name}":@"张三"},@{@"${order_no}":@"202009041156181103"}], //选择模板时,且模板是变量模板时,可以传入变量值,需要传入json格式;
            @"send_time": @"2020-08-26 16:08:14", //定时发送时间
        };
        
        // 将数据转换为JSON数据
        NSError *error = nil;
        NSData *jsonData = [NSJSONSerialization dataWithJSONObject:values 
                                                           options:0 
                                                             error:&error];
        
        if (error) {
            NSLog(@"JSON序列化错误: %@", error.localizedDescription);
            return 1;
        }

        // 创建请求对象
        NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url
                                                      cachePolicy:NSURLRequestReloadIgnoringCacheData
                                                  timeoutInterval:30.0];

        // 设置请求方法和头部
        [request setHTTPMethod:@"POST"];
        [request setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];

        // 设置请求体
        [request setHTTPBody:jsonData];

        // 创建会话和任务
        NSURLSession *session = [NSURLSession sharedSession];
        NSURLSessionDataTask *task = [session dataTaskWithRequest:request
                                        completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {
                                            if (error) {
                                                NSLog(@"请求错误: %@", error.localizedDescription);
                                                return;
                                            }
                                            
                                            // 处理响应
                                            NSHTTPURLResponse *httpResponse = (NSHTTPURLResponse *)response;
                                            NSLog(@"状态码: %ld", (long)httpResponse.statusCode);
                                            
                                            if (data) {
                                                NSString *responseString = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
                                                NSLog(@"响应内容: %@", responseString);
                                            }
                                        }];

        [task resume];

        // 保持程序运行直到请求完成(仅用于测试)
        [[NSRunLoop currentRunLoop] runUntilDate:[NSDate dateWithTimeIntervalSinceNow:5.0]];
    }
    return 0;
}