xamarin/ios-samples

NFC Tag Reader error

acaliaro opened this issue · 11 comments

Hi
I am using the NFC TAG READER demo on an iphone 8 plus.

When I click "Scan" I have the error code 202 "Session is invalidated unexpectedly" on this method.

    public void DidInvalidate(NFCNdefReaderSession session, NSError error)
    {

        var readerError = (NFCReaderError)(long)error.Code;

        if (readerError != NFCReaderError.ReaderSessionInvalidationErrorFirstNDEFTagRead &&
            readerError != NFCReaderError.ReaderSessionInvalidationErrorUserCanceled)
        {

            InvokeOnMainThread(() =>
            {
                var alertController = UIAlertController.Create("Session Invalidated", error.LocalizedDescription, UIAlertControllerStyle.Alert);
                alertController.AddAction(UIAlertAction.Create("Ok", UIAlertActionStyle.Default, null));
                DispatchQueue.MainQueue.DispatchAsync(() =>
                {
                    this.PresentViewController(alertController, true, null);
                });
            });
        }




    }

So the version of code I see doesn't have that InvokeOnMainThread, so I fixed it with:

https://github.com/xamarin/ios-samples/pull/387/files

However, I don't have an NFC device to test things with. Can you see if that fixes your issue?

@chamons yes, there is also this problem, but it's not relative to mine. I think it's a "entitlement" problem, but I have to investigate better. It seems that the project works fine in VS4MAC but there is the problem in VS2019 for windows. I have to do some tests again

I think the problem is in "entitlements".

In VS4MAC I can use "automatic provisioning" in "info.plist" and set an entitlement in ""Firma del bundle iOS" (it's in italian...).

In VS2019, if I set "automatic provisioning" in "firma del bundle iOS", I can add the entitlement manually (always under "firma del bundle iOS")...

I don't understand this difference between VS4MAC and VS2019.... @chamons can you give me some info?

So the Entitlements.plist in your project should look something like this:

https://gist.github.com/chamons/d3c520a4a1d622e37f3536668e6d7821

with a com.apple.developer.nfc.readersession.formats key.

You can open that file in a text editor, but both IDEs should have a checkbox when opening that file to enable this.

yes, both ide have the checkbox, it's not the problem.
The problem is that in VS4MAC I can set the Entitlement in the "Firma del bundle iOS" page

image

in VS2019 it seems I can't do this. It's disabled... If I don't set this property, the "scan" button does not display the NFC popup window

emaf commented

Hi @acaliaro,
Could you report the problem about the disabled fields from Visual Studio 2019? You can find more information on how to report problems from VS here.

As a workaround, you can manually add those values to the csproj file:

  • Custom Entitlements: <CodesignEntitlements></CodesignEntitlements>
  • Custom Resource Rules: <CodesignResourceRules></CodesignResourceRules>
  • Additional Arguments: <CodesignExtraArgs></CodesignExtraArgs>

ok @emaf

do you think we can close this issue?

emaf commented

Sure!

Thanks!

Hi @acaliaro,
Could you report the problem about the disabled fields from Visual Studio 2019? You can find more information on how to report problems from VS here.

As a workaround, you can manually add those values to the csproj file:

  • Custom Entitlements: <CodesignEntitlements></CodesignEntitlements>
  • Custom Resource Rules: <CodesignResourceRules></CodesignResourceRules>
  • Additional Arguments: <CodesignExtraArgs></CodesignExtraArgs>

It seems that modifing the csproj file does not work. It is reverted to the previous situation when I reopen the solution (I have always the CustomEntitlements empty)

emaf commented

@acaliaro I've just realised you'll also need to switch to Manual Provisioning, that's the real workaround here. The problem is the Automatic Provisioning process deleting those values (which is wrong).