判断银行卡-识别银行卡所在银行

  • 获取bank.infoPlist
  • 根据银行卡号的前6位,或者前8位进行识别

    - (NSString *)returnBankName:(NSString*) idCard{
        
        if(idCard==nil || idCard.length<16 || idCard.length>19){
            _resultLabel.text = @"卡号不合法";
            return @"";
            
        }
        NSString *plistPath = [[NSBundle mainBundle] pathForResource:@"bank" ofType:@"plist"];
        NSDictionary* resultDic = [NSDictionary dictionaryWithContentsOfFile:plistPath];
        NSArray *bankBin = resultDic.allKeys;
        
        //6位Bin号
        NSString* cardbin_6 = [idCard substringWithRange:NSMakeRange(0, 6)];
        //8位Bin号
        NSString* cardbin_8 = [idCard substringWithRange:NSMakeRange(0, 8)];
        if ([bankBin containsObject:cardbin_6]) {
            return [resultDic objectForKey:cardbin_6];
        }else if ([bankBin containsObject:cardbin_8]){
            return [resultDic objectForKey:cardbin_8];
        }else{
            _resultLabel.text = @"plist文件中不存在请自行添加对应卡种";
            return @"";
        }
        return @"";
        
    }
    复制代码

  • 在输入的过程中控制4位1控制,使用uitext的代理:

    -(BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string{
            NSString *text = [self.textField text];
            NSCharacterSet *characterSet = [NSCharacterSet characterSetWithCharactersInString:@"0123456789\b"];
            string = [string stringByReplacingOccurrencesOfString:@" " withString:@""];
            
            if ([string rangeOfCharacterFromSet:[characterSet invertedSet]].location != NSNotFound) {
                return NO;
            }
            
            text = [text stringByReplacingCharactersInRange:range withString:string];
            text = [text stringByReplacingOccurrencesOfString:@" " withString:@""];
            
            NSString *newString = @"";
            while (text.length > 0) {
                
                NSString *subString = [text substringToIndex:MIN(text.length, 4)];
                newString = [newString stringByAppendingString:subString];
                if (subString.length == 4) {
                    newString = [newString stringByAppendingString:@" "];
                }
                text = [text substringFromIndex:MIN(text.length, 4)];
            }
            
            newString = [newString stringByTrimmingCharactersInSet:[characterSet invertedSet]];
            
            // 限制长度
            if (newString.length >= 24) {
                return NO;
            }
            
            [self.textField setText:newString];
            
            return NO;
    }
    复制代码

转载于:https://juejin.im/post/5b8f78ffe51d450e935c071f

你可能感兴趣的:(判断银行卡-识别银行卡所在银行)