Getting Unique Device ID for iOS, Android and Windows with Xamarin

Over a year ago, I wrote a quick tip on getting device Unique ID for Windows and Windows Phone (8 & 7). Lately, at RedBit, we have been doing quite a bit of iOS, Android and Windows projects using Xamarin and recently put two articles up on our learnings, Cross Platform Animations using Xamarin Forms and GZip Compression with Xamarin Forms. For different reasons, we needed to use a unique ID to identify devices and getting the device ID on each platform is obviously different.

For iOS you can use the UIDevice.CurrentDevice.IdentifierForVendor which can be called from a Xamarin iOS or Xamarin Forms app.

UIKit.UIDevice.CurrentDevice.IdentifierForVendor.AsString();

For Android, again it’s a different API but note, that you need API Level 9 or higher for this to work. It is called as follows from Xamarin Android or Xamarin Forms

Android.OS.Build.Serial;

For Windows you can reference this post I wrote a while back Getting Device ID on Windows & Windows Phone.

At RedBit we have an internal library that we use and here is an excerpt of our RedBit.Mobile.Hardware class

public class Hardware
{
    private static Hardware _Default;
    public static Hardware Default
    {
        get
        {
            return _Default ?? (_Default = new Hardware());
        }
    }

    ///

    /// Gets a device unique identifier
    ///

public string DeviceId { get { return GetDeviceIdInternal(); } } ///
/// Gets a device unique identifier depending on the platform ///

/// string representing the unique id private string GetDeviceIdInternal() { var id = default(string); #if __IOS__ id = UIKit.UIDevice.CurrentDevice.IdentifierForVendor.AsString(); #elif WINDOWS_PHONE id = Windows.Phone.System.Analytics.HostInformation.PublisherHostId; #else id = Android.OS.Build.Serial; #endif return id; } }

I’ve put a sample up on GitHub that outputs the device ID for iOS, Android and Windows. Here is the output on iOS.

deviceId-ios

As always, comments welcome!

Killing Your iOS App using Xamarin

In the middle of a project where the team is building a customer app for iPad using Xamarin a requirement came up to automatically shut down the after a certain action was performed by the user.  Usually it’s recommended to not ‘kill’ your app on iOS and apps may fail certification if you do this.  Make sure to read the iOS documentation.

Now because we are doing an Enterprise Deploy and not an App Store Deploy, we were pretty safe.

I go in more detail in the post titled Exiting an iOS App using Xamarin on the Exiting an iOS App using Xamarin.

Sharing Code Part IV: Portable Class Libraries

In the last three posts, I have gone through various ways to share common code across the various Windows based platforms. So far in this series of articles in sharing code we have looked at

  1. Sharing Code Part I: Common Source File (source code download)
  2. Sharing Code Part II: Partial Classes (source code download)
  3. Sharing Code Part III: Conditional Compile (source code download)

In this final part, we’ll look at using a Portable Class Library to share the Math2 class across the different platforms instead of the C# source code file. Personally, I prefer sharing the source code file across the platforms and not sharing an assembly as it gives a little more control. Of course, if I’m building out an API or SDK for external developers to use, a Portable Class Library is the way to go. Continue reading Sharing Code Part IV: Portable Class Libraries

Sharing Code Part III: Conditional Compile

In the first part of Sharing Code series we looked at sharing a common class source file across three different platforms, and in the second part we continued with using partial classes to separate the code out. In this third part of the series, we’ll look at using conditional compiles in our code and continue working on the sample code from the previous articles. Continue reading Sharing Code Part III: Conditional Compile

Sharing Code Part II: Partial Classes

In the first part of Sharing Code series we looked at sharing a common class source file across three different platforms. Continuing the series on Sharing Code across various platforms, we’ll look at leveraging partial classes to help share code across our many apps on Windows 8, Windows Phone 7 & Windows Phone 8 using the same source code from Part I.

What is a Partial Classes?

Essentially a partial class allows you to separate a class or structure into two or more separate files. Personally, I use this technique to hide any specific platform code from other platforms.

Continue reading Sharing Code Part II: Partial Classes

Sharing Code Part I: Common Source Files

In this article, I’ll go through how to share source code files between Windows Phone 8, Windows Phone 7.1 and Windows 8.  In this sample we’ll go through how to share a Math class between two separate Windows Phone projects.

Creating the Projects

First thing is to create two Visual Studio 2012 Projects one for Windows Phone 7.1, 8 and Windows 8.

Windows Phone 7.1 Project

Create a Windows Phone Project and call it WP7_App and uncheck ‘Create Directory for solution’.

Continue reading Sharing Code Part I: Common Source Files

Sharing Code Across Platforms: Introduction

In 2006 I did a presentation on that focused on .NET Compact Framework for Desktop Developers where the core focus was on how to leverage existing code from the full desktop .NET Framework and .NET Compact Framework.

Well seven years later (yes I’ve been doing this a long time!), some of those techniques are still usable with all the different mobile platforms available and as a developer you definitely want to leverage as much code as possible.

In the next series of articles, I will go through some techniques on sharing code across different platforms including Windows Phone 7, Windows Phone 8 and Windows 8. You can use some of the techniques also on iOS and Android using Xamarin.

The following topics will be covered:

  1. Sharing Code Part I: Common Source File (source code download)
  2. Sharing Code Part II: Partial Classes (source code download)
  3. Sharing Code Part III: Conditional Compile (source code download)
  4. Sharing Code Part IV: Portable Class Libraries (source code download)

I have personally used these techniques with various products we have built plus customer work.  By the end of the series, hopefully you will have a good understanding on how to leverage your code across different platforms using C#.

Note that the samples being used are not using anything like MVVM or other frameworks as it’s something I was using my 11yr old son on using programming to solve some math homework he had and I wanted to keep it as simple as possible.

Windows Phone & HTML5 Sample App Summary

I recently wrote a four part article about how to build a simple count down timer app using HTML5 and Windows Phone.  The articles covered getting started with HTML5 and Windows Phone, multiple resolutions, using the WebBrowser control to ‘talk’ to web page and finally getting ready for Windows Phone Store certification.

Here is the result of the articles.

finalImage

Here is a list of the articles available for easy reference

  1. XBoxReveal HTML5 Windows Phone Sample
  2. XBoxReveal HTML5 Windows Phone Sample: Multiple Resolutions
  3. XBoxReveal HTML5 Windows Phone Sample: WebBrowser Control
  4. XBoxReveal HTML5 Windows Phone Sample: Store Certification

As for the source code, if you want to run it yourself you can download the #xboxreveal source code from GitHub.

Hope it was useful and please leave comments or questions!

XBoxReveal HTML5 Windows Phone Sample: Store Certification

In this final article of the XBoxReveal HTML5 Windows Phone sample, I’ll be going through some of the things you need to do as a developer to get ready for Windows Store Certification.

If you have not read the previous articles, here is a list for some background

  1. XBoxReveal HTML5 Windows Phone Sample
  2. XBoxReveal HTML5 Windows Phone Sample: Multiple Resolutions
  3. XBoxReveal HTML5 Windows Phone Sample: WebBrowser Control

Getting Ready for Store

The first thing you want to do to get ready for Windows Phone Store certification is read the App Certification Requirements for Windows Phone article on MSDN.  Lucky for the people who don’t like going through the entire document (like me 🙂 the Windows Phone SDK comes with a Store Test Kit to help go through the cert process.

wp-testkit

Continue reading XBoxReveal HTML5 Windows Phone Sample: Store Certification

XBoxReveal HTML5 Windows Phone Sample: WebBrowser Control

In this third installment of the Windows Phone HTML5 XBox Reveal sample, we’ll look at fixing the horizontal and vertical scrolling of the index.html

Here are the first articles available if you have not read them yet for some background

  1. HTML5 & Windows Phone: XBox Reveal Sample
  2. HTML5 & Windows Phone: Multiple Resolutions

Usually using CSS you would fix horizontal scrolling by setting the following meta tag in your HTML page.

<meta name="viewport" content="width=device-width, height=device-height, initial-scale=1.0, maximum-scale=1.0, user-scalable=no;" />

Unfortunately this does not work within our app. But fortunately for us, there is a fix to this but does take a little extra work.

Continue reading XBoxReveal HTML5 Windows Phone Sample: WebBrowser Control