Xamarin iOS NFC Session is invalidated unexpectedly - xamarin.forms

Have a Xamarin forms project and trying to hook in NFC reading to the app. Currently plumbing in the iOS native side of things. I've setup all the provisioning and options on the apple portal side of things and i've added the following to the entitlements:
<dict>
<key>com.apple.developer.nfc.readersession.formats</key>
<array>
<string>NDEF</string>
</array>
</dict>
Also added to the info.plist:
<key>NFCReaderUsageDescription</key>
<string>NFC tag to read NDEF messages into the application</string>
The code i've got for my native dependency for iOS is as follows:
[assembly: Dependency(typeof(RFIDScannerHelper))]
namespace MyProject.Mobile.Platform.iOS
{
public class RFIDScannerHelper : IRFIDScannerHelper
{
public bool hasRFID()
{
return true;
}
NFCNdefReaderSession Session;
public void ScanRFID(Action<string> act, VisualElement el)
{
NFChecker nfchecker = new NFChecker();
Session = new NFCNdefReaderSession(nfchecker, null, false);
Session?.BeginSession();
}
}
public class NFChecker : NSObject, INFCNdefReaderSessionDelegate
{
public Action<string> nfcFoundAction;
public void DidDetect(NFCNdefReaderSession session, NFCNdefMessage[] messages)
{
foreach (NFCNdefMessage msg in messages)
{
if (msg.Records.Count() > 0)
{
nfcFoundAction.Invoke(new NSString(msg.Records[0].Payload, NSStringEncoding.UTF8));
}
}
}
public void DidInvalidate(NFCNdefReaderSession session, NSError error)
{
var readerError = (NFCReaderError)(long)error.Code;
if (readerError != NFCReaderError.ReaderSessionInvalidationErrorFirstNDEFTagRead &&
readerError != NFCReaderError.ReaderSessionInvalidationErrorUserCanceled)
{
}
}
}
}
When this runs it all seems to fire correctly but on start of session it goes straight to the DidInvalidate method in the ReaderDelegate and the error says "Session is invalidated unexpectedly".
Can anyone tell me what I could be missing out?
UPDATE
I've also tried the xamarin provided sample here. But I also receive the exact same error "Session is invalidated unexpectedly". I've mucked around with our provisioning but no combination changes this error. Has anyone even got the xamarin sample to work?

#matt, it's a Visual Studio 2019 error. You should select the Entitlements.plist in your project settings, but you are not able because the "entry" where you should insert the path is always disabled. I have reported the problem
https://developercommunity.visualstudio.com/content/problem/752711/xamarinios-i-cant-set-entitlements.html

Related

xamarin forms android, Font asset not found sans-serif-medium when switching to AppShell

I am attempting to code an App in Xamarin Forms.
Upon startup, if the app is logged in the the user will be taken to the main shell with flyouts etc. If they are not logged in, then they will be taken to the signup/login page.
I have the following code in my App.xaml.cs
public App()
{
Instance = this;
InitializeComponent();
AssessStartupPage();
}
public void AssessStartupPage()
{
if (App.LoggedInUser == null)
{
SwitchToSignupPage_OTP();
return;
}
SwitchToShell();
}
public void SwitchToSignupPage_OTP() => SwitchMainView(new SignupPage_OTP());
public void SwitchToShell() => SwitchMainView(new AppShell());
When I attempt to switch to the shell from another Page (by calling App.Instance.SwitchToShell) the following exception is thrown:
Java.Lang.RuntimeException
Font asset not found sans-serif-medium
However if I change the AssessStartupPage() function which is called on startup to this:
public void AssessStartupPage()
{
SwitchToShell();
return;
}
The shell loads correctly at startup. However I need to be able to have the Signup page load at start if the user is not signed in and therefore need to be able to switch to the shell once signup is complete.
I have updated all the xamarin forms and xamarin essential libraries to the latest stable version but nothing seems to help.
The problem doesn't occur when I debug the app in UWP mode.

Filter out oidc correlation failed exceptions from App Insights

I have a .net core 2.2 web app with OpenID Connect login and a baseline App Insights installation. I am able to generate an error by waiting 15 minutes on the login page and then submitting. However, I don't want these to show up in app insights as we have alerts based on number of errors. How can these errors from "signin-oidc" be filtered out either by preventing the application from sending it or having app insights ignore it?
You can use ITelemetryProcessor to abandon the unwanted messages.
First of all, you need to use visual studio to check the unique properties of the errors(from "signin-oidc"), like below(it's a trace message for example):
Then create a custom class which implements the ITelemetryProcessor. Remember that use the unique property or property combination to filter out the unwanted messages. Code like below:
namespace WebApplication1netcore4
{
public class MyTelemetryProcessor : ITelemetryProcessor
{
private ITelemetryProcessor Next { get; set; }
public MyTelemetryProcessor(ITelemetryProcessor next)
{
this.Next = next;
}
public void Process(ITelemetry telemetry)
{
ExceptionTelemetry err1= telemetry as ExceptionTelemetry;
// you can also use combinations of properties by using && or || operator
if (err1!= null && err1.Context.Properties.Keys.Contains("the unique property of that error"))
{
if (err1.Context.Properties["unique property"] == "the property value")
{
//return means abandon this error message which has the specified property value
return;
}
}
if (err1 == null)
{
this.Next.Process(telemetry);
}
if (err1 != null)
{
this.Next.Process(err1);
}
}
}
}
Then in the Startup.cs -> ConfigureServices() method, add the following code:
services.AddApplicationInsightsTelemetry();
services.AddApplicationInsightsTelemetryProcessor<MyTelemetryProcessor>();

How can I get NavigationService and navigate from a backdoor method?

I'm trying to navigate from a backdoor so that I can bypass a certain screen when running my UITests.
Here's my code:
AppDelegate.cs
[Export("bypassInitialGuidePage:")]
public NSString BypassInitialGuidePage(NSString noValue)
{
BackdoorHelpers.BypassInitialGuidePage();
return new NSString();
}
MainActivity.cs
[Export("BypassInitialGuidePage")]
public void BypassInitialGuidePage() => BackdoorHelpers.BypassInitialGuidePage();
BackdoorHelpers.cs
public static class BackdoorHelpers
{
#if DEBUG
public static void BypassInitialGuidePage()
{
new SettingsService().InitialGuideDone = true;
// Add navigation logic here
}
#endif
}
The BypassInitialGuidePage() method is being called successfully from the tests. However, I need help with getting the NavigationService so I can navigate away from current page.
I'm using Prism.Forms v7.1.0.431 and I've tried the solutions in https://github.com/PrismLibrary/Prism/issues/1032 but they are not working with the latest version.

How to dismiss a keyboard in Xamarin Forms

What I'm looking for is a way to dismiss the keyboard while maintaining focus on the Entry field so an external keyboard can be used without giving up all that screen real estate.
I can't seem to implement the code in How to dismiss keyboard on button press in Xamarin Forms I'm calling it in the OnFocused of an Entry field.
Android.App.Application.Context as Activity;
is returning null Without the "as activity it works just fine, but I can't run
.CurrentFocus?.WindowToken;
on a Context.
This is what my code looks like.
[assembly: Dependency (typeof (KeyboardInteractions))] namespace namespace.Droid
{
public class KeyboardInteractions : IKeyboardInteractions
{
public void HideKeyboard()
{
var activity = Android.App.Application.Context as Activity;
var token = activity?.CurrentFocus?.WindowToken;
var i = (InputMethodManager)Android.App.Application.Context.GetSystemService(Context.InputMethodService);
if (token != null) {
i.HideSoftInputFromWindow(token, 0);
}
}
}
}
Use the CurrentActivityPlugin.
Once you have the plugin setup you can use it like this :
var activity = CrossCurrentActivity.Current?.Activity;
In case of queries feel free to revert.

How to setup PUB/SUB NetMQ in Xamarin Forms

I have a Windows Services running as a Publisher and I am trying to setup Xamarin Forms as the Subscriber. The code below works just fine in a Console App or LinqPad, but when copied and pasted into Xamarin Forms, the SubscriberSocket just does not respond to messages from the server.
Do you know how to wire this up?
I am using NetMQ v 4.0.0.1
public partial class MainPage : ContentPage
{
public MainPage()
{
InitializeComponent();
Task.Run(() => StartPubSubSocketSubscriber());
}
private void StartPubSubSocketSubscriber()
{
string topic = "TopicA";
using (var subSocket = new SubscriberSocket())
{
subSocket.Options.ReceiveHighWatermark = 1000;
subSocket.Connect("tcp://192.168.30.120:5556");
subSocket.Subscribe(topic);
while (true)
{
string messageTopicReceived = subSocket.ReceiveFrameString();
string messageReceived = subSocket.ReceiveFrameString();
Device.BeginInvokeOnMainThread(() =>
{
label.Text = messageReceived;
});
}
}
}
}
I also tried starting the background thread with Task.Factory.StartNew(() => StartPubSubSocketSubscriber(), TaskCreationOptions.LongRunning); but it is just as unresponsive to messages from the publisher.
Thank you.
PS.: removed subSocket.Connect("tcp://localhost:5556");
The fix for this was a 2 step process:
The SubscriberSocket was incorrectly pointing to localhost. An understandable mistake since the emulator runs on the same machine as the server application. Make sure the Subscriber has the explicit IP address when running on a virtual environment or another device.
The issue with SubscriberSocket not responding was actually on a server. I had set it up with pubSocket.Bind("tcp://localhost:5556");, once I changed it to pubSocket.Bind("tcp://*:5556"); the SubscriberSocket started responding. This is an error in documentation.
The hint to the solution came from the NetMQ github issue tracking:
https://github.com/zeromq/netmq/issues/747

Resources