@iotize/device-com-nfc.cordova 3.9.0 → 3.9.2

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.
@@ -1,51 +1,51 @@
1
- //
2
- // AppDelegate+NFC.swift
3
- // PhoneGap NFC - Cordova Plugin
4
- //
5
- // Copyright © 2019 dev@iotize.com. All rights reserved.
6
-
7
- import CoreNFC
8
-
9
- extension AppDelegate {
10
-
11
- override open func application(_ application: UIApplication,
12
- continue userActivity: NSUserActivity,
13
- restorationHandler: @escaping ([Any]?) -> Void) -> Bool {
14
-
15
- NSLog("Extending UIApplicationDelegate")
16
-
17
- guard userActivity.activityType == NSUserActivityTypeBrowsingWeb else {
18
- return false
19
- }
20
-
21
- // Confirm that the NSUserActivity object contains a valid NDEF message.
22
- if #available(iOS 12.0, *) {
23
- let ndefMessage = userActivity.ndefMessagePayload
24
- guard ndefMessage.records.count > 0,
25
- ndefMessage.records[0].typeNameFormat != .empty else {
26
- return false
27
- }
28
- let nfcPluginInstance: NfcPlugin = self.viewController.getCommandInstance("NfcPlugin") as! NfcPlugin
29
- var resolved: Bool = false;
30
- NSLog(nfcPluginInstance.debugDescription);
31
-
32
- DispatchQueue.global().async {
33
- let waitingTimeInterval: Double = 0.1;
34
- print("<NFC> Did start timeout")
35
- for _ in 1...2000 { // 5?s timeout
36
- if ( !nfcPluginInstance.isListeningNDEF ) {
37
- Thread.sleep(forTimeInterval: waitingTimeInterval)
38
- } else {
39
- let jsonDictionary = ndefMessage.ndefMessageToJSON()
40
- nfcPluginInstance.sendThroughChannel(jsonDictionary: jsonDictionary)
41
- resolved = true
42
- return
43
- }
44
- }
45
- }
46
- return resolved
47
- } else {
48
- return false
49
- }
50
- }
51
- }
1
+ //
2
+ // AppDelegate+NFC.swift
3
+ // PhoneGap NFC - Cordova Plugin
4
+ //
5
+ // Copyright © 2019 dev@iotize.com. All rights reserved.
6
+
7
+ import CoreNFC
8
+
9
+ extension AppDelegate {
10
+
11
+ override open func application(_ application: UIApplication,
12
+ continue userActivity: NSUserActivity,
13
+ restorationHandler: @escaping ([Any]?) -> Void) -> Bool {
14
+
15
+ NSLog("Extending UIApplicationDelegate")
16
+
17
+ guard userActivity.activityType == NSUserActivityTypeBrowsingWeb else {
18
+ return false
19
+ }
20
+
21
+ // Confirm that the NSUserActivity object contains a valid NDEF message.
22
+ if #available(iOS 12.0, *) {
23
+ let ndefMessage = userActivity.ndefMessagePayload
24
+ guard ndefMessage.records.count > 0,
25
+ ndefMessage.records[0].typeNameFormat != .empty else {
26
+ return false
27
+ }
28
+ let nfcPluginInstance: NfcPlugin = self.viewController.getCommandInstance("NfcPlugin") as! NfcPlugin
29
+ var resolved: Bool = false;
30
+ NSLog(nfcPluginInstance.debugDescription);
31
+
32
+ DispatchQueue.global().async {
33
+ let waitingTimeInterval: Double = 0.1;
34
+ print("<NFC> Did start timeout")
35
+ for _ in 1...2000 { // 5?s timeout
36
+ if ( !nfcPluginInstance.isListeningNDEF ) {
37
+ Thread.sleep(forTimeInterval: waitingTimeInterval)
38
+ } else {
39
+ let jsonDictionary = ndefMessage.ndefMessageToJSON()
40
+ nfcPluginInstance.sendThroughChannel(jsonDictionary: jsonDictionary)
41
+ resolved = true
42
+ return
43
+ }
44
+ }
45
+ }
46
+ return resolved
47
+ } else {
48
+ return false
49
+ }
50
+ }
51
+ }
@@ -1,144 +1,144 @@
1
- import CoreNFC
2
-
3
- public extension String {
4
-
5
- func dataFromHexString() -> Data {
6
- var bytes = [UInt8]()
7
- for i in 0..<(count/2) {
8
- let range = index(self.startIndex, offsetBy: 2*i)..<index(self.startIndex, offsetBy: 2*i+2)
9
- let stringBytes = self[range]
10
- let byte = strtol((stringBytes as NSString).utf8String, nil, 16)
11
- bytes.append(UInt8(byte))
12
- }
13
- return Data(bytes: UnsafePointer<UInt8>(bytes), count:bytes.count)
14
- }
15
-
16
- }
17
-
18
- public extension Data {
19
-
20
- func hexEncodedString() -> String {
21
- let format = "%02hhX"
22
- return map { String(format: format, $0) }.joined()
23
- }
24
- }
25
-
26
- public extension NFCISO15693Tag {
27
-
28
- func toJSON(ndefMessage: NFCNDEFMessage? = nil) -> [AnyHashable: Any] {
29
-
30
- let wrapper = NSMutableDictionary()
31
- wrapper.setValue([UInt8](self.identifier) , forKey: "id")
32
-
33
- let returnedJSON = NSMutableDictionary()
34
- returnedJSON.setValue("tag", forKey: "type")
35
- returnedJSON.setObject(wrapper, forKey: "tag" as NSString)
36
-
37
-
38
-
39
- return returnedJSON as! [AnyHashable : Any]
40
- }
41
- }
42
-
43
- public enum NfcTech: String {
44
- case IsoDep = "IsoDep" // Provides access to ISO-DEP (ISO 14443-4) properties and I/O operations on a Tag.
45
- case MifareClassic = "MifareClassic" // Provides access to MIFARE Classic properties and I/O operations on a Tag.
46
- case MifareUltralight = "MifareUltralight" // Provides access to MIFARE Ultralight properties and I/O operations on a Tag.
47
- case Ndef = "Ndef" // Provides access to NDEF content and operations on a Tag.
48
- case NdefFormatable = "NdefFormatable" // Provide access to NDEF format operations on a Tag.
49
- case NfcA = "NfcA" // Provides access to NFC-A (ISO 14443-3A) properties and I/O operations on a Tag.
50
- case NfcB = "NfcB" // Provides access to NFC-B (ISO 14443-3B) properties and I/O operations on a Tag.
51
- case NfcBarcode = "NfcBarcode" // Provides access to tags containing just a barcode.
52
- case NfcF = "NfcF" // Provides access to NFC-F (JIS 6319-4) properties and I/O operations on a Tag.
53
- case NfcV = "NfcV" // Provides access to NFC-V (ISO 15693) properties and I/O operations on a Tag.
54
- case AnyTag = ""
55
- }
56
-
57
- @available(iOS 13.0, *)
58
- public extension NFCTag {
59
-
60
- func toJSON(isTapDiscoveryEnabled: Bool) async -> [AnyHashable: Any] {
61
- let wrapper = NSMutableDictionary()
62
- var techTypes: [String] = []
63
-
64
- switch self {
65
- case .iso15693(let iso15693Tag):
66
- wrapper.setValue([UInt8](iso15693Tag.identifier) , forKey: "id")
67
- techTypes.append(NfcTech.NfcV.rawValue)
68
- do {
69
- let ndefMessage = try await iso15693Tag.readNDEF()
70
- techTypes.append(NfcTech.Ndef.rawValue)
71
- wrapper.setValue(ndefMessage.toJSONRecords(), forKey: "ndefMessage")
72
- } catch {
73
- print("ReadNDEF Error: \(error)")
74
- }
75
-
76
- break
77
- case .iso7816(let iso7816Tag):
78
- wrapper.setValue([UInt8](iso7816Tag.identifier) , forKey: "id")
79
- techTypes.append(NfcTech.NfcA.rawValue)
80
- if let ndefMessage = try? await iso7816Tag.readNDEF() {
81
- techTypes.append(NfcTech.Ndef.rawValue)
82
- wrapper.setValue(ndefMessage.toJSONRecords(), forKey: "ndefMessage")
83
- }
84
- break
85
- case .miFare(let miFareTag):
86
- wrapper.setValue([UInt8](miFareTag.identifier) , forKey: "id")
87
- switch miFareTag.mifareFamily {
88
- case .ultralight:
89
- techTypes.append(NfcTech.MifareUltralight.rawValue)
90
- break
91
- default:
92
- techTypes.append(NfcTech.MifareClassic.rawValue)
93
- break
94
- }
95
- if let ndefMessage = try? await miFareTag.readNDEF() {
96
- techTypes.append(NfcTech.Ndef.rawValue)
97
- wrapper.setValue(ndefMessage.toJSONRecords(), forKey: "ndefMessage")
98
- }
99
- break
100
- default:
101
- break
102
- }
103
-
104
- wrapper.setValue(techTypes, forKey: "techTypes")
105
-
106
- let returnedJSON = NSMutableDictionary()
107
- returnedJSON.setValue(wrapper, forKey: "tag")
108
- returnedJSON.setValue("tag", forKey: "type")
109
- if (isTapDiscoveryEnabled) {
110
- //For now, NFCTag is labelled as IoTizeTap if it is a 15693 tag with 4+ NDEF messages.
111
- //Cf Android implementation
112
- if (techTypes.contains(NfcTech.NfcV.rawValue)) {
113
- if let ndef = wrapper.value(forKey: "ndefMessage") {
114
- if (ndef as! [[AnyHashable: Any]]).count >= 4 {
115
- returnedJSON.setValue("nfc-tap-device", forKey: "type")
116
- //Add tap property
117
- let tapProp = NSMutableDictionary()
118
- tapProp.setValue(false, forKey: "nfcPairingDone")
119
- returnedJSON.setValue(tapProp, forKey: "tap")
120
- }
121
- }
122
- }
123
- }
124
- return returnedJSON as! [AnyHashable : Any]
125
-
126
- }
127
- }
128
-
129
- public extension NFCNDEFMessage {
130
- func toJSONRecords() -> [[AnyHashable: Any]] {
131
- let array = NSMutableArray()
132
- for record in self.records {
133
- let recordDictionary = self.ndefToNSDictionary(record: record)
134
- array.add(recordDictionary)
135
- }
136
- return array as! [[AnyHashable: Any]]
137
- }
138
- }
139
-
140
- public func printNFC(_ message: String) {
141
- #if DEBUG
142
- print("NFC Plugin \(String(format: "%.3f", CACurrentMediaTime())): \(message)")
143
- #endif
144
- }
1
+ import CoreNFC
2
+
3
+ public extension String {
4
+
5
+ func dataFromHexString() -> Data {
6
+ var bytes = [UInt8]()
7
+ for i in 0..<(count/2) {
8
+ let range = index(self.startIndex, offsetBy: 2*i)..<index(self.startIndex, offsetBy: 2*i+2)
9
+ let stringBytes = self[range]
10
+ let byte = strtol((stringBytes as NSString).utf8String, nil, 16)
11
+ bytes.append(UInt8(byte))
12
+ }
13
+ return Data(bytes: UnsafePointer<UInt8>(bytes), count:bytes.count)
14
+ }
15
+
16
+ }
17
+
18
+ public extension Data {
19
+
20
+ func hexEncodedString() -> String {
21
+ let format = "%02hhX"
22
+ return map { String(format: format, $0) }.joined()
23
+ }
24
+ }
25
+
26
+ public extension NFCISO15693Tag {
27
+
28
+ func toJSON(ndefMessage: NFCNDEFMessage? = nil) -> [AnyHashable: Any] {
29
+
30
+ let wrapper = NSMutableDictionary()
31
+ wrapper.setValue([UInt8](self.identifier) , forKey: "id")
32
+
33
+ let returnedJSON = NSMutableDictionary()
34
+ returnedJSON.setValue("tag", forKey: "type")
35
+ returnedJSON.setObject(wrapper, forKey: "tag" as NSString)
36
+
37
+
38
+
39
+ return returnedJSON as! [AnyHashable : Any]
40
+ }
41
+ }
42
+
43
+ public enum NfcTech: String {
44
+ case IsoDep = "IsoDep" // Provides access to ISO-DEP (ISO 14443-4) properties and I/O operations on a Tag.
45
+ case MifareClassic = "MifareClassic" // Provides access to MIFARE Classic properties and I/O operations on a Tag.
46
+ case MifareUltralight = "MifareUltralight" // Provides access to MIFARE Ultralight properties and I/O operations on a Tag.
47
+ case Ndef = "Ndef" // Provides access to NDEF content and operations on a Tag.
48
+ case NdefFormatable = "NdefFormatable" // Provide access to NDEF format operations on a Tag.
49
+ case NfcA = "NfcA" // Provides access to NFC-A (ISO 14443-3A) properties and I/O operations on a Tag.
50
+ case NfcB = "NfcB" // Provides access to NFC-B (ISO 14443-3B) properties and I/O operations on a Tag.
51
+ case NfcBarcode = "NfcBarcode" // Provides access to tags containing just a barcode.
52
+ case NfcF = "NfcF" // Provides access to NFC-F (JIS 6319-4) properties and I/O operations on a Tag.
53
+ case NfcV = "NfcV" // Provides access to NFC-V (ISO 15693) properties and I/O operations on a Tag.
54
+ case AnyTag = ""
55
+ }
56
+
57
+ @available(iOS 13.0, *)
58
+ public extension NFCTag {
59
+
60
+ func toJSON(isTapDiscoveryEnabled: Bool) async -> [AnyHashable: Any] {
61
+ let wrapper = NSMutableDictionary()
62
+ var techTypes: [String] = []
63
+
64
+ switch self {
65
+ case .iso15693(let iso15693Tag):
66
+ wrapper.setValue([UInt8](iso15693Tag.identifier) , forKey: "id")
67
+ techTypes.append(NfcTech.NfcV.rawValue)
68
+ do {
69
+ let ndefMessage = try await iso15693Tag.readNDEF()
70
+ techTypes.append(NfcTech.Ndef.rawValue)
71
+ wrapper.setValue(ndefMessage.toJSONRecords(), forKey: "ndefMessage")
72
+ } catch {
73
+ print("ReadNDEF Error: \(error)")
74
+ }
75
+
76
+ break
77
+ case .iso7816(let iso7816Tag):
78
+ wrapper.setValue([UInt8](iso7816Tag.identifier) , forKey: "id")
79
+ techTypes.append(NfcTech.NfcA.rawValue)
80
+ if let ndefMessage = try? await iso7816Tag.readNDEF() {
81
+ techTypes.append(NfcTech.Ndef.rawValue)
82
+ wrapper.setValue(ndefMessage.toJSONRecords(), forKey: "ndefMessage")
83
+ }
84
+ break
85
+ case .miFare(let miFareTag):
86
+ wrapper.setValue([UInt8](miFareTag.identifier) , forKey: "id")
87
+ switch miFareTag.mifareFamily {
88
+ case .ultralight:
89
+ techTypes.append(NfcTech.MifareUltralight.rawValue)
90
+ break
91
+ default:
92
+ techTypes.append(NfcTech.MifareClassic.rawValue)
93
+ break
94
+ }
95
+ if let ndefMessage = try? await miFareTag.readNDEF() {
96
+ techTypes.append(NfcTech.Ndef.rawValue)
97
+ wrapper.setValue(ndefMessage.toJSONRecords(), forKey: "ndefMessage")
98
+ }
99
+ break
100
+ default:
101
+ break
102
+ }
103
+
104
+ wrapper.setValue(techTypes, forKey: "techTypes")
105
+
106
+ let returnedJSON = NSMutableDictionary()
107
+ returnedJSON.setValue(wrapper, forKey: "tag")
108
+ returnedJSON.setValue("tag", forKey: "type")
109
+ if (isTapDiscoveryEnabled) {
110
+ //For now, NFCTag is labelled as IoTizeTap if it is a 15693 tag with 4+ NDEF messages.
111
+ //Cf Android implementation
112
+ if (techTypes.contains(NfcTech.NfcV.rawValue)) {
113
+ if let ndef = wrapper.value(forKey: "ndefMessage") {
114
+ if (ndef as! [[AnyHashable: Any]]).count >= 4 {
115
+ returnedJSON.setValue("nfc-tap-device", forKey: "type")
116
+ //Add tap property
117
+ let tapProp = NSMutableDictionary()
118
+ tapProp.setValue(false, forKey: "nfcPairingDone")
119
+ returnedJSON.setValue(tapProp, forKey: "tap")
120
+ }
121
+ }
122
+ }
123
+ }
124
+ return returnedJSON as! [AnyHashable : Any]
125
+
126
+ }
127
+ }
128
+
129
+ public extension NFCNDEFMessage {
130
+ func toJSONRecords() -> [[AnyHashable: Any]] {
131
+ let array = NSMutableArray()
132
+ for record in self.records {
133
+ let recordDictionary = self.ndefToNSDictionary(record: record)
134
+ array.add(recordDictionary)
135
+ }
136
+ return array as! [[AnyHashable: Any]]
137
+ }
138
+ }
139
+
140
+ public func printNFC(_ message: String) {
141
+ #if DEBUG
142
+ print("NFC Plugin \(String(format: "%.3f", CACurrentMediaTime())): \(message)")
143
+ #endif
144
+ }
@@ -1,78 +1,78 @@
1
- //
2
- // NFCNDEFDelegate.swift
3
- // NFC
4
- //
5
- // Created by dev@iotize.com on 05/08/2019.
6
- // Copyright © 2019 dev@iotize.com. All rights reserved.
7
- //
8
-
9
- import Foundation
10
- import CoreNFC
11
-
12
- class NFCNDEFDelegate: NSObject, NFCNDEFReaderSessionDelegate {
13
- var session: NFCNDEFReaderSession?
14
- var completed: ([AnyHashable : Any]?, Error?) -> ()
15
-
16
- init(completed: @escaping ([AnyHashable: Any]?, Error?) -> (), message: String?) {
17
- self.completed = completed
18
- super.init()
19
- self.session = NFCNDEFReaderSession.init(delegate: self, queue: nil, invalidateAfterFirstRead: false)
20
- if (self.session == nil) {
21
- self.completed(nil, "NFC is not available" as? Error);
22
- return
23
- }
24
- self.session!.alertMessage = message ?? ""
25
- self.session!.begin()
26
- }
27
-
28
- func readerSession(_: NFCNDEFReaderSession, didDetectNDEFs messages: [NFCNDEFMessage]) {
29
- for message in messages {
30
- self.fireNdefEvent(message: message)
31
- }
32
- self.session?.invalidate()
33
- }
34
-
35
- func readerSession(_: NFCNDEFReaderSession, didInvalidateWithError _: Error) {
36
- completed(nil, "NFCNDEFReaderSession error" as? Error)
37
- }
38
-
39
- func readerSessionDidBecomeActive(_: NFCNDEFReaderSession) {
40
- print("NDEF Reader session active")
41
- }
42
-
43
- func fireNdefEvent(message: NFCNDEFMessage) {
44
- let response = message.ndefMessageToJSON()
45
- completed(response, nil)
46
- }
47
-
48
-
49
-
50
- }
51
-
52
- extension NFCNDEFMessage {
53
- func ndefMessageToJSON() -> [AnyHashable: Any] {
54
- let array = NSMutableArray()
55
- for record in self.records {
56
- let recordDictionary = self.ndefToNSDictionary(record: record)
57
- array.add(recordDictionary)
58
- }
59
- let wrapper = NSMutableDictionary()
60
- wrapper.setObject(array, forKey: "ndefMessage" as NSString)
61
-
62
- let returnedJSON = NSMutableDictionary()
63
- returnedJSON.setValue("ndef", forKey: "type")
64
- returnedJSON.setObject(wrapper, forKey: "tag" as NSString)
65
-
66
- return returnedJSON as! [AnyHashable : Any]
67
- }
68
-
69
- func ndefToNSDictionary(record: NFCNDEFPayload) -> NSDictionary {
70
- let dict = NSMutableDictionary()
71
- dict.setObject([record.typeNameFormat.rawValue], forKey: "tnf" as NSString)
72
- dict.setObject([UInt8](record.type), forKey: "type" as NSString)
73
- dict.setObject([UInt8](record.identifier), forKey: "id" as NSString)
74
- dict.setObject([UInt8](record.payload), forKey: "payload" as NSString)
75
-
76
- return dict
77
- }
78
- }
1
+ //
2
+ // NFCNDEFDelegate.swift
3
+ // NFC
4
+ //
5
+ // Created by dev@iotize.com on 05/08/2019.
6
+ // Copyright © 2019 dev@iotize.com. All rights reserved.
7
+ //
8
+
9
+ import Foundation
10
+ import CoreNFC
11
+
12
+ class NFCNDEFDelegate: NSObject, NFCNDEFReaderSessionDelegate {
13
+ var session: NFCNDEFReaderSession?
14
+ var completed: ([AnyHashable : Any]?, Error?) -> ()
15
+
16
+ init(completed: @escaping ([AnyHashable: Any]?, Error?) -> (), message: String?) {
17
+ self.completed = completed
18
+ super.init()
19
+ self.session = NFCNDEFReaderSession.init(delegate: self, queue: nil, invalidateAfterFirstRead: false)
20
+ if (self.session == nil) {
21
+ self.completed(nil, "NFC is not available" as? Error);
22
+ return
23
+ }
24
+ self.session!.alertMessage = message ?? ""
25
+ self.session!.begin()
26
+ }
27
+
28
+ func readerSession(_: NFCNDEFReaderSession, didDetectNDEFs messages: [NFCNDEFMessage]) {
29
+ for message in messages {
30
+ self.fireNdefEvent(message: message)
31
+ }
32
+ self.session?.invalidate()
33
+ }
34
+
35
+ func readerSession(_: NFCNDEFReaderSession, didInvalidateWithError _: Error) {
36
+ completed(nil, "NFCNDEFReaderSession error" as? Error)
37
+ }
38
+
39
+ func readerSessionDidBecomeActive(_: NFCNDEFReaderSession) {
40
+ print("NDEF Reader session active")
41
+ }
42
+
43
+ func fireNdefEvent(message: NFCNDEFMessage) {
44
+ let response = message.ndefMessageToJSON()
45
+ completed(response, nil)
46
+ }
47
+
48
+
49
+
50
+ }
51
+
52
+ extension NFCNDEFMessage {
53
+ func ndefMessageToJSON() -> [AnyHashable: Any] {
54
+ let array = NSMutableArray()
55
+ for record in self.records {
56
+ let recordDictionary = self.ndefToNSDictionary(record: record)
57
+ array.add(recordDictionary)
58
+ }
59
+ let wrapper = NSMutableDictionary()
60
+ wrapper.setObject(array, forKey: "ndefMessage" as NSString)
61
+
62
+ let returnedJSON = NSMutableDictionary()
63
+ returnedJSON.setValue("ndef", forKey: "type")
64
+ returnedJSON.setObject(wrapper, forKey: "tag" as NSString)
65
+
66
+ return returnedJSON as! [AnyHashable : Any]
67
+ }
68
+
69
+ func ndefToNSDictionary(record: NFCNDEFPayload) -> NSDictionary {
70
+ let dict = NSMutableDictionary()
71
+ dict.setObject([record.typeNameFormat.rawValue], forKey: "tnf" as NSString)
72
+ dict.setObject([UInt8](record.type), forKey: "type" as NSString)
73
+ dict.setObject([UInt8](record.identifier), forKey: "id" as NSString)
74
+ dict.setObject([UInt8](record.payload), forKey: "payload" as NSString)
75
+
76
+ return dict
77
+ }
78
+ }