1、CardIO 识别
框架 GitHub 下载地址
-
配置
-
1、把框架整个拉进自己的工程,然后在 TARGETS => Build Phases => Link Binary With Libraries 里边分别加入下面这几个框架。
Accelerate.framework MobileCoreServices.framework CoreMedia.framework AudioToolbox.framework AVFoundation.framework
-
2、在TARGETS => Build Settings => Other Linker Flags 中添加 -ObjC 和 -lc++ 。
-
3、在 iOS8 + 系统中使用相机需要在 Info.plist 中添加 Privacy - Camera Usage Description,并设置其值。
-
4、在我们需要调用的文件中导入
// 导入头文件 #import "CardIO.h" #import "CardIOPaymentViewControllerDelegate.h // 遵守协议 <CardIOPaymentViewControllerDelegate>
-
-
开始扫描银行卡
[CardIOUtilities preload]; CardIOPaymentViewController *scanViewController = [[CardIOPaymentViewController alloc] initWithPaymentDelegate:self]; [self presentViewController:scanViewController animated:YES completion:nil];
-
取消扫描
// CardIOPaymentViewControllerDelegate 协议方法 - (void)userDidCancelPaymentViewController:(CardIOPaymentViewController *)paymentViewController { [[[UIAlertView alloc] initWithTitle:@"User cancelled sca" message:nil delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil, nil] show]; [self dismissViewControllerAnimated:YES completion:nil]; }
-
扫描完成
// CardIOPaymentViewControllerDelegate 协议方法 - (void)userDidProvideCreditCardInfo:(CardIOCreditCardInfo *)cardInfo inPaymentViewController:(CardIOPaymentViewController *)paymentViewController { // 获取扫描结果 // cardNumber 是扫描的银行卡号,显示的是完整号码,而 redactedCardNumber 只显示银行卡后四位,前面的用 * 代替了,返回的银行卡号都没有空格 NSString *redactedCardNumber = cardInfo.cardNumber; // 卡号 NSUInteger expiryMonth = cardInfo.expiryMonth; // 月 NSUInteger expiryYear = cardInfo.expiryYear; // 年 NSString *cvv = cardInfo.cvv; // CVV 码 // 显示扫描结果 NSString *msg = [NSString stringWithFormat:@"Number: %@\n\n expiry: %02lu/%lu\n\n cvv: %@", [self dealCardNumber:redactedCardNumber], expiryMonth, expiryYear, cvv]; [[[UIAlertView alloc] initWithTitle:@"Received card info" message:msg delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil, nil] show]; [self dismissViewControllerAnimated:YES completion:nil]; }
// 对银行卡号进行每隔四位加空格处理,自定义方法 - (NSString *)dealCardNumber:(NSString *)cardNumber { NSString *strTem = [cardNumber stringByReplacingOccurrencesOfString:@" " withString:@""]; NSString *strTem2 = @""; if (strTem.length % 4 == 0) { NSUInteger count = strTem.length / 4; for (int i = 0; i < count; i++) { NSString *str = [strTem substringWithRange:NSMakeRange(i * 4, 4)]; strTem2 = [strTem2 stringByAppendingString:[NSString stringWithFormat:@"%@ ", str]]; } } else { NSUInteger count = strTem.length / 4; for (int j = 0; j <= count; j++) { if (j == count) { NSString *str = [strTem substringWithRange:NSMakeRange(j * 4, strTem.length % 4)]; strTem2 = [strTem2 stringByAppendingString:[NSString stringWithFormat:@"%@ ", str]]; } else { NSString *str = [strTem substringWithRange:NSMakeRange(j * 4, 4)]; strTem2 = [strTem2 stringByAppendingString:[NSString stringWithFormat:@"%@ ", str]]; } } } return strTem2; }
-
效果