广州首易短信验证码
参数说明:
@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");
}
这时候就用到了倒计时功能
倒计时有两种方式可以实现
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>