2019-06-12 05:20:59 +03:00
|
|
|
|
using CoreNFC;
|
|
|
|
|
using Foundation;
|
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Text.RegularExpressions;
|
|
|
|
|
|
2020-05-29 19:26:36 +03:00
|
|
|
|
namespace Bit.iOS.Core
|
2019-06-12 05:20:59 +03:00
|
|
|
|
{
|
|
|
|
|
public class NFCReaderDelegate : NFCNdefReaderSessionDelegate
|
|
|
|
|
{
|
|
|
|
|
private Regex _otpPattern = new Regex("^.*?([cbdefghijklnrtuv]{32,64})$");
|
|
|
|
|
private Action<bool, string> _callback;
|
|
|
|
|
|
|
|
|
|
public NFCReaderDelegate(Action<bool, string> callback)
|
|
|
|
|
{
|
|
|
|
|
_callback = callback;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public override void DidDetect(NFCNdefReaderSession session, NFCNdefMessage[] messages)
|
|
|
|
|
{
|
|
|
|
|
var results = new List<string>();
|
2020-03-28 16:16:28 +03:00
|
|
|
|
foreach (var message in messages)
|
2019-06-12 05:20:59 +03:00
|
|
|
|
{
|
2020-03-28 16:16:28 +03:00
|
|
|
|
foreach (var record in message.Records)
|
2019-06-12 05:20:59 +03:00
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
results.Add(new NSString(record.Payload, NSStringEncoding.UTF8));
|
|
|
|
|
}
|
|
|
|
|
catch { }
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-03-28 16:16:28 +03:00
|
|
|
|
foreach (var result in results)
|
2019-06-12 05:20:59 +03:00
|
|
|
|
{
|
|
|
|
|
var matches = _otpPattern.Matches(result);
|
2020-03-28 16:16:28 +03:00
|
|
|
|
if (matches.Count > 0 && matches[0].Groups.Count > 1)
|
2019-06-12 05:20:59 +03:00
|
|
|
|
{
|
|
|
|
|
var otp = matches[0].Groups[1].ToString();
|
|
|
|
|
_callback.Invoke(true, otp);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
_callback.Invoke(false, "No tags were read.");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public override void DidInvalidate(NFCNdefReaderSession session, NSError error)
|
|
|
|
|
{
|
|
|
|
|
_callback.Invoke(false, error?.LocalizedDescription);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|