Skip to content

treastrain/TRETJapanNFCReader

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

578 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

TRETNFCKit, A wrapper for Core NFC and a useful helper when using NFC, leveraging Swift features for Apple platforms (iOS etc.)

TRETNFCKit

A wrapper for Core NFC and a useful helper when using NFC, leveraging Swift features for Apple platforms.
(Renamed from treastrain/TRETJapanNFCReader)

MIT License GitHub Stars
Platform: iOS & iPadOS|macOS|tvOS|watchOS|visionOS SwiftUI compatible
Swift: 6.0 Swift Package Manager compatible
X (formerly Twitter): @treastrain
Swift Build & Test Xcode Build & Test Example App Build

Features

  • ✅ Asynchronous sequence for NFC tag reading, which is very similar to CardSession.eventStream (provided by Core NFC in iOS 17.4+, which allows communication with HCE-based NFC readers based on ISO 7816-4)
    • NFCNDEFReaderSession.messageEventStream / NFCNDEFReaderSession.tagEventStream
    • NFCTagReaderSession.eventStream
    • NFCVASReaderSession.responseEventStream
  • ✅ SwiftUI compatible
    • nfcNDEFMessageReader: NDEF messages
    • nfcNDEFTagReader: NDEF tags
    • nfcNativeTagReader: NFC-A/B, NFC-F
    • feliCaTagReader: FeliCa (NFC-F)
    • iso7816TagReader: ISO 7816-compatible (NFC-A/B)
    • iso15693TagReader: ISO 15693-compatible (NFC-V)
    • miFareTagReader: MiFare (MIFARE Plus, UltraLight, DESFire) base on ISO 14443 (NFC-A)

Usage

Asynchronous sequence for NFC tag reading

import TRETNFCKit

func tagReaderSample() async {
    guard NFCTagReaderSession.readingAvailable else { return }
    guard let readerSession = NFCTagReaderSession(pollingOption: [.iso14443, .iso15693, .iso18092]) else { return }
    readerSession.alertMessage = "Place the tag on a flat, non-metal surface and rest your iPhone on the tag."
    for await event in readerSession.eventStream {
        switch event {
        case .sessionBecomeActive:
            break
        case .sessionDetected(let tags):
            let tag = tags.first!
            do {
                try await readerSession.connect(to: tag)
                switch tag {
                case .feliCa(let feliCaTag):
                    readerSession.alertMessage = "FeliCa\n\(feliCaTag.currentIDm as NSData)"
                case .iso7816(let iso7816Tag):
                    readerSession.alertMessage = "ISO14443-4 type A / B tag with ISO7816\n\(iso7816Tag.identifier as NSData)"
                case .iso15693(let iso15693Tag):
                    readerSession.alertMessage = "ISO 15693\n\(iso15693Tag.identifier as NSData)"
                case .miFare(let miFareTag):
                    readerSession.alertMessage = "MiFare technology tag (MIFARE Plus, UltraLight, DESFire) base on ISO14443\n\(miFareTag.identifier as NSData)"
                @unknown default:
                    readerSession.alertMessage = "Unknown tag."
                }
                readerSession.invalidate()
            } catch {
                readerSession.invalidate(errorMessage: error.localizedDescription)
            }
        case .sessionInvalidated(_):
            break
        }
    }
}

For SwiftUI wrapper

import TRETNFCKit

struct NFCNativeTagReaderExampleView: View {
    @State private var isPresented = false
    
    var body: some View {
        List {
            Button("Read") {
                isPresented = true
            }
            .disabled(!NFCTagReaderSession.readingAvailable || isPresented)
        }
        .nfcNativeTagReader(
            isPresented: $isPresented,
            pollingOption: [.iso14443, .iso15693, .iso18092],
            detectingAlertMessage: "Place the tag on a flat, non-metal surface and rest your iPhone on the tag.",
            onDidBecomeActive: { _ in /* ... */ },
            onDidDetect: { readerSession, tags in
                do {
                    let tag = tags.first!
                    try await readerSession.connect(to: tag)
                    switch tag {
                    case .feliCa(let feliCaTag):
                        readerSession.alertMessage = "FeliCa\n\(feliCaTag.currentIDm as NSData)"
                    case .iso7816(let iso7816Tag):
                        readerSession.alertMessage = "ISO14443-4 type A / B tag with ISO7816\n\(iso7816Tag.identifier as NSData)"
                    case .iso15693(let iso15693Tag):
                        readerSession.alertMessage = "ISO 15693\n\(iso15693Tag.identifier as NSData)"
                    case .miFare(let miFareTag):
                        readerSession.alertMessage = "MiFare technology tag (MIFARE Plus, UltraLight, DESFire) base on ISO14443\n\(miFareTag.identifier as NSData)"
                    @unknown default:
                        readerSession.alertMessage = "Unknown tag."
                    }
                    readerSession.invalidate()
                } catch {
                    readerSession.invalidate(errorMessage: error.localizedDescription)
                }
            },
            onDidInvalidate: { _ in /* ... */ }
        )
    }
}

Availability

  • iOS 13.0+
  • iPadOS 13.0+
  • macOS 10.15+
  • Mac Catalyst 13.0+
  • tvOS 13.0+
  • watchOS 6.0+
  • visionOS 1.0+
  • Xcode 16.0+
    • Swift 6.0+

Note

Features related to NFC tag reading are only available in iOS 13.0+.

License

MIT License

Notices

The names of e-money and the services are generally trademarks and registered trademarks of each company. This library is not officially provided by service providers and others.

About

NFC (FeliCa) Reader for iOS 13 later Core NFC / Japanese e-Money prepaid or identification cards

Topics

Resources

License

Stars

Watchers

Forks

Sponsor this project

 

Packages

 
 
 

Contributors

Languages