Reputation: 21
Hello All I am trying to scan and Authenticate the password protected tag using Ntag213 using CoreNFC library in IOS Swift.I used the sample code provided in the apple site.https://developer.apple.com/documentation/corenfc/building_an_nfc_tag-reader_app
since the Tag is compatible with the Mifare commands I am using the Mifare command to authenticate. I have provided the values of PWD and PACK. still its not able to scan and read the Tag. I have modified the MessagesTableViewController file of that as below.
import UIKit
import CoreNFC
/// - Tag: MessagesTableViewController
class MessagesTableViewController: UITableViewController, NFCNDEFReaderSessionDelegate {
// MARK: - Properties
let reuseIdentifier = "reuseIdentifier"
var detectedMessages = [NFCNDEFMessage]()
var session: NFCNDEFReaderSession?
// MARK: - Actions
/// - Tag: beginScanning
@IBAction func beginScanning(_ sender: Any) {
guard NFCNDEFReaderSession.readingAvailable else {
let alertController = UIAlertController(
title: "Scanning Not Supported",
message: "This device doesn't support tag scanning.",
preferredStyle: .alert
)
alertController.addAction(UIAlertAction(title: "OK", style: .default, handler: nil))
self.present(alertController, animated: true, completion: nil)
return
}
session = NFCNDEFReaderSession(delegate: self, queue: nil, invalidateAfterFirstRead: false)
session?.alertMessage = "Hold your iPhone near the item to learn more about it."
session?.begin()
}
// MARK: - NFCNDEFReaderSessionDelegate
/// - Tag: processingTagData
func readerSession(_ session: NFCNDEFReaderSession, didDetectNDEFs messages: [NFCNDEFMessage]) {
DispatchQueue.main.async {
// Process detected NFCNDEFMessage objects.
self.detectedMessages.append(contentsOf: messages)
self.tableView.reloadData()
}
}
func addMessage(fromUserActivity ndefMessage: NFCNDEFMessage) {
// Add the message to the list of detected messages
detectedMessages.append(ndefMessage)
// Reload the table view
tableView.reloadData()
}
func readerSession(_ session: NFCNDEFReaderSession, didDetect tags: [NFCNDEFTag]) {
guard let tag = tags.first else {
return
}
session.connect(to: tag) { error in
if let error = error {
// Handle connection error
print(error.localizedDescription)
session.invalidate()
return
}
if let miFareTag = tag as? NFCMiFareTag {
let uid = miFareTag.identifier
// Print the tag's UID
print("Tag UID: \(uid)")
// Get the password and PACK values from base64
let pwdBytes = Data(base64Encoded: "xyzzzz")!
let packBytes = Data(base64Encoded: "ABC")!
// Authenticate the tag
let authenticationCommand = Data([0x1B] + pwdBytes.map { $0 as UInt8 })
miFareTag.sendMiFareCommand(commandPacket: authenticationCommand) { response, error in
if let error = error {
// Authentication failed
print("The tag is not authenticated")
session.invalidate()
return
}
// Authentication succeeded
// Continue with further operations
if response == packBytes {
print("The tag is authenticated")
} else {
print("The tag is not authenticated")
}
}
} else {
// Handle unsupported tag type
session.invalidate()
}
}
}
func readerSession(_ session: NFCNDEFReaderSession, didInvalidateWithError error: Error) {
// Check the invalidation reason from the returned error.
if let readerError = error as? NFCReaderError {
// Show an alert when the invalidation reason is not because of a
// successful read during a single-tag read session, or because the
// user canceled a multiple-tag read session from the UI or
// programmatically using the invalidate method call.
if (readerError.code != .readerSessionInvalidationErrorFirstNDEFTagRead)
&& (readerError.code != .readerSessionInvalidationErrorUserCanceled) {
let alertController = UIAlertController(
title: "Session Invalidated",
message: error.localizedDescription,
preferredStyle: .alert
)
alertController.addAction(UIAlertAction(title: "OK", style: .default, handler: nil))
DispatchQueue.main.async {
}}}}}
I tried the code provided as above as per the code it should scan and authenticated the Tag but its not doing it and I am not getting any errors.
Upvotes: 1
Views: 497