Swiftpack.co - tattn/NFCReader as Swift Package

Swiftpack.co is a collection of thousands of indexed Swift packages. Search packages.
See all packages published by tattn.
tattn/NFCReader 1.0.3
Scan and decode NFC tags on iOS
⭐️ 94
πŸ•“ 3 years ago
iOS
.package(url: "https://github.com/tattn/NFCReader.git", from: "1.0.3")

NFCReader

Scan and decode NFC tags on iOS

Features

  • β˜‘ Scan NFC Tag (see below about supported tags)
  • β˜‘ Scan custom NFC tags

Supported Tags

  • IC cards for transit in Japan
    • Suica, Pasmo, Kitaca, ICOCA, TOICA, manaca, PiTaPa, nimoca, SUGOCA, はやかけん
  • IC cards for shopping in Japan
    • nanaco
    • Edy
    • WAON

Requirements

  • Xcode 11.x
  • Swift 5.1+
  • iOS 13.0+
  • iPhone 7 and later

Installation

Swift Package Manager (Recommended)

You can install this framework with Swift Package Manager in Xcode 11.

Carthage

github "tattn/NFCReader"

Configuration

NFCReader uses Core NFC, so you need to add the following setting:

  1. Add Near Field Communication Tag Reading to Capabilities.
  2. Add Privacy - NFC Scan Usage Description to Info.plist.
  3. Add system codes of the card you want to scan to Info.plist.
Card System code
Transit cards in Japan 0003
nanaco, Edy, WAON FE00

Usage

Read histories

import NFCReader

let reader = Reader<Suica>() // `Nanaco`, `Edy` or `Waon`
reader.read(didBecomeActive: { _ in
    print("didBecomeActive")
}, didDetect: { reader, result in
    switch result {
    case .success(let suica):
        let balance = suica.boardingHistory.first?.balance ?? 0
        reader.setMessage("Your balance is Β₯\(balance) .")
    case .failure(let error):
        reader.setMessage("something wrong")
    }
})

You can see more details at Sources/NFCReader/Tags:

Scan multiple tags

let reader = Reader<FeliCa>
reader.read(didDetect: { reader, result in
    switch result {
    case .success(let tag):
        switch tag {
        case .edy(let edy):
            print(edy)
        case .nanaco(let nanaco):
            print(nanaco)
        case .waon(let waon):
            print(waon)
        case .suica(let suica):
            print(suica)
        }
    case .failure(let error):
        print(error)
    }
})

The reader can also read just specific tags. Please see Sources/NFCReader/Tags/FeliCa/FeliCa.swift.

Scan tags repeatedly

reader.read(didDetect: { reader, result in
    switch result {
    case .success(let suica):
        let balance = suica.boardingHistory.first?.balance ?? 0
        reader.setMessage("Your balance is Β₯\(balance) .")
        reader.restartReading() // continue to scan
    case .failure(let error):
        reader.setMessage("something wrong")
        reader.restartReading()
    }
})

Custom message

var configuration = ReaderConfiguration()
configuration.message.alert = "Hold your iPhone near the Suica."

let reader = Reader<Suica>(configuration: configuration)

Read custom tags

Please see ./Sources/NFCReader/Tags/FeliCa/nanaco/Nanaco.swift.

ToDo

  • ☐ Decode entrance and exit histories of Suica. (service code: 108F)
  • ☐ Decode SF entrance histories of Suica. (service code: 10CB)
  • ☐ Support more NFC tags.

Contributing

  1. Fork it!
  2. Create your feature branch: git checkout -b my-new-feature
  3. Commit your changes: git commit -am 'Add some feature'
  4. Push to the branch: git push origin my-new-feature
  5. Submit a pull request :D

Support this project

Donating to help me continue working on this project.

Donate

License

NFCReader is released under the MIT license. See LICENSE for details.

Author

Tatsuya Tanaka

Twitter GitHub

GitHub

link
Stars: 94
Last commit: 3 years ago
Advertisement: IndiePitcher.com - Cold Email Software for Startups

Release Notes

first releaseπŸŽ‰
4 years ago

Swiftpack is being maintained by Petr Pavlik | @ptrpavlik | @swiftpackco | API | Analytics