Skip to content

Commit 59d4a6e

Browse files
committed
Improvements to the NFC connection
1 parent f86cd91 commit 59d4a6e

File tree

2 files changed

+25
-25
lines changed

2 files changed

+25
-25
lines changed

YubiKit/YubiKit/Connection.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -78,8 +78,8 @@ public enum ConnectionError: Error, Sendable {
7878
case missingResult
7979
/// Awaiting call to connect() was cancelled.
8080
case cancelled
81-
/// SmartCardConnection was closed.
82-
case closed
81+
/// Awaiting call to connect() was dismissed by the user.
82+
case cancelledByUser
8383
}
8484

8585
/// A ResponseError containing the status code.

YubiKit/YubiKit/NFCSmartCardConnection.swift

Lines changed: 23 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -267,13 +267,7 @@ private final actor NFCConnectionManager: NSObject {
267267
if let message = message {
268268
currentState.session?.alertMessage = message
269269
}
270-
}
271-
272-
switch result {
273-
case .success:
274-
await invalidate()
275-
case let .failure(error):
276-
await invalidate(error: error)
270+
currentState.session?.invalidate()
277271
}
278272
}
279273

@@ -282,7 +276,7 @@ private final actor NFCConnectionManager: NSObject {
282276
trace(message: "Manager.connected(session:tag:) - tag: \(String(describing: tag.identifier))")
283277

284278
guard let promise = currentState.connectionPromise else {
285-
await invalidate()
279+
await cleanup(session: session)
286280
return
287281
}
288282

@@ -299,15 +293,21 @@ private final actor NFCConnectionManager: NSObject {
299293
await promise.fulfill(connection)
300294
}
301295

302-
private func invalidate(error: Error? = nil) async {
303-
currentState.session?.invalidate()
296+
private func cleanup(session: NFCTagReaderSession, error: Error? = nil) async {
297+
guard currentState.session === session else {
298+
return
299+
}
300+
301+
switch error {
302+
case .none:
303+
await currentState.didCloseConnection?.fulfill(nil)
304+
await currentState.connectionPromise?.cancel(with: ConnectionError.cancelledByUser)
305+
case let .some(error):
306+
await currentState.didCloseConnection?.fulfill(nil)
307+
await currentState.connectionPromise?.cancel(with: error)
308+
}
304309

305-
// Workaround for the NFC session being active for an additional 4 seconds after
306-
// invalidate() has been called on the session.
307-
try? await Task.sleep(for: .seconds(5))
308-
await currentState.didCloseConnection?.fulfill(error)
309-
await currentState.connectionPromise?.cancel(with: error ?? ConnectionError.cancelled)
310-
currentState = .inactive
310+
self.currentState = .inactive
311311
}
312312
}
313313

@@ -325,16 +325,16 @@ extension NFCConnectionManager: NFCTagReaderSessionDelegate {
325325
trace(message: "NFCTagReaderSessionDelegate: Session invalidated – \(error.localizedDescription)")
326326

327327
let nfcError = error as? NFCReaderError
328+
329+
let mappedError: Error?
328330
switch nfcError?.code {
329331
case .some(.readerSessionInvalidationErrorUserCanceled):
330-
return
332+
mappedError = nil // user cancelled, no error
331333
default:
332-
Task {
333-
if await currentState.session === session {
334-
await stop(with: .failure(error))
335-
}
336-
}
334+
mappedError = error
337335
}
336+
337+
Task { await cleanup(session: session, error: mappedError) }
338338
}
339339

340340
// @TraceScope
@@ -354,7 +354,7 @@ extension NFCConnectionManager: NFCTagReaderSessionDelegate {
354354
if await session === currentState.session {
355355
await connected(session: session, tag: firstTag)
356356
} else {
357-
await invalidate(error: ConnectionError.cancelled)
357+
await cleanup(session: session, error: ConnectionError.cancelled)
358358
}
359359
}
360360
}

0 commit comments

Comments
 (0)