I'm currently facing a problem with implementing a specific feature into an NFC based iOS 13 app. When reading a tag, I'd like to get the unique hardware id and the NDEF message in one session. So far I've checked different sample projects, including code provided by Apple and was able to get the information I'm interested in, but not in the same reading session.
I simplified the following code snippets to better focus on the problem (missing error checks, etc.).
Using an NFCTagReaderSession to get the unique hardware id:
func tagReaderSession(_ session: NFCTagReaderSession, didDetect tags: [NFCTag]) {
let tag = tags.first!
session.connect(to: tag) { (error: Error?) in
if case let .miFare(mifareTag) = tag {
print(mifareTag.identifier as NSData) //this is the info I'm interested in
}
}
}
The payload type of a message record however seems to be only available in a NFCNDEFReaderSession:
func readerSession(_ session: NFCNDEFReaderSession, didDetect tags: [NFCNDEFTag]) {
let tag = tags.first!
session.connect(to: tag, completionHandler: { (error: Error?) in
tag.queryNDEFStatus(completionHandler: { (ndefStatus: NFCNDEFStatus, capacity: Int, error: Error?) in
tag.readNDEF(completionHandler: { (message: NFCNDEFMessage?, error: Error?) in
var statusMessage: String
if nil != error || nil == message {
statusMessage = "Fail to read NDEF from tag"
} else {
statusMessage = "Found 1 NDEF message"
let payload = message.records.first!
if let type = String(data: payload.type, encoding: .utf8) {
print("type:%@", type) //this is the info I'm interested in
}
}
session.alertMessage = statusMessage
session.invalidate()
})
})
})
}
As you can see, I can either get the hardware id, using a NFCTagReaderSession or the payload type of a message record, using a NFCNDEFReaderSession. Am I missing something here or are there indeed two different reading sessions required to get the information I'm interested in? Thanks in advance.