IOS 广州首易短信验证码 post 请求及验证码button倒计时

广州首易短信验证码

参数说明:

@required

CorpID:企业ID

LoginName:登录用户名

send_no:电话号码

msg:发送的消息

@optional

Passwd:账号密码   

-(void)sendPhoneMessage:(NSString *)phoneNumber{
    NSString * URLString = @"http://sms3.mobset.com/SDK/Sms_Send.asp?";
    NSURL * URL = [NSURL URLWithString:[URLString stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
    
    NSString * postString = [NSString stringWithFormat:@"CorpID=%@&LoginName=%@&Passwd=%@&send_no=%@&msg=%@",@"",@"",@"",phoneNumber,@"短信验证码:87289"];
    NSData * postData = [self gb2312Encoding:postString];  //sms3.mobset.com 采用GB2312的编码方式  注意:用utf-8中文会乱码

    NSMutableURLRequest *URLRequest = [[NSMutableURLRequest alloc] initWithURL:URL cachePolicy:NSURLRequestReloadIgnoringLocalCacheData timeoutInterval:10.0];
    [URLRequest setHTTPMethod:@"POST"];
    [URLRequest setHTTPBody: postData];
    
    NSURLConnection *connection = [[NSURLConnection alloc] initWithRequest:URLRequest delegate:self] ;
    [connection start];
    //如果连接已经建好,则初始化data
    if(connection){
        receiveData = [NSMutableData data];
    }else{
        NSLog(@"theConnection is NULL");
    }
}

#pragma mark - GB2312编码
- (NSData*)gb2312Encoding:(NSString*)string
{
    NSStringEncoding encoding =CFStringConvertEncodingToNSStringEncoding(kCFStringEncodingGB_18030_2000);
    return [string dataUsingEncoding:encoding];
}

#pragma mark - NSURLConnection Delegate
//接收respone,里面包含了HTTP的各种信息
-(void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
{
    [receiveData setLength: 0];
}
//接受到数据时调用,长的数据会被拆分成多个片段发送回来,需要拼接
-(void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
{
    [receiveData appendData:data];
}
//数据接受结束时调用,处理完整数据
- (void)connectionDidFinishLoading:(NSURLConnection *)connection
{
    if (receiveData) {
        NSString *string = [[NSString alloc] initWithData:receiveData encoding:NSUTF8StringEncoding];
        NSLog(@"data:%@",string);
    }
}
//请求出错时调用
-(void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error
{
    NSLog(@"ERROR with theConenction");
}

点击发送短信验证码后 要让button休息一段时间 防止连续点击
这时候就用到了倒计时功能

倒计时有两种方式可以实现
1.使用NSTimer
- (IBAction)sendSMSBtnClick:(id)sender{
     secondsCountDown = 60;//60秒倒计时
     countDownTimer = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(countDown) userInfo:nil repeats:YES];
}


-(void)countDown{
    secondsCountDown--;
    if(secondsCountDown <= 0){
        [countDownTimer invalidate];
        [button setTitle:@"发送验证码" forState:UIControlStateNormal];
        button.enabled = YES;
    }else{
        [button setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
        [button setTitle:[NSString stringWithFormat:@"%d秒后重新发送",secondsCountDown] forState:UIControlStateNormal];
        self.enabled = NO;
    }
}

2.使用GCD
- (IBAction)sendSMSBtnClick:(id)sender{
   [self countDown];
}


#pragma mark - 时间倒计时
-(void)countDown{
    __block int secondsCountDown = 60;
    dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
    dispatch_source_t sourceTimer = dispatch_source_create(DISPATCH_SOURCE_TYPE_TIMER, 0, 0, queue);
    dispatch_source_set_timer(sourceTimer,dispatch_walltime(NULL, 0), NSEC_PER_SEC, 0);
    dispatch_source_set_event_handler(sourceTimer, ^{
        if(secondsCountDown <= 0){
            dispatch_source_cancel(sourceTimer);
            dispatch_async(dispatch_get_main_queue(), ^{
                [button setTitle:@"发送验证码" forState:UIControlStateNormal];
                button.enabled = YES;
            });
        }else{
            secondsCountDown--;
            dispatch_async(dispatch_get_main_queue(), ^{
                [button setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
                [button setTitle:[NSString stringWithFormat:@"%d秒后重新发送",secondsCountDown] forState:UIControlStateNormal];
                button.enabled = NO;
            });
        }
    });
    dispatch_resume(sourceTimer);
}<span style="font-family:HannotateSC-W5;FONT-SIZE: 13px">

</span>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值