Wednesday, October 29, 2014

Get up and running with the Xamarin Android Player (and Google Play Services)

At Evolve 2014, Xamarin announced their very own Android simulator to help take some of the pain out of Android development. 

It’s hardware accelerated on both Intel and AMD platforms, runs on Windows and OSX, and it integrates very nicely with Xamarin Studio.  You no longer have to open your emulator of choice first and hope your dev environment finds it – Xamarin Studio will actually start it for you if its not already running.

But I’ll note here – you will need an active Xamarin Android account in order to run it (sorry Java developers).

It is currently in Alpha but it’s been working beautifully for me so far – grab the alpha here:

http://xamarin.com/android-player

I won’t bother walking you through the installation steps, all you need to do is download and run the installer.  And then download the images for the Xamarin Android Player to use (at the time of writing there are two Nexus 4 images).

download

It’s fairly straight forward, and there’s a great guide here:

http://developer.xamarin.com/guides/android/getting_started/installation/android-player/

I’ve personally installed it on a Windows 8.1 64 machine and two Macs running OSX 10.9.5 & 10.10.  All three installed and ran without any issues.

running

Once you’re up and running, you may see some some cryptic error messages when trying to deploy your app to the Xamarin Android Player.

First, close everything down, restart Xamarin Studio and try again.  If that still doesn’t work, you may need to update the Android SDK Tools from the Android SDK Manager.  I have one Mac working with 23.0.2 and 2nd with 23.0.5.  Anything older may not work.

SDK

Google Play Services

Finally, you’re probably going to want to install Google Services on the Xamarin Android Player.  This will allow you to test your applications that make use of the Google Maps API. 

To get the Google Services running and enable mapping support, you’re going to install a Google Apps package (GApps).  And it’s very important you install the correct package for the version of Android your running in the Android Player. I found RootzWiki.com has the best descriptions for the app packages:

http://wiki.rootzwiki.com/Google_Apps#Universal_Packages_2

For the Nexus 4 (Jelly Bean) image you’ll want to install 20121011 

Unfortunately RootzWiki doesn’t currently have a KitKit package so you can grab it from wiki.cyanogenmod.org:

http://wiki.cyanogenmod.org/w/Google_Apps#gappsCM11

for the Nexus 4 (KitKit) image use CM 11 20140606

 

Make sure you download the Zip file(s) intact, Safari on Mac OS likes to automatically extract zip files so you may need to adjust your settings.

Safari > preferences > General > uncheck "Open safe files after downloading"

install

Once you’ve downloaded the zip files for the respective emulator images, it’s simply a matter of starting the emulator and dragging the zip file onto the Xamarin Android Player.

reboot

After you drag the corresponding zip file onto the running Xamarin Android Player, it will reboot and take 20+ seconds to restart.

The final step is to update your Google apps, this will help to make sure you have the latest version of the Google Play Services installed.  To do this, you’ll want to open the newly installed Google Play Store app, log in with a valid Google account, and update any installed Google applications.  You may see some errors popping up saying that Google play needs to be re-installed.  You can ignore them, they’ll stop once you have everything up to date.

 

google-account

update

 

 

And now you’re all set to use Google Play Services including Maps on the Xamarin Android Player.

You’ll of course need to follow the above steps for both Android images and you’ll likely need different zip file packages corresponding to the correct version of Android for future Xamarin Android Player Images.

maps

Bonus Link:

If you want maximum performance but you don’t want to remove Hyper-V on your Windows machine – see Scott Hanselman’s blog on adding Hyper-V boot options on Windows 8.1:

http://www.hanselman.com/blog/SwitchEasilyBetweenVirtualBoxAndHyperVWithABCDEditBootEntryInWindows81.aspx

*Follow up – Jan 20th, 2015

I just tested this out with the new emulator images available with build 0.2.5 and everything seems to be working as expected on both Windows and OSX

Wednesday, October 15, 2014

Getting Permission to Access User Location in iOS8 with Xamarin iOS

If you’ve been developing location aware applications for iOS for a while you know that you could simply attempt to access location data in your code.  And the first time your app was run on a device, it would ask the user for permission, hopefully they say yes, and things are easy.

There are a few additional steps now in iOS 8.

The first thing you need to do is add one or two keys to your applications info.plist.  You can do this by hand with a text editor or click on the info.plist files and use the “Source” view from within Xamarin Studio.

If you’re just using location data in a normal application, the key is:

NSLocationWhenInUseUsageDescription which is a string type.  The string value is the message that will be displayed to the user the first time your application attempts to receive location data. 

Alternatively, if you’re application is background enabled for location data, you would use:

NSLocationAlwaysUsageDescription which again has a string value.

The idea with these keys, is that you can now describe to your users why you require location information.  Alternatively, you can leave the values blank to show a generic popup message.

The new keys in your info.plist would look like this:

<key>NSLocationWhenInUseUsageDescription</key>
<string>In order to track your running routes</string>

or for a background enabled app without a custom message:

<key>NSLocationAlwaysUsageDescription</key>
<string></string>

 

infop

 

Additionally, in your code you now need to specifically request authorization by calling a method defined on an instance of the LocationManager class.  RequestWhenInUseAuthorization () for standard applications and RequestAlwaysAuthorization () for background enabled apps.

 

1 if (CLLocationManager.LocationServicesEnabled)
2 {
3 LocMgr = new CLLocationManager();
4
5 ...
6
7 // request permission
8 LocMgr.RequestWhenInUseAuthorization ();
9 // Start location updates
10 LocMgr.StartUpdatingLocation ();
11
12 ...
13 }

permission