July 25, 2013

Xamarin.Mobile: new, improved and open-sourced

By

Today we’re excited to announce updated releases of three cross-platform components: Xamarin.Mobile, Xamarin.Social, and Xamarin.Auth; and also that we are open-sourcing the code on github.

These components are available on the Xamarin Component Store and let you:

  • Xamarin.Mobileaccess hardware features like the camera in a few simple lines of code,
  • access built-in data like contacts in an easy, cross-platform way,
  • quickly add social media integration like Facebook, Twitter, Flickr and more,
  • build secure custom authentication using an existing OAuth implementation,

and much more! You can read about each individual component on the store: Xamarin.Mobile, Xamarin.Auth and Xamarin.Social.

To show just how easy it is to build apps with these components, we’ve put together a quick sample app called SoMA which lets you take pictures then share them on social media along with a Google map link your current location. It uses Xamarin.Mobile to access the camera and location services, and Xamarin.Social (which piggybacks onto Xamarin.Auth) to integrate with Facebook, Twitter, Flickr, App.net… all in just a few dozen lines of code (on github).

Here are some screenshots – we provide a custom data entry form when required, but also integrate with native features (like Twitter on iOS) when available.
Social Mobile Auth sample
To demonstrate how Xamarin.Mobile simplifies your code, this snippet . This API works on Xamarin.iOS, Xamarin.Android, Windows Phone and also Windows 8!

var locator = new Geolocator { DesiredAccuracy = 50 };
//            new Geolocator (this) { ... }; on Android
var position = await locator.GetPositionAsync (timeout: 10000);
Console.WriteLine ("Position Latitude: {0}", position.Latitude);
Console.WriteLine ("Position Longitude: {0}", position.Longitude);
location = string.Format("{0},{1}", position.Latitude, position.Longitude);
LocationText.Text = location; // on UI

Xamarin.Social makes it just as easy to post the picture to Facebook or other social network, with this snippet for iOS or Android:

// 1. Create the service
var facebook = new FacebookService {
    ClientId = "YOUR_CLIENT_ID",
    RedirectUrl = new System.Uri ("http://www.facebook.com/connect/login_success.html")
};
// 2. Create an item to share
var item = new Item { Text = "Xamarin.Social rocks!" };
item.Images.Add(new ImageData(fileName));
// 3. Present the UI on Android
var shareIntent = service.GetShareUI (this, item, result => {
    // result lets you know if the user shared the item or canceled
    Console.WriteLine(service.Title + " shared");
});
StartActivity (shareIntent);
// 3. Present the UI on iOS
var shareController = facebook.GetShareUI (item, result => {
    // result lets you know if the user shared the item or canceled
    DismissViewController (true, null);
});
PresentViewController (shareController, true, null); 

One more thing…

In case you missed it at the top, we’re also open-sourcing these components. You can find the code on github for Xamarin.Social, Xamarin.Mobile and Xamarin.Auth.

This is great news for two reasons: first, you can see how we’ve approached exposing platform-specific features (such as geolocation) in a cross-platform way. Secondly, you can embrace and extend the libraries to support new hardware features or social networks that we haven’t seen, thought or heard of! Help everyone out by sending a pull-request and contributing back to the community.

We hope you enjoy using these components and exploring the code!

TwitterFacebookGoogle+LinkedInEmail