Securing Mac application with Touch ID

This Monday I got a shiny new Macbook Pro with Touchbar. There few things I don’t like about it but one thing I absolutely love is TouchID integration.

I wanted to check how easy it is to integrate with Xamarin.Mac app. Surprisingly, the APIs are exactly same as what are available on iOS, I used the code from Mike James’ blog post about securing iOS apps with TouchID


All you need is below code snippet in your Xamarin.Mac app to secure it with all new Thouch ID

LAContextReplyHandler replyHandler;

// ...

void UseTouchID()
    var contex = new LAContext();
    NSError authError;
    if (contex.CanEvaluatePolicy (LAPolicy.DeviceOwnerAuthenticationWithBiometrics, out authError))
        replyHandler = new LAContextReplyHandler((success, error) =>
            if (success)
                InvokeOnMainThread(() => ShowMessage());

    contex.EvaluatePolicy(LAPolicy.DeviceOwnerAuthenticationWithBiometrics, "authenticate", replyHandler);

Note that we do need to have a fallback mechanism for legacy Macbook Pros (i.e without Touchbar), you can download complete source code
on Github here

JSON Copy version 1.0 for Xamarin Studio

UPDATE: JSON Copy now available in Xamarin Studio’s Add-in Manager, under IDE Extensions

Few months back I published an Addin for MonoDevelop and Xamarin Studio to allow developer to paste JSON string as C# classes. The functionality was primitive and experimental, we did not have any control over the code formatting or class structure.

In v1.0 I have fixed most of those limitations in the Addin, now we have more control over how C# structured. You can find download and installation instruction here.

Here is what’s new in version 1.0:

  1. Choose between Pascal Casing and default lowercase letters
  2. Mark classes as internal or public
  3. Create Properties or Fields for JSON properties.
  4. New Preferences Panel


I would love to hear your feedback, please create an issue in Github repo or leave a comment

Paste JSON as Classes in Xamarin Studio

Update: Now compatible with Xamarin Studio/MonoDevelop 5

As a C# mobile developer I spend lot of time writing classes to serialise or deserialise JSON string when integrating apps with third-party REST APIs.Visual Studio has an amazing feature to rescue us from this tedious job, Paste JSON As Classes command, where it automatically generates the strongly typed classes for JSON string.

I was motivated to create a similar add-in for my primary development IDE, Xamarin Studio. I started writing the C# code generator with T4 templates, and quickly realised that it will take too long to finish the development with limited time I get to spend on side projects.

Luckily, I stumbled upon this awesome open source project on codeplex to generate the C# classes for JSON text. It supports typed arrays, typed objects, integers, floats, booleans, strings and nullable types. It helped me quickly put together an Add-in to paste JSON from clipboard to C# classes in Xamarin Studio



You need to follow these three easy steps to install it on our Xamarin Studio.

  1. Download the add-in package from Github
  2. Open Xamarin Studio -> Add-in Manager
  3. Click on Install from file… button and select the downloaded package to install

Once you install the add-in the Paste JSON as classes command will appear in Edit menu, that enable you to paste C# classes directly from the text in your clipboard.

The complete code for the add-in is available on the Github here, please add an issue if you find any bug or send me a pull request :)

Magic of NSLinguistic​Tagger

This evening one of my friends sent me the link of iA Writer’s video featuring their latest update to the app.

I was blown away by the syntax highlighter in edit mode, where it highlights words based on selection of parts of speech.

At first, I thought they are doing some sophisticated natural language processing, I’m not expert but supporting multiple languages sounds like a tedious/impossible job to do in an year, unless there is easy way.

Apple provides us NSLinguistic​Tagger class as part of the iOS 5 SDK, it allows us to split up natural language text and tag it with information. As per the documentation it can identify languages, scripts and stem forms of words! NSLinguistic​Tagger is quite easy to use.

We can start by creating the instance of NSLinguisticTagger with tag schemes for a language and bunch of NSLinguisticTaggerOptions, options are used to tell tagger to ignore words, white spaces, punctuations etc.

var options = NSLinguisticTaggerOptions.JoinNames 
              | NSLinguisticTaggerOptions.OmitWhitespace;

var tagger = new NSLinguisticTagger (
    NSLinguisticTagger.GetAvailableTagSchemesForLanguage ("en"), 

Once we create the instance, we will assign it a string to analyse

tagger.AnalysisString = "A quick movement of the enemy will jeopardise six gunboats";

And finally, fetch the tags with EnumerateTagsInRange method and print them out.

tagger.EnumerateTagsInRange (new NSRange (0, statement.Length), NSLinguisticTag.SchemeLexicalClass, options, TaggerEnumerator);


void TaggerEnumerator (NSString tag, NSRange tokenRange, NSRange sentenceRange, ref bool stop)
    var word = statement.Substring (tokenRange.Location,
    words.Add (new Tuple<string, string> (tag, word));

The result after processing A quick movement of the enemy will jeopardise six gunboats looks something like this:

 Adjective: quick
 Noun: movement, enemy, gunboats
 Preposition: of
 Verb: will, jeopardise
 Number: six

it even identifies the number, that’s pretty amazing!

PS: You can find the complete code here –

Modal UIAlertView (iOS 7)

It’s common practice to use NSRunLoop to show modal UIAlertView to block code execution until user input is processed in iOS.

The below code worked perfectly on previous versions of iOS (5 & 6)

int ShowModalAletView (string title, string message, params string[] buttons)
    int clicked = -1;
    var alertView = new UIAlertView (title, message, null, null, buttons);
    alertView.Show ();
    bool done = false;
    alertView.Clicked += (sender, buttonArgs) => {
        Console.WriteLine ("User clicked on {0}", buttonArgs.ButtonIndex);
        clicked = buttonArgs.ButtonIndex;
    while (clicked == -1){
        NSRunLoop.Current.RunUntil (NSDate.FromTimeIntervalSinceNow (0.5));
    return clicked;

until Apple released the iOS7, it broke few existing apps 1. Fortunately, there is a better way of implementing it with C#’s async/await

Here’s the new code taking advantage of async/await pattern to show modal UIAlertView:

Task ShowModalAletViewAsync (string title, string message, params string[] buttons)
    var alertView = new UIAlertView (title, message, null, null, buttons);
    alertView.Show ();
    var tsc = new TaskCompletionSource ();

    alertView.Clicked += (sender, buttonArgs) => {
        Console.WriteLine (“User clicked on {0}”, buttonArgs.ButtonIndex);
    return tsc.Task;

async Task PromptUser() {
    var result = await ShowModalAletViewAsync (“Alert”, “Do you want to continue?”, “Yes”, “No”); //process the result

  1. From Apple Developer Forum: