[iOS]iOS开发短信验证码功能

//发送短信验证码URL
NSString *httpSmsAuthCodeUrl = @"http://<ip>:<port>/open/api/sms/authcode/send";
//校验短信验证码URL
NSString *httpVerifySmsAuthCodeUrl = @"http://<ip>:<port>/open/api/sms/authcode/verify";

#pragma mark 获取短信验证码
- (void)sendSmsAuthCode{
    NSLog(@"go");
    //短信验证码URL请求转换为NSURL
    NSURL *url = [NSURL URLWithString: httpSmsAuthCodeUrl];
    //创建可变http请求对象
    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:5.0];
    //POST方式发起
    [request setHTTPMethod:@"POST"];
    //添加Content-type头域
    [request setValue:kHttpHeaderContentTypeValue forHTTPHeaderField:kHttpHeaderContentType];
    //HTTP报体数据字典
    NSDictionary *dict = @{@"apiKey": @"357964ca9d6147a38746798586994a93",
                           @"secretKey": @"a2ef78b6d4e440239b8789856c86e9a5",
                           @"sourceAddr": @"1069060600860007",
                           @"destAddr": @"18867101821",
                           @"expireSeconds": @"300"};
    //字典转换成NSDATA类型
    NSData *data = [NSJSONSerialization dataWithJSONObject:dict options:NSJSONWritingPrettyPrinted error:nil];
    //设置HPPT报体
    [request setHTTPBody:data];
    
    //发起同步请求,超时时间在创建HTTP请求对象时设置
    NSURLResponse *response = nil;
    NSError *error = nil;
    //response包含HTTP请求URL,HTTP应答状态码,HTTP应答头域
    //dataResp包含HTTP应答报体
    NSData *dataResp = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];
    if (error) {
        NSLog(@"error: %@", error);
        return;
    }
    
    //dataResp转成字典,以便获取应答字段的内容
    NSDictionary *dictResp = [NSJSONSerialization JSONObjectWithData:dataResp options:NSJSONReadingAllowFragments error:&error];
    NSMutableString *result = [[NSMutableString alloc]initWithFormat:@"%@: %@", dictResp[@"resultCode"], dictResp[@"resultMsg"]];
    //弹提示框,告知验证结果
    UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"获取短信验证码结果" message:result delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
    [alert show];
    
    return;
}

#pragma mark 校验短信验证码
- (void)verifySmsAuthCode{
    NSString *smsAuthCode = self.smsText.text;
    //校验短信验证码URL请求转换为NSURL
    NSURL *url = [NSURL URLWithString: httpVerifySmsAuthCodeUrl];
    //创建可变http请求对象
    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:5.0];
    //POST方式发起
    [request setHTTPMethod:@"POST"];
    //添加Content-type头域
    [request setValue:kHttpHeaderContentTypeValue forHTTPHeaderField:kHttpHeaderContentType];
    //HTTP报体数据字典
    NSDictionary *dict = @{@"apiKey": @"357964ca9d6147a38746798586994a93",
                           @"destAddr": @"18867101821",
                           @"authCode": smsAuthCode};
    //字典转换成NSDATA类型
    NSData *data = [NSJSONSerialization dataWithJSONObject:dict options:NSJSONWritingPrettyPrinted error:nil];
    //设置HPPT报体
    [request setHTTPBody:data];

    //发起同步请求,超时时间在创建HTTP请求对象时设置
    NSURLResponse *response = nil;
    NSError *error = nil;
    //response包含HTTP请求URL,HTTP应答状态码,HTTP应答头域
    //dataResp包含HTTP应答报体
    NSData *dataResp = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];
    if (error) {
        NSLog(@"error: %@", error);
        return;
    }
    
    //dataResp转成字典,以便获取应答字段的内容
    NSDictionary *dictResp = [NSJSONSerialization JSONObjectWithData:dataResp options:NSJSONReadingAllowFragments error:&error];
    NSMutableString *result = [[NSMutableString alloc]initWithFormat:@"%@: %@", dictResp[@"resultCode"], dictResp[@"resultMsg"]];
    //弹提示框,告知验证结果
    UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"验证结果" message:result delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
    [alert show];
    
    return;
}


中移贯众开放平台短信验证码开发说明:

http://openservice.com.cn/open_devportal/authcode.action


运行效果如下图所示。

点击图标获取验证码。

输入验证码后,点击验证,会得到验证结果。

214219_ZEqP_2392723.png



转载于:https://my.oschina.net/u/2392723/blog/475841

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值