EddyVerbruggen / nativescript-nfc

:pencil: NativeScript plugin to discover, read, and write NFC tags
MIT License
84 stars 37 forks source link

Add writeTag support for iOS #52

Open gasparrobi opened 4 years ago

gasparrobi commented 4 years ago

writing to a tag is now supported in iOS 13.

see writeNDEF at https://developer.apple.com/documentation/corenfc/nfcndeftag

EddyVerbruggen commented 4 years ago

Oh cool!

EddyVerbruggen commented 4 years ago

I've added code to do a little PoC of this feature, but in my tests (on line 165) nfcNdefTag.writeNDEFCompletionHandler is undefined.

Here's the (commented) code.. feel free to play with it:

https://github.com/EddyVerbruggen/nativescript-nfc/blob/d4414b3a55f7d8eff6ed839ef531cb0262b10ad3/src/nfc.ios.ts#L150-L171

gasparrobi commented 4 years ago

I'm trying to use your snippet. While the readerSessionDidDetectTags is called, the tags input parameter is undefined. I'm not sure why. I've tried with both empty and non-empty tags

joeelia commented 4 years ago

Looking forward to this working

sashok1337 commented 4 years ago

Maybe it's somehow connected to NativeScript/ios-runtime#1251 ? There is also native object transforms to JS without needed methods. I've tested app with latest version of ios-platform (that consist fix of that error) - nfcNdefTag.writeNDEFCompletionHandler still undefined :(

demetrius-tech commented 4 years ago

Do you guys need any help? I need this feature and may be you've already made some progress or I can start working on this.

joeelia commented 4 years ago

No progress. iOS write to NFC still dosent work for me

demetrius-tech commented 4 years ago

Check this out: https://github.com/chariotsolutions/phonegap-nfc

The problem is that beginSession() is used instead of scanNdef().

"On iOS, the user must start a NFC session to scan for a tag. This is different from Android which can constantly scan for NFC tags. The nfc.scanNdef and nfc.scanTag functions start a NFC scanning session. The NFC tag is returned to the caller via a Promise. If your existing code uses the deprecated nfc.beginSession, update it to use nfc.scanNdef."

They recently fixed this on phonegap-nfc: https://github.com/chariotsolutions/phonegap-nfc/commit/a3b1e3ad5c11267aa3a8468519b4bb7e4d84800a

EddyVerbruggen commented 4 years ago

A PR would be very much appreciated!

demetrius-tech commented 4 years ago

Sure, but it will take time as I don't have the dev env set up for iOS. I am developing for Android now. I am happy to help. I've never done a PR in my life, but I am eager to learn :D

joeelia commented 4 years ago

Perhaps we have to call queryNDEFStatus before trying writeNDEF as per https://developer.apple.com/documentation/corenfc/nfcndeftag/3075574-writendef?language=objc. Found this native Swift sample app code for iOS

    func readerSession(_ session: NFCNDEFReaderSession, didDetect tags: [NFCNDEFTag]) {
        if tags.count > 1 {
            session.alertMessage = "More than 1 tags found. Please present only 1 tag."
            self.tagRemovalDetect(tags.first!)
            return
        }

        // You connect to the desired tag.
        let tag = tags.first!
        session.connect(to: tag) { (error: Error?) in
            if error != nil {
                session.restartPolling()
                return
            }

            // You then query the NDEF status of tag.
            tag.queryNDEFStatus() { (status: NFCNDEFStatus, capacity: Int, error: Error?) in
                if error != nil {
                    session.invalidate(errorMessage: "Fail to determine NDEF status.  Please try again.")
                    return
                }

                if status == .readOnly {
                    session.invalidate(errorMessage: "Tag is not writable.")
                } else if status == .readWrite {
                    if self.ndefMessage!.length > capacity {
                        session.invalidate(errorMessage: "Tag capacity is too small.  Minimum size requirement is \(self.ndefMessage!.length) bytes.")
                        return
                    }

                    // When a tag is read-writable and has sufficient capacity,
                    // write an NDEF message to it.
                    tag.writeNDEF(self.ndefMessage!) { (error: Error?) in
                        if error != nil {
                            session.invalidate(errorMessage: "Update tag failed. Please try again.")
                        } else {
                            session.alertMessage = "Update success!"
                            session.invalidate()
                        }
                    }
                } else {
                    session.invalidate(errorMessage: "Tag is not NDEF formatted.")
                }
            }
        }
    }

But I dont know how to implement this in Nativescript :D

demetrius-tech commented 4 years ago

@EddyVerbruggen: I've done good progress on implementing writeTag for iOS, it actually works, please check here: https://github.com/demetrius-tech/nativescript-nfc/blob/dev-ios-write-uid/src/nfc.ios.ts

Kindly support me in finding a solution for getting the UID #55 I'm pretty desperate.

timh5 commented 2 years ago

@demetrius-tech - what is the status of this feature? it seems that we'll have to add some obj-c code to make it work? Is that the latest development? Can I hire you to consult on some realtime questions as we try to impliment this? Please email me your skype or google/chat info. timhon@gmail.com

demetrius-tech commented 2 years ago

@timh5 it was working in the past, with version 7. Let me check if I committed the code in my repo and I will support you for free, as I don't have much time for other projects.