-
Notifications
You must be signed in to change notification settings - Fork 27
/
README.md
173 lines (135 loc) · 5.94 KB
/
README.md
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
# NFCReaderWriter
<img src="https://github.com/janlionly/NFCReaderWriter/blob/master/Resources/NFC-Result.PNG" width="250" height="541">
[![Version](https://img.shields.io/cocoapods/v/NFCReaderWriter.svg?style=flat)](https://cocoapods.org/pods/NFCReaderWriter)
[![Carthage Compatible](https://img.shields.io/badge/Carthage-compatible-4BC51D.svg?style=flat)](https://github.com/Carthage/Carthage)
[![License](https://img.shields.io/cocoapods/l/NFCReaderWriter.svg?style=flat)](https://github.com/janlionly/NFCReaderWriter/blob/master/LICENSE)
[![Platform](https://img.shields.io/cocoapods/p/NFCReaderWriter.svg?style=flat)](https://github.com/janlionly/NFCReaderWriter)
![Swift](https://img.shields.io/badge/%20in-swift%204.2-orange.svg)
## Description
**NFCReaderWriter** which supports to read data from NFC chips(iOS 11), write data to NFC chips(iOS 13) and read NFC tags infos(iOS 13) by iOS devices. Compatible with both Swift and Objective-C.
## Installation
### CocoaPods
```swift
pod 'NFCReaderWriter'
```
### Carthage
```swift
github "janlionly/NFCReaderWriter"
```
### Swift Package Manager
- iOS: Open Xcode, File->Swift Packages, search input **https://github.com/janlionly/NFCReaderWriter.git**, and then select Version Up to Next Major **1.1.4** < .
- Or add dependencies in your `Package.swift`:
```swift
.package(url: "https://github.com/janlionly/NFCReaderWriter.git", .upToNextMajor(from: "1.1.4")),
```
## Usage
1. Set your provisioning profile to support for **Near Field Communication Tag Reading**;
2. Open your project target, on **Signing & Capabilities** tab, add the Capability of **Near Field Communication Tag Reading**;
3. Remember to add **NFCReaderUsageDescription** key for descriptions to your Info.plist.
4. Support for read tag identifier(iOS 13), you should add your NFC tag type descriptions to your Info.plist.
(eg: like **com.apple.developer.nfc.readersession.felica.systemcodes**, **com.apple.developer.nfc.readersession.iso7816.select-identifiers**)
**More information please run demo above.**
```swift
/// ----------------------
/// 1. NFC Reader(iOS 11):
/// ----------------------
// every time read NFC chip's data, open a new session to detect
readerWriter.newReaderSession(with: self, invalidateAfterFirstRead: true, alertMessage: "Nearby NFC Card for read")
readerWriter.begin()
// implement NFCReaderDelegate to read NFC chip's data
func reader(_ session: NFCReader, didDetectNDEFs messages: [NFCNDEFMessage]) {
for message in messages {
for (i, record) in message.records.enumerated() {
print("Record \(i+1): \(String(data: record.payload, encoding: .ascii))")
// other record properties: typeNameFormat, type, identifier
}
}
readerWriter.end()
}
/// ----------------------
/// 2. NFC Writer(iOS 13):
/// ----------------------
// every time write data to NFC chip, open a new session to write
readerWriter.newWriterSession(with: self, isLegacy: true, invalidateAfterFirstRead: true, alertMessage: "Nearby NFC Card for write")
readerWriter.begin()
// implement NFCReaderDelegate to write data to NFC chip
func reader(_ session: NFCReader, didDetect tags: [NFCNDEFTag]) {
// here for write test data
var payloadData = Data([0x02])
let urls = ["apple.com", "google.com", "facebook.com"]
payloadData.append(urls[Int.random(in: 0..<urls.count)].data(using: .utf8)!)
let payload = NFCNDEFPayload.init(
format: NFCTypeNameFormat.nfcWellKnown,
type: "U".data(using: .utf8)!,
identifier: Data.init(count: 0),
payload: payloadData,
chunkSize: 0)
let message = NFCNDEFMessage(records: [payload])
readerWriter.write(message, to: tags.first!) { (error) in
if let err = error {
print("ERR:\(err)")
} else {
print("write success")
}
self.readerWriter.end()
}
}
/// -------------------------
/// 3. NFC Tag Reader(iOS 13)
/// -------------------------
readerWriter.newWriterSession(with: self, isLegacy: false, invalidateAfterFirstRead: true, alertMessage: "Nearby NFC card for read tag identifier")
readerWriter.begin()
// implement NFCReaderDelegate to read tag info from NFC chip
func reader(_ session: NFCReader, didDetect tag: __NFCTag, didDetectNDEF message: NFCNDEFMessage) {
let tagId = readerWriter.tagIdentifier(with: tag)
let content = contentsForMessages([message])
DispatchQueue.main.async {
self.tagIdLabel.text = "Read Tag Identifier:\(tagId.hexadecimal)"
self.tagInfoTextView.text = "TagInfo:\n\(tagInfosDetail)\nNFCNDEFMessage:\n\(content)"
}
self.readerWriter.end()
}
/// --------------------------------
/// other NFCReaderDelegate methods:
/// --------------------------------
func readerDidBecomeActive(_ session: NFCReader) {
print("Reader did become")
}
func reader(_ session: NFCReader, didInvalidateWithError error: Error) {
print("ERROR:\(error)")
}
```
## Version Updates
### V1.1.4
```swift
// You can change alertMessage anywhere before call 'readerWriter.end()' as follow:
readerWriter.alertMessage = "NFC Tag Info detected"
```
### V1.1.3
```swift
// Updated: add alertMessage property when detected NFC successfully
readerWriter.detectedMessage = "Your Read/Write NFC successful content."
```
### V1.1.2
```swift
// Support for reading tag infos and NDEFMessage when the NFC chip was scanned once
func reader(_ session: NFCReader, didDetect tag: __NFCTag, didDetectNDEF message: NFCNDEFMessage)
```
### V1.0.6
```swift
// Support for reading tag identifier
func reader(_ session: NFCReader, didDetect tag: __NFCTag) {
let tagId = readerWriter.tagIdentifier(with: tag)
// ...
}
```
## Requirements
- iOS 11.0+
- Swift 4.2 to 5.2
## Author
Visit my github: [janlionly](https://github.com/janlionly)<br>
Contact with me by email: janlionly@gmail.com
## Contribute
I would love you to contribute to **NFCReaderWriter**
## License
**NFCReaderWriter** is available under the MIT license. See the [LICENSE](https://github.com/janlionly/NFCReaderWriter/blob/master/LICENSE) file for more info.