Skip to content

Add writeTag support for iOS #52

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
gasparrobi opened this issue Dec 26, 2019 · 14 comments
Open

Add writeTag support for iOS #52

gasparrobi opened this issue Dec 26, 2019 · 14 comments

Comments

@gasparrobi
Copy link

writing to a tag is now supported in iOS 13.

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

@EddyVerbruggen
Copy link
Owner

Oh cool!

EddyVerbruggen added a commit that referenced this issue Dec 30, 2019
@EddyVerbruggen
Copy link
Owner

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:

readerSessionDidDetectTags(session: NFCNDEFReaderSession, tags: NSArray<NFCNDEFTag> | NFCNDEFTag[]): void {
/*
// TODO prolly remember the tags for when the app wants to write to it (also: check Android impl for possibly sth similar)
const nfcNdefTag = tags[0];
session.connectToTagCompletionHandler(nfcNdefTag, (error: NSError) => {
console.log(">> connected to tag, error: " + error);
});
// TODO either Text or URI
const payload: NFCNDEFPayload = NFCNDEFPayload.wellKnownTypeTextPayloadWithStringLocale("EddyIOS", NSLocale.currentLocale);
console.log(">> payload: " + payload);
const ndefMessage: NFCNDEFMessage = NFCNDEFMessage.alloc().initWithNDEFRecords([payload]);
console.log(">> ndefMessage: " + ndefMessage);
if (nfcNdefTag.writeNDEFCompletionHandler) {
nfcNdefTag.writeNDEFCompletionHandler(ndefMessage, (error: NSError) => {
console.log(">> writeNDEFCompletionHandler, error: " + error);
});
}
*/
}

@gasparrobi
Copy link
Author

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
Copy link

joeelia commented Jan 13, 2020

Looking forward to this working

@sashok1337
Copy link

sashok1337 commented Feb 14, 2020

Maybe it's somehow connected to NativeScript/ios-jsc#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
Copy link

demetrius-tech commented May 8, 2020

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
Copy link

joeelia commented May 8, 2020

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

@demetrius-tech
Copy link

demetrius-tech commented May 8, 2020

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: chariotsolutions/phonegap-nfc@a3b1e3a

@EddyVerbruggen
Copy link
Owner

A PR would be very much appreciated!

@demetrius-tech
Copy link

demetrius-tech commented May 8, 2020

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
Copy link

joeelia commented May 23, 2020

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
Copy link

demetrius-tech commented Jun 23, 2020

@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
Copy link

timh5 commented Dec 4, 2021

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

@demetrius-tech
Copy link

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

6 participants