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>
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章