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.
pod 'NFCReaderWriter'
github "janlionly/NFCReaderWriter"
- 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
:
.package(url: "https://github.com/janlionly/NFCReaderWriter.git", .upToNextMajor(from: "1.1.4")),
-
Set your provisioning profile to support for Near Field Communication Tag Reading;
-
Open your project target, on Signing & Capabilities tab, add the Capability of Near Field Communication Tag Reading;
-
Remember to add NFCReaderUsageDescription key for descriptions to your Info.plist.
-
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.
/// ----------------------
/// 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)")
}
// You can change alertMessage anywhere before call 'readerWriter.end()' as follow:
readerWriter.alertMessage = "NFC Tag Info detected"
// Updated: add alertMessage property when detected NFC successfully
readerWriter.detectedMessage = "Your Read/Write NFC successful content."
// Support for reading tag infos and NDEFMessage when the NFC chip was scanned once
func reader(_ session: NFCReader, didDetect tag: __NFCTag, didDetectNDEF message: NFCNDEFMessage)
// Support for reading tag identifier
func reader(_ session: NFCReader, didDetect tag: __NFCTag) {
let tagId = readerWriter.tagIdentifier(with: tag)
// ...
}
- iOS 11.0+
- Swift 4.2 to 5.2
Visit my github: janlionly
Contact with me by email: janlionly@gmail.com
I would love you to contribute to NFCReaderWriter
NFCReaderWriter is available under the MIT license. See the LICENSE file for more info.