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; }
-
效果