Xamarin Contacts API
Our goal at Xamarin is to make cross-platform mobile development as easy as possible. As part of that we’ve created Xamarin.Mobile, a library that runs on iOS, Android and Windows Phone 7 and abstracts commonly-used APIs to reduce developer effort in supporting multiple platforms.
Sometimes, when developing a new abstract API, we have the opportunity to design an API more elegant and beautiful than anything offered by the underlying platforms. In the last couple of weeks, we created a new addressbook API that we think you will love using.
The following sample shows how to query the address book for people’s whose last name is “Smith” and extract some common fields:
var book = new AddressBook (this) {
PreferContactAggregation = true
};
foreach (Contact c in book.Where (c => c.LastName == "Smith")) {
print (c.DisplayName);
foreach (Phone p in c.Phones)
print ("Phone: " + p.Number);
foreach (Email e in c.Emails)
print ("Email: " + e.Address);
}As you can see, the query is expressed in a simple LINQ statement which works efficiently across all three major platforms, and the code is succinct and readable.
You will also notice that we expose some simple properties for each Contact (DisplayName, Emails, Phones). These properties are available on all platforms. See the API documentation for a complete list.
Comparison to Android and iOS APIs
It’s interesting to compare the code snippet above with both the Android/Java version and the equivalent iOS/Objective-C version.
In the case of the native Android APIs, you will notice that Android requires developer to use an ad-hoc query system based on passing arrays with special keys and iterating over a ContentResolver. Writing and debugging these statements is not a lot of fun. We think everyone will agree our API is a lot friendlier.
On iOS API is a little simpler as it does not use a query system, and instead you must express the intent with more code and do more bookkeeping.
The Magic Behind Xamarin.Contacts
If you look closely, the Addressbook class implements the IQueryable interface which is the gateway to turn C# expressions into fine-tuned queries for the underlying Android query system, the iOS addresbook APIs, or the contacts on Windows Phone 7.
The magic of Xamarin.Contacts is that it maps to the most efficient implementation on each platform, wheverever possible. On Android in particular, searching for a contact is as fast as using the native ContentResolver system — but much simpler. You can use Where, Select, Count, Skip, Take, Any, First, Single, OrderBy and OrderByDescending operations knowing that Xamarin.Contacts will transform these into native query elements. For any other operations, we gracefully degrade to LINQ to Objects.
On iOS and on Windows Phone, we convert to objects and run LINQ to Objects.
Availability
You can start using Xamarin.Contacts today to simplify your app. Download it from the Xamarin.Mobile web page, and be sure to check out the API documentation for more details.
2 Comments
Excellent! Now my Path clone will be able to silently upload entire address books in only 3 lines of code!
Nice work! Very effective and elegant facade. Looking forward to what the Xamarin team does with notifications.