iOS點擊獲取短信驗證碼按鈕


概述

iOS點擊獲取短信驗證碼按鈕, 由於 Demo整體測試運行效果 , 整個修改密碼界面都已展現, 並附送正則表達式及修改密碼邏輯.

詳細

在APP開發中,點擊獲取驗證碼的倒計時按鈕 是在注冊、修改密碼、綁定手機號等場景中使用!在項目中,多次使用這個按鈕,則自定義一個簡單的獲取短信驗證碼倒計時功能方便使用, 大大提高開發效率。

截圖.png

一、主要思路

1、自定義驗證碼按鈕:ZLVerifyCodeButton

2、自定義文本輸入框:ZLTextField

3、正則表達式:手機號及密碼校驗方法

4、修改密碼界面里調用這個短信驗證碼, 調取后台接口,獲取短信驗證碼處理相關其他邏輯.

二、程序實現

Step1. 首先自定義按鈕:ZLVerifyCodeButton

只需要調用方法即可,可以在自定義里按照自己需求去更改按鈕的UI。

@interface ZLVerifyCodeButton : UIButton
// 由於有些時間需求不同,特意露出方法,倒計時時間次數
- (void)timeFailBeginFrom:(NSInteger)timeCount;
@end

自定義按鈕:

- (void)setup {
    
    [self setTitle:@" 獲取驗證碼 " forState:UIControlStateNormal];
    self.titleLabel.font = [UIFont systemFontOfSize:10];
    self.backgroundColor = [UIColor whiteColor];
    self.layer.cornerRadius = 3.0;
    self.clipsToBounds = YES;
    [self setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
    self.layer.borderColor = [UIColor redColor].CGColor;
    self.layer.borderWidth = 1.0;
}

倒計時方法:

- (void)timeFailBeginFrom:(NSInteger)timeCount {
    
    self.count = timeCount;
    self.enabled = NO;
    
    // 加1個計時器
    self.timer = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(timerFired) userInfo:nil repeats:YES];
}

計時器方法:

- (void)timerFired {
    if (self.count != 1) {
        self.count -= 1;
        self.enabled = NO;
        [self setTitle:[NSString stringWithFormat:@"剩余%ld秒", self.count] forState:UIControlStateNormal];
//       [self setTitle:[NSString stringWithFormat:@"剩余%ld秒", self.count] forState:UIControlStateDisabled];
    } else {
        
        self.enabled = YES;
        [self setTitle:@"獲取驗證碼" forState:UIControlStateNormal];
//        self.count = 60;
        
        // 停掉定時器
        [self.timer invalidate];
        self.timer = nil;
    }
}

 

Step2. 自定義文本輸入框:ZLTextField

- (void)setupUI {
    
    // 輸入框
    self.borderStyle = UITextBorderStyleNone;
    [self setAutocapitalizationType:UITextAutocapitalizationTypeNone];
    self.backgroundColor = [UIColor whiteColor]; // ZLColor(0, 0, 0);
    self.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;
    self.returnKeyType = UIReturnKeyNext;
    self.font = [UIFont systemFontOfSize:15];
    self.textColor = ZLColor(113, 111, 118);
    // 設置光標顏色
    self.tintColor =  ZLColor(113, 111, 118);
    // 設置UITextField的占位文字顏色
    self.placeholder = @"設置了占位文字內容以后, 才能設置占位文字的顏色";
    [self setValue: ZLColor(113, 111, 118) forKeyPath:@"_placeholderLabel.textColor"];
    // 添加背景圖片
//    self.background = [UIImage imageNamed:@"u58"];
    // 左間隔
    self.leftView = [[UIView alloc] init];
    self.leftView.width = 15;
    self.leftViewMode = UITextFieldViewModeAlways;
    // clearButton
    self.clearButtonMode = UITextFieldViewModeWhileEditing;
    
    CGFloat marginX = 15;
    // 間隔線
    UIView *line = [[UIView alloc] init];
    line.frame = CGRectMake(marginX, self.height - 0.7, UI_View_Width - marginX * 2, 1);
    line.backgroundColor = ZLColor(249, 249, 249);
    [self addSubview:line];
    
}

 

Step3. 正則表達式:手機號及密碼校驗方法

- (BOOL)match:(NSString *)pattern {
    // 1.創建正則表達式
    NSRegularExpression *regex = [[NSRegularExpression alloc] initWithPattern:pattern options:0 error:nil];
    // 2.測試字符串
    NSArray *results = [regex matchesInString:self options:0 range:NSMakeRange(0, self.length)];
    
    return results.count > 0;
}

 

Step4. 修改密碼界面里調用這個短信驗證碼, 調取后台接口,獲取短信驗證碼處理相關其他邏輯

在你所需要的控制器里調用這個短信驗證碼按鈕即可:

4.1)初始化創建設置相關按鈕屬性

// 獲取驗證碼按鈕
@property (nonatomic, weak) ZLVerifyCodeButton *codeBtn;
    // 獲取驗證碼按鈕
    ZLVerifyCodeButton *codeBtn = [ZLVerifyCodeButton buttonWithType:UIButtonTypeCustom];
    codeBtn.frame = CGRectMake(codeField.width - codeBtnW - marginX, 10, codeBtnW, 30);
    [codeBtn addTarget:self action:@selector(codeBtnVerification) forControlEvents:UIControlEventTouchUpInside];
    [self.codeField addSubview:codeBtn];
    self.codeBtn = codeBtn;

4.2)調取后台接口,獲取短信驗證碼處理相關其他邏輯

// 獲取驗證碼點擊事件
- (void)codeBtnVerification {

    NSLog(@"驗證碼:%@", self.mobileField.text);
    
    [self.codeBtn timeFailBeginFrom:60]; // 倒計時60s
    
    // 調用短信驗證碼接口
    // 用戶輸入的驗證碼數字傳給server,判斷請求結果作不同的邏輯處理,根據自己家的產品大大需求來即可....
//    if (請求成功且匹配成功驗證碼數字){
//        [self.codeBtn timeFailBeginFrom:60];  // 倒計時60s
//    } else {
//        [self.codeBtn timeFailBeginFrom:1]; // 處理請求成功但是匹配不成功的情況,並不需要執行倒計時功能
//    }
}

4.3)調取后台修改密碼接口,處理相關邏輯

// 調用修改密碼接口
- (void)sureBtnPress {
    
    if (![self.mobileField.text isPhoneNumber]) {
        
        [self setupAlertMessage:@"新手機號碼輸入不正確"];
    } else if (![self.codeField.text isEqualToString:self.codeStr]) {

        [self setupAlertMessage:@"驗證碼輸入不正確"];
    } else if(![self.newpswField.text isPSW]) {

        [self setupAlertMessage:@"新密碼不符合要求"];
    } else if ([self.mobileField.text isPhoneNumber] && [self.codeField.text isEqualToString:self.codeStr] && [self.newpswField.text isPSW]) {
        
        // 調用修改密碼接口
        [self changePswForServer];
    }
    
}

// 彈框提示錯誤信息
- (void)setupAlertMessage:(NSString *)message {
    
    UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"提示" message:message preferredStyle:UIAlertControllerStyleAlert];
    UIAlertAction *okAction = [UIAlertAction actionWithTitle:@"確定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        
    }];
    [alert addAction:okAction];
    [self presentViewController:alert animated:YES completion:nil];
}

三、壓縮文件截圖及運行效果

1、壓縮文件截圖

A8E5039C-7F7A-4C80-A427-C08AC97E3D8F.png

2、項目文件截圖

DFE113F6-6F16-419A-8642-D14E75CE24D1.png

3、運行時的效果圖

運行效果.gif

四、其他補充

界面性問題可以根據自己項目需求調整即可, 具體可參考代碼, 項目能夠直接運行!

由於 Demo整體測試運行效果 , 整個修改密碼界面都已展現, 並附送正則表達式及修改密碼邏輯.

 

注:本文著作權歸作者,由demo大師發表,拒絕轉載,轉載需要作者授權


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM