Skip to content

TRETNFCKit 3.0.0-alpha.9

Pre-release
Pre-release
Compare
Choose a tag to compare
@treastrain treastrain released this 03 Feb 18:02
· 56 commits to tretnfckit-main since this release
996ad15

Milestone

Topic

The new reader session wrappers include a new instance property that provides the reader session's events via AsyncSequence, inspired by the CardSession.eventStream, the new API provided for card emulation in Core NFC on iOS 17.4+.

Example

import SwiftUI
import TRETNFCKit_Async

struct FeliCaReaderView: View {
    @State private var readerSession: AsyncNFCTagReaderSession?
    
    var body: some View {
        List {
            Button {
                readerSession = AsyncNFCTagReaderSession(pollingOption: .iso18092)
            } label: {
                Text("Read")
            }
            .disabled(readerSession != nil)
        }
        .task(id: readerSession == nil) {
            defer { readerSession = nil }
            guard let readerSession else { return }
            guard AsyncNFCTagReaderSession.readingAvailable else { return }
            
            for await event in readerSession.eventStream {
                switch event {
                case .sessionIsReady:
                    readerSession.alertMessage = "Place the tag on a flat, non-metal surface and rest your iPhone on the tag."
                    readerSession.start()
                case .sessionStarted:
                    break
                case .sessionBecomeActive:
                    break
                case .sessionDetected(let tags):
                    do {
                        let tag = tags.first!
                        guard case .feliCa(let feliCaTag) = tag else { throw NFCReaderError(.readerErrorInvalidParameter) }
                        try await readerSession.connect(to: tag)
                        let (idm, systemCode) = try await feliCaTag.polling(systemCode: Data([0xFE, 0x00]), requestCode: .systemCode, timeSlot: .max1)
                        readerSession.alertMessage = "\(systemCode as NSData)\n\(idm as NSData)"
                        readerSession.stop()
                    } catch {
                        readerSession.stop(errorMessage: error.localizedDescription)
                    }
                case .sessionCreationFailed(let reason):
                    print(reason)
                case .sessionInvalidated(let reason):
                    print(reason)
                }
            }
        }
        .navigationTitle("FeliCa Reader")
    }
}

What's Changed

Full Changelog: 3.0.0-alpha.8...3.0.0-alpha.9