@capgo/capacitor-nfc 8.0.18 → 8.0.23

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -4,7 +4,7 @@ import UIKit
4
4
 
5
5
  @objc(NfcPlugin)
6
6
  public class NfcPlugin: CAPPlugin, CAPBridgedPlugin {
7
- private let pluginVersion: String = "8.0.18"
7
+ private let pluginVersion: String = "8.0.23"
8
8
 
9
9
  public let identifier = "NfcPlugin"
10
10
  public let jsName = "CapacitorNfc"
@@ -29,19 +29,52 @@ public class NfcPlugin: CAPPlugin, CAPBridgedPlugin {
29
29
  private var invalidateAfterFirstRead = true
30
30
  private var sessionType: String = "ndef"
31
31
 
32
+ private func isSessionAvailable(for type: String) -> Bool {
33
+ if type == "tag" {
34
+ return NFCTagReaderSession.readingAvailable
35
+ }
36
+ return NFCNDEFReaderSession.readingAvailable
37
+ }
38
+
39
+ private func isNfcAvailable() -> Bool {
40
+ NFCTagReaderSession.readingAvailable || NFCNDEFReaderSession.readingAvailable
41
+ }
42
+
43
+ private func makeTagReaderSession(
44
+ pollingOptions: NFCTagReaderSession.PollingOption,
45
+ alertMessage: String?
46
+ ) -> NFCTagReaderSession? {
47
+ guard let session = NFCTagReaderSession(
48
+ pollingOption: pollingOptions,
49
+ delegate: self,
50
+ queue: sessionQueue
51
+ ) else {
52
+ return nil
53
+ }
54
+
55
+ if let alertMessage, !alertMessage.isEmpty {
56
+ session.alertMessage = alertMessage
57
+ }
58
+
59
+ session.begin()
60
+ return session
61
+ }
62
+
32
63
  @objc public func startScanning(_ call: CAPPluginCall) {
33
64
  #if targetEnvironment(simulator)
34
65
  call.reject("NFC is not available on the simulator.", "NO_NFC")
35
66
  return
36
67
  #else
37
- guard NFCNDEFReaderSession.readingAvailable else {
68
+ let requestedSessionType = call.getString("iosSessionType", "ndef").lowercased()
69
+ sessionType = requestedSessionType == "tag" ? "tag" : "ndef"
70
+
71
+ guard isSessionAvailable(for: sessionType) else {
38
72
  call.reject("NFC is not available on this device.", "NO_NFC")
39
73
  return
40
74
  }
41
75
 
42
76
  invalidateAfterFirstRead = call.getBool("invalidateAfterFirstRead", true)
43
77
  let alertMessage = call.getString("alertMessage")
44
- sessionType = call.getString("iosSessionType", "ndef")
45
78
 
46
79
  DispatchQueue.main.async {
47
80
  // Invalidate any existing sessions
@@ -52,11 +85,19 @@ public class NfcPlugin: CAPPlugin, CAPBridgedPlugin {
52
85
 
53
86
  if self.sessionType == "tag" {
54
87
  // Use NFCTagReaderSession for raw tag support
55
- self.tagReaderSession = NFCTagReaderSession(
56
- pollingOption: [.iso14443, .iso15693, .iso18092],
57
- delegate: self,
58
- queue: self.sessionQueue
88
+ self.tagReaderSession = self.makeTagReaderSession(
89
+ pollingOptions: [.iso14443, .iso15693, .iso18092],
90
+ alertMessage: alertMessage
59
91
  )
92
+
93
+ // Some configurations block FeliCa polling; retry without iso18092 to keep common formats working.
94
+ if self.tagReaderSession == nil {
95
+ self.tagReaderSession = self.makeTagReaderSession(
96
+ pollingOptions: [.iso14443, .iso15693],
97
+ alertMessage: alertMessage
98
+ )
99
+ }
100
+
60
101
  guard self.tagReaderSession != nil else {
61
102
  call.reject(
62
103
  "Failed to create NFC tag reader session. Make sure the 'Near Field Communication Tag Reader Session Formats' entitlement includes the 'TAG' format in your app target.",
@@ -64,10 +105,6 @@ public class NfcPlugin: CAPPlugin, CAPBridgedPlugin {
64
105
  )
65
106
  return
66
107
  }
67
- if let alertMessage, !alertMessage.isEmpty {
68
- self.tagReaderSession?.alertMessage = alertMessage
69
- }
70
- self.tagReaderSession?.begin()
71
108
  } else {
72
109
  // Use NFCNDEFReaderSession (default behavior)
73
110
  self.ndefReaderSession = NFCNDEFReaderSession(
@@ -159,7 +196,7 @@ public class NfcPlugin: CAPPlugin, CAPBridgedPlugin {
159
196
  }
160
197
 
161
198
  @objc public func getStatus(_ call: CAPPluginCall) {
162
- let status = NFCNDEFReaderSession.readingAvailable ? "NFC_OK" : "NO_NFC"
199
+ let status = isNfcAvailable() ? "NFC_OK" : "NO_NFC"
163
200
  call.resolve([
164
201
  "status": status
165
202
  ])
@@ -190,7 +227,7 @@ public class NfcPlugin: CAPPlugin, CAPBridgedPlugin {
190
227
  ])
191
228
  #else
192
229
  call.resolve([
193
- "supported": NFCNDEFReaderSession.readingAvailable
230
+ "supported": isNfcAvailable()
194
231
  ])
195
232
  #endif
196
233
  }
@@ -408,8 +445,8 @@ extension NfcPlugin: NFCNDEFReaderSessionDelegate {
408
445
  if (error as NSError).code != NFCReaderError.readerSessionInvalidationErrorFirstNDEFTagRead.rawValue {
409
446
  DispatchQueue.main.async {
410
447
  let payload: [String: Any] = [
411
- "status": NFCNDEFReaderSession.readingAvailable ? "NFC_OK" : "NO_NFC",
412
- "enabled": NFCNDEFReaderSession.readingAvailable
448
+ "status": self.isNfcAvailable() ? "NFC_OK" : "NO_NFC",
449
+ "enabled": self.isNfcAvailable()
413
450
  ]
414
451
  self.notifyListeners("nfcStateChange", data: payload, retainUntilConsumed: true)
415
452
  }
@@ -488,8 +525,8 @@ extension NfcPlugin: NFCTagReaderSessionDelegate {
488
525
  if nfcError.code != NFCReaderError.readerSessionInvalidationErrorUserCanceled.rawValue {
489
526
  DispatchQueue.main.async {
490
527
  let payload: [String: Any] = [
491
- "status": NFCNDEFReaderSession.readingAvailable ? "NFC_OK" : "NO_NFC",
492
- "enabled": NFCNDEFReaderSession.readingAvailable
528
+ "status": self.isNfcAvailable() ? "NFC_OK" : "NO_NFC",
529
+ "enabled": self.isNfcAvailable()
493
530
  ]
494
531
  self.notifyListeners("nfcStateChange", data: payload, retainUntilConsumed: true)
495
532
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@capgo/capacitor-nfc",
3
- "version": "8.0.18",
3
+ "version": "8.0.23",
4
4
  "description": "Native NFC tag discovery, reading and writing for Capacitor apps on iOS and Android.",
5
5
  "main": "dist/plugin.cjs.js",
6
6
  "module": "dist/esm/index.js",