Android
iOS
 

Environment Checks

This recipe shows how you can make various environment checks from your code to handle different runtime environments.

Check Simulator vs Device

You can detect whether you are running on the simulator or device by looking up the value of the ObjCRuntime.Runtime.Arch field.   If the value is ARCH.Device, you are running on the physical hardware, otherwise you are running on the simulator.

 

Check your Xamarin.iOS Version

The version of Xamarin.iOS is stored in the field MonoTouch.Constants.Version. This is a string, you can turn this into a Version object with code like this:

Version version = new Version (MonoTouch.Constants.Version);
if (version > new Version (5,3)){
   // Code that uses features from MonoTouch 5.3
}

Detecting the GC Engine

Xamarin.iOS applications can run with either the SGen Garbage Collector or the Boehm Garbage Collector. Your application can detect at runtime which GC is in use by checking the value of the System.GC.MaxGeneration property. If the value is zero, your code is running under Boehm, otherwise it is running under SGen.

 

if (GC.MaxGeneration == 0)
    return "Using Boehm GC";
else
    return "Using SGen";