“Cannot get libraries from incompatible component” in Xamarin Studio

I recently experienced this error when moving a project from Windows to Mac using a Git repository. In my case the problem might have been related to my .gitignore file’s rule to ignore DLLs.

Solution

If your project is using Classic APIs and the component is only available for Unified API, then just switch to the Unified APIs. If this is not the case, try closing Xamarin Studio and deleting all the files and folders in the “Components” folder in your project root folder, then just launch Xamarin Studio and it should download all the components again.

Managing user preferences in Xamarin iOS

Xamarin provides many libraries and classes for managing user data (user preferences, SQLite, serialization etc.) but the most common method used by iOS developers is the NSUserDefaults class.

Every App gets its own singleton NSUserDefaults class, to access it, use the StandardUserDefaults property:

NSUserDefaults provides its own methods to save ints, floats, doubles, dates, strings and custom data.

Storing and retrieving data

There is a helper method for each of the common data types. The following are some examples showing how to store data

Always remember to synchronize your changes with the local file, otherwise in case of a crash you might lose them

Retrieving data is as simple as storing it

Storing nulls and dates

The NSUserDefaults can only save objects derived from NSObject, so if you want to save a DateTime, it is better to convert it to an NSDate. The following example shows how to store a NSDate object and check for null values:

Final notes

It is always a good practice to have a singleton Settings class to manage all the settings in order to have all the keys in a single place and avoid bugs difficult to detect.

Scrolling an UIView when the keyboard appears with Autolayout

We have come to iOS 8 and yet there is no easy way to make your UIView scroll (for example in a form with many UITextFields) when the keyboard appears. I have created a reusable class to automatically handle the keyboard navigation between UITextField – as described in my previous post – and scrolling the UIViewController as the user passes from one UITextField to the next.

Usage

Create a UIViewController on your Storyboard as usual but instead of adding the controls directly onto it, add an UIScrollView and make it the same size as the UIViewController and inside the UIScrollView add an UIView that fills it. The inner UIView will be the container for all your controls.

Next, in your UIViewController’s code add the following code:

… and that’s it! The helper class will automatically handle the tab order for your UITextFields and the UIScrollView.

The full code and the test project are available on my GitHub repository.

Navigating through UITextFields with the iOS keyboard

TLDR; Just add the class to your project and use the code snippet below to initialize it. Add the text fields in the same order the user will see them (the tab order).

The problem

Having an interface with multiple text fields is not rare, but navigating between them is always a painful duty for the developer.
I have created a reusable class for Xamarin iOS that can be plugged anywhere with just a single line of code to manage the situation automatically.

The idea

The idea is that we create a custom UITextFieldDelegate and a list of the fields in the order they should be navigated in. As a UITextField returns (calling the ShouldReturn method on its delegate), we detect its position relative to the list and if it’s not the last control, we focus the next in the list until the last control loses focus.
Most developers have been using the “tag” property as the tab-order, so finding the next control to focus is as easy as calling the ViewWithTag method (on the field’s parent) with the current control’s tag + 1. Of course all the fields should have the same parent to use this approach.

Some code

I have created a helper method to initialize the delegate on each text field and also take care of the “ReturnKeyType” property automatically.

Usage

Just call the helper method and let it take care of the rest

That’s it!

But if you want to use the tag property for some reason, then you just initialize the delegate on the first text field and the rest will be taken care of.

Full code is available in this repository.