Core NFC writeLock()

We have been successfully writing to NFC tags using Beta 13, but have no luck with trying to lock them using the writeLock() function. After execution we get back a result of nil but it is definitely not locking. Any clues or has anyone managed to get this work as yet?


Code is extremley straight forward (but might have done something stupid!) - Trying on different variations of a NXP NTAG213 tag:


if (self.lockTag) {
                                // locking required also
                                os_log("Tag needs to be locked")
                                tag.writeLock() { (error: Error?) in
                                    if error != nil {
                                        os_log("LOCK FAILED!!")
                                        session.alertMessage = "Lock failed try again"
                                        session.invalidate()
                                    } else {
                                        session.alertMessage = "Write and Lock successful"
                                        session.invalidate()
                                    }
                                }
                                
                            } else {
                                // only writing
                                os_log("Tag is unlocked")
                                session.alertMessage = "Write successful!"
                                session.invalidate()
                            }

Replies

Could you complement all alert with print() and tell what you get ?


Anyway, I do not understand the logic.


line 1: what is self referring to ? What does lockTag means ?

If that means that tag is locked, code cannot work, because once tag is locked, you cannot write to it

OK - yes being stupid - missed out some returns! Not totally sure why it would not just fall through correctly, but this has fixed it


if (self.lockTag) {
                                // locking required also
                                os_log("Tag needs to be locked")
                                tag.writeLock() { (error: Error?) in
                                    if error != nil {
                                        os_log("LOCK FAILED!!")
                                        session.alertMessage = "Lock failed try again"
                                        session.invalidate()
                                        return
                                    } else {
                                        session.alertMessage = "Write and Lock successful"
                                        session.invalidate()
                                        return
                                    }
                                }
                                return



This works perfectly and locks the tags.

Thanks for commenting - as my response below - I missed out some returns and it was not processing correctly but not erroring either.


Thanks again!

Great you found a solution. Don't forget to close the thread.