diff --git a/Sources/Core/NFCNDEFReaderSession+.swift b/Sources/Core/NFCNDEFReaderSession+.swift index db1f06f..16f29b8 100644 --- a/Sources/Core/NFCNDEFReaderSession+.swift +++ b/Sources/Core/NFCNDEFReaderSession+.swift @@ -98,6 +98,17 @@ private final class NFCNDEFReaderSessionDelegateHandleObjectForMessage: NSObject var continuation: AsyncStream.Continuation? func readerSessionDidBecomeActive(_ session: NFCNDEFReaderSession) { + let invalidator = NFCReaderSessionInvalidator(session) + continuation?.onTermination = { @Sendable in + switch $0 { + case .finished: + break + case .cancelled: + invalidator.invalidate(errorMessage: invalidator.alertMessage) + @unknown default: + break + } + } continuation?.yield(.sessionBecomeActive) } @@ -117,6 +128,17 @@ private final class NFCNDEFReaderSessionDelegateHandleObjectForTag: NSObject, NF var continuation: AsyncStream.Continuation? func readerSessionDidBecomeActive(_ session: NFCNDEFReaderSession) { + let invalidator = NFCReaderSessionInvalidator(session) + continuation?.onTermination = { @Sendable in + switch $0 { + case .finished: + break + case .cancelled: + invalidator.invalidate(errorMessage: invalidator.alertMessage) + @unknown default: + break + } + } continuation?.yield(.sessionBecomeActive) } diff --git a/Sources/Core/NFCReaderSessionInvalidator.swift b/Sources/Core/NFCReaderSessionInvalidator.swift index 165cd19..f389a74 100644 --- a/Sources/Core/NFCReaderSessionInvalidator.swift +++ b/Sources/Core/NFCReaderSessionInvalidator.swift @@ -13,6 +13,10 @@ package final class NFCReaderSessionInvalidator: self.readerSession = readerSession } + package var alertMessage: String { + readerSession?.alertMessage ?? "" + } + package func invalidate(errorMessage: String) { readerSession?.invalidate(errorMessage: errorMessage) } diff --git a/Sources/Core/NFCTagReaderSession+.swift b/Sources/Core/NFCTagReaderSession+.swift index 56cf186..619be8c 100644 --- a/Sources/Core/NFCTagReaderSession+.swift +++ b/Sources/Core/NFCTagReaderSession+.swift @@ -56,6 +56,17 @@ private final class NFCTagReaderSessionDelegateHandleObject: NSObject, NFCTagRea var continuation: AsyncStream.Continuation? func tagReaderSessionDidBecomeActive(_ session: NFCTagReaderSession) { + let invalidator = NFCReaderSessionInvalidator(session) + continuation?.onTermination = { @Sendable in + switch $0 { + case .finished: + break + case .cancelled: + invalidator.invalidate(errorMessage: invalidator.alertMessage) + @unknown default: + break + } + } continuation?.yield(.sessionBecomeActive) } diff --git a/Sources/Core/NFCVASReaderSession+.swift b/Sources/Core/NFCVASReaderSession+.swift index 9334d99..7ceaf22 100644 --- a/Sources/Core/NFCVASReaderSession+.swift +++ b/Sources/Core/NFCVASReaderSession+.swift @@ -52,6 +52,17 @@ private final class NFCVASReaderSessionDelegateHandleObject: NSObject, NFCVASRea var continuation: AsyncStream.Continuation? func readerSessionDidBecomeActive(_ session: NFCVASReaderSession) { + let invalidator = NFCReaderSessionInvalidator(session) + continuation?.onTermination = { @Sendable in + switch $0 { + case .finished: + break + case .cancelled: + invalidator.invalidate(errorMessage: invalidator.alertMessage) + @unknown default: + break + } + } continuation?.yield(.sessionBecomeActive) }