首先,在此了解一下Card.io :Card.io是一家为智能手机提供基于软件的信用卡支付解决方案的公司,该公司的技术可以让用户拿着智能手机对信用卡拍照,然后进行光学识别,进而完成支付。有时候通过手机完成信用卡支付还是很麻烦,你需要输入一堆信息:包括卡号、有效期、密码等。有了card.io这个繁琐的过程就可以全免了。
实现原理:Card.io是让手机摄像头获取信用卡的信息,中间利用了OCR(光学字符识别)的扫描技术返回结果。
一、工程配置
1、下载CardIO框架
https://github.com/paypal/PayPal-iOS-SDK
2、把CardIO文件夹拖到工程里,在Link Binary With Libraries中添加框架,如下图红色框框所示:
至此,工程配置完成。
二、代码实现
在ViewController中添加头文件CardIO.h,添加代理CardIOPaymentViewControllerDelegate,添加扫码button,跳转至扫码界面。
self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]initWithTitle:@"扫码" style:UIBarButtonItemStylePlain target:self action:@selector(didScanBtn)];
-(void)didScanBtn
{
CardIOPaymentViewController *scanViewController = [[CardIOPaymentViewController alloc] initWithPaymentDelegate:self];
scanViewController.modalPresentationStyle = UIModalPresentationFormSheet;
[self presentViewController:scanViewController animated:YES completion:nil];
}
实现代理
pragma mark - CardIOPaymentViewControllerDelegate
//扫描完成
- (void)userDidProvideCreditCardInfo:(CardIOCreditCardInfo *)info inPaymentViewController:(CardIOPaymentViewController *)paymentViewController {
// 获取扫描结果
NSString *redactedCardNumber = info.redactedCardNumber; // 卡号
NSUInteger expiryMonth = info.expiryMonth; // 月
NSUInteger expiryYear = info.expiryYear; // 年
NSString *cvv = info.cvv; // CVV 码
// 显示扫描结果
NSString *msg = [NSString stringWithFormat:@"Number: %@\n\n expiry: %02lu/%lu\n\n cvv: %@", redactedCardNumber, expiryMonth, expiryYear, cvv];
[[[UIAlertView alloc] initWithTitle:@"Received card info"
message:msg
delegate:nil
cancelButtonTitle:@"确定"
otherButtonTitles:nil, nil] show];
[self dismissViewControllerAnimated:YES completion:nil];
}
//用户取消扫描
- (void)userDidCancelPaymentViewController:(CardIOPaymentViewController *)paymentViewController {
[self dismissViewControllerAnimated:YES completion:nil];
}
三、效果图