Swift CoreNFC as quick as possible

Thus it is easy to find by masking the status byte with the value 0x3F.” — NFC Data Exchange Format (NDEF) Technical Specification, NFC ForumThus, the payload of record has to be advanced by 3 before converting to a String for displaying!record.payload.advanced(by:3)Handling result at the didDetectNDEFsCase 1 (init with parameter — invalidateAfterFirstRead: true):If NFC reader session is initialised with invalidateAfterFirstRead equals true, NFC tag reading dialog will be automatically dismissed after tag is successfully detected and the blue-tick animation is completed (last for around 3 seconds).Due to this 3-second time gap, UIAlertController is presented but covered by the NFC tag reading dialog completely, and user cannot interact with anyUIAlertAction..This results in a bad user experience..See below gif:Case 2 (init with parameter — invalidateAfterFirstRead: false):NFC reading tag dialog can be dismissed manually if NFCNDEFReaderSession is initialised with invalidateAfterFirstRead equals false.However, the NFC session can actually be dismissed after the blue-tick animation is completed..Thus, calling nfcSession.invalidate() right before showing UIAlertController does not guarantee the NFC tag reading dialog is actually dismissed..There is an around 0.5 second time gap for this..Thus, the UIAlertController is still shown behind the NFC tag reading dialog for a noticeable short period of time..User experience is not perfect.Case 3 (init with parameter — invalidateAfterFirstRead: false + 1s delay):Finally, the solution to provide the best user experience is to give an around 1 second delay to the DispatchQueue to ensure that the NFC tag reading dialog is completely dismissed before showing an UIAlertController.Minor reminders:The NFCNDEFReaderSession has a default 60 seconds time out for reading NFC tag..Below is the error message for time out.Session is invalidated due to maximum session timeout2..Alert message in the NFC tag reading dialog is optimised for 3 lines of wordings..The 4th line is truncated at the end.3..Titles of NFC tag reading dialog and cancel button are not customisable and the locale is fixed to the device locale.Conclusion:Ensure “NFC Tag Reading” is enabled for your app at the Apple Developer Console.“Near Field Communication Tag Reading” has to be checked in app capabilities list.Make sure your real device has NFC chip or setting CoreNFC.framework to be an optional framework.“Privacy — NFC Scan Usage Description” must be filled in to request the permission of using user’s NFC chip.Create the NFCNDEFReaderSession with invalidateAfterFirstRead: false , and give one second delay to show any UIAlertController or navigate to any page.didDetectNDEFs callback is run in background thread and never update UI directly.Have a nice day and enjoy programming ^_^ !. More details

Leave a Reply