Last active
December 18, 2023 02:33
-
-
Save myrickchow32/dcd5a1a81c622298f9df053357db008e to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
extension ViewController: NFCNDEFReaderSessionDelegate { | |
func readerSession(_ session: NFCNDEFReaderSession, didInvalidateWithError error: Error) { | |
/* | |
Example of error message: | |
"Session is invalidated by user cancellation" --- User has cancelled the NFC reader dialog | |
"Session is invalidated due to maximum session timeout" --- Time out for scanning a NFC tag is exceeded. | |
"Feature not supported" --- Device does not support the NFC feature (iPhone 6S or older) or app is running at simulator | |
*/ | |
print("The session was invalidated: \(error.localizedDescription)") | |
} | |
func readerSession(_ session: NFCNDEFReaderSession, didDetectNDEFs messages: [NFCNDEFMessage]) { | |
// Step 7: Retrieving the NFC data from the NFCNDEFMessage list | |
var result = "" | |
messages.forEach { (nfcndefMessage) in | |
nfcndefMessage.records.forEach({ (nfcndefPayload) in | |
result += String.init(data: nfcndefPayload.payload.advanced(by: 3), encoding: .utf8)! | |
}) | |
} | |
// Step 8: didDetectNDEFs callback is run in background thread. All UI updates must be handled carefully. | |
DispatchQueue.main.async { [weak self] in | |
self?.showAlert("Scanned NFC tag info: " + result) // Simply show an UIAlertController with message | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment