chariotsolutions / phonegap-nfc

PhoneGap NFC Plugin
MIT License
706 stars 555 forks source link

Is there anyway to detect a canceled scan? #393

Closed joshstrange closed 4 years ago

joshstrange commented 4 years ago

I was showing something in the background when you started a scan but if you press the iOS "Cancel" button then you get stuck (in my app). Is there a way to detect when a user does that or do I need to find another solution?

fixdaddy commented 4 years ago

I have the same question. IOS surely allows it :https://developer.apple.com/documentation/corenfc/building_an_nfc_tag-reader_app.

I don't feel competent to tackle this update myself, though.

fixdaddy commented 4 years ago

Looking at the file phonegap-nfc/blob/master/src/ios/NfcPlugin.m, I feel there is code to deal with this (lines 86-93). You would have to look for an error that reads "readerSessionInvalidationErrorUserCanceled". Logically, your code should be looking for it in the failure callback of the nfc.beginSession method. Unfortunately, I don't see clear evidence that there is code for a failure callback (lines 33-39). Help from experts would be greatly appreciated here... Thanks in advance!

fixdaddy commented 4 years ago

I have tested with an iPhone and can confirm the following:

Line 87 reads as follows: NSLog(@"didInvalidateWithError %@ %@", error.localizedDescription, error.localizedFailureReason);

From the log output, it seems that the error.localizedDescription has a value that could be used in the code using the plugin to distinguish between actions to take when the user canceled the scan.

@don on you mentioned in the Readme file that if someone had a use case and idea how to implement, a ticket for discussion should be opened. I believe all conditions are met.

I suggest to use the failure callback of the nfc.beginSession to pass the localizedDescription.

I volounteer to test an improved version of the NfcPlugin.m file.

Thanks in advance!

wreiske commented 4 years ago

Add me on that list as a volunteer to test! +1

cytostatic commented 4 years ago

I have the same Problem. Is there any solution yet?

It is a cool plugin, but with this it is unusable for my App.

fixdaddy commented 4 years ago

Hello,

No solution yet, unfortunately. I agree the plugin is cool.

I am still hopeful someone who knows the language and architecture of the plugin/Cordova will step in.

don commented 4 years ago

The failure callback for beginSession is the right place to send this. I'm working on some other iOS fixes and will try to add this too.

don commented 4 years ago

Scan cancelled error is never being sent because the callback isn't being kept.

https://github.com/chariotsolutions/phonegap-nfc/blob/df3afc165d5546bccbedca66ec1496bd04b36e6d/src/ios/NfcPlugin.m#L98-L101

Better behavior would be to fire the success callback when it starts, then the error if there is an error. This is fine for callbacks, but doesn't work promises. There may be breaking change in the next release.

Does anyone care that the session started callback?

fixdaddy commented 4 years ago

The failure callback for beginSession is the right place to send this. I'm working on some other iOS fixes and will try to add this too.

That's great news Don! I see that you self-assigned #363. Awesome. I am using the fix provided.

fixdaddy commented 4 years ago

Scan cancelled error is never being sent because the callback isn't being kept.

https://github.com/chariotsolutions/phonegap-nfc/blob/df3afc165d5546bccbedca66ec1496bd04b36e6d/src/ios/NfcPlugin.m#L98-L101

Better behavior would be to fire the success callback when it starts, then the error if there is an error. This is fine for callbacks, but doesn't work promises. There may be breaking change in the next release.

Does anyone care that the session started callback?

I'm sorry, Don! You lost me with this question. If it does what you wrote above "Better behavior would be to fire the success callback when it starts, then the error if there is an error.", I'm all for it.

don commented 4 years ago

@fixdaddy the next release will call the error callback when the user cancels the scan

fixdaddy commented 4 years ago

@fixdaddy the next release will call the error callback when the user cancels the scan

And when the scan times out, I guess? I think IOS reacts the same way in both cases.

don commented 4 years ago

The Ionic wrapper for nfc.beginSession doesn't work. beginSession is deprecated and replaced with scanNdef which returns a Promise. This is published in phonegap-nfc-1.1.0.

See example code in https://github.com/chariotsolutions/phonegap-nfc#nfcscanndef

Ionic wrapper will be patched soon, in the meantime you can call it with

(<any>window).nfc.scanNdef();