Skip to content

An iOS library using the Vision API to read credit card information

License

Notifications You must be signed in to change notification settings

yhkaplan/credit-card-scanner

Repository files navigation

💳 CreditCardScanner

CreditCardScanner is a library for scanning credit cards to make adding payment information to user accounts easy. It uses Apple's Vision API for secure, on-device machine learning to read the following info from a credit card: number, name, and expiration date.

Example of CreditCardScanner running

Installing

Requirements

  • iOS 13.0+ (due to Vision API having first appeared in iOS 13.0)
    • Even if your minimum deployment target is iOS 12 or lower, you can make this an iOS 13.0+ only feature using canImport and @available
#if canImport(CreditCardScanner)
import CreditCardScanner
#endif

class ViewController: UIViewController {
    override func viewDidLoad() {
        super.viewDidLoad()

        if #available(iOS 13, *) {
            let creditCardScannerViewController = CreditCardScannerViewController(delegate: self)
            present(creditCardScannerViewController, animated: true)
        } else {
            print("Oh well...")
        }
    }
}

@available(iOS 13, *)
extension ViewController: CreditCardScannerViewControllerDelegate {

Swift Package Manager

  • In Xcode, add as Swift package with this URL: https://github.com/yhkaplan/credit-card-scanner.git

Carthage (Experimental)

  • Add this to Cartfile: github "yhkaplan/credit-card-scanner"
  • Follow instructions on Carthage README for integration without adding to copy files script
  • This framework is build as a static framework for Carthage, that's why it has the settings above
  • To build with Carthage yourself, run swift package generate-xcodeproj then run the necessary Carthage commands

Cocoapods

  • Support coming soon

Usage

  1. Add description to Info.plist Privacy - Camera Usage Description
    • Ex: $(PRODUCT_NAME) uses the camera to add credit card
  2. import CreditCardScanner
  3. Conform to CreditCardScannerViewControllerDelegate
  4. Present CreditCardScannerViewController and set its delegate
import CreditCardScanner

class ViewController: UIViewController, CreditCardScannerViewControllerDelegate {

    override func viewDidLoad() {
        super.viewDidLoad()

        let creditCardScannerViewController = CreditCardScannerViewController(delegate: self)
        present(creditCardScannerViewController, animated: true)
    }

    func creditCardScannerViewController(_ viewController: CreditCardScannerViewController, didErrorWith error: CreditCardScannerError) {
        viewController.dismiss(animated: true)
        print(error.errorDescription ?? "Unknown error")
    }

    func creditCardScannerViewController(_ viewController: CreditCardScannerViewController, didFinishWith card: CreditCard) {
        // Do something with credit card info
        print("\(card)")
    }

}

Trying out the Example app

# Install xcodegen if not present
$ brew install xcodegen
# Generate project
$ xcodegen

Alternatives

Card.io

  • Card.io
  • This was a good solution, but it has been unmaintained for a long time and is not fully open-source

CardScan

  • CardScan
  • Open-source and looks well made
  • Supports iOS 11/12 unlike CreditCardScanner
  • Costs money to use

Credits/Inspiration

This was a two person project by @po-miyasaka and @yhkaplan.

This project would not have been possible without Apple's example project (used with permission under an MIT license) demonstrating Vision and AVFoundation and Apple's other example project demonstrating a fully-featured photo app (also used with permission under an MIT license)

License

Licensed under MIT license. See LICENSE for more info.