nativescript-nfc icon indicating copy to clipboard operation
nativescript-nfc copied to clipboard

Add writeTag support for iOS

Open gasparrobi opened this issue 5 years ago • 14 comments

writing to a tag is now supported in iOS 13.

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

gasparrobi avatar Dec 26 '19 10:12 gasparrobi

Oh cool!

EddyVerbruggen avatar Dec 27 '19 06:12 EddyVerbruggen

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

EddyVerbruggen avatar Dec 30 '19 08:12 EddyVerbruggen

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

gasparrobi avatar Jan 06 '20 17:01 gasparrobi

Looking forward to this working

joeelia avatar Jan 13 '20 21:01 joeelia

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 :(

sashok1337 avatar Feb 14 '20 11:02 sashok1337

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.

demetrius-tech avatar May 08 '20 15:05 demetrius-tech

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

joeelia avatar May 08 '20 15:05 joeelia

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

demetrius-tech avatar May 08 '20 16:05 demetrius-tech

A PR would be very much appreciated!

EddyVerbruggen avatar May 08 '20 16:05 EddyVerbruggen

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

demetrius-tech avatar May 08 '20 16:05 demetrius-tech

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

joeelia avatar May 23 '20 20:05 joeelia

@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.

demetrius-tech avatar Jun 23 '20 19:06 demetrius-tech

@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. [email protected]

timh5 avatar Dec 04 '21 06:12 timh5

@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.

demetrius-tech avatar Dec 04 '21 07:12 demetrius-tech