Xamarin.iOS: different behaviour on device and simulator

Viewed 152

I need to detect if my code runs on Xamarin.iOS and for this reason I wrote next code:

bool isIOS()
{
   try
   {
      var assmbl = Assembly.Load("Xamarin.iOS.dll");
      if (assmbl == null) return false;

      var classType = assmbl.GetType("UIKit.UIDevice");
      if (classType == null) return false; // HERE IS THE PROBLEM !!!

      var prop = classType.GetProperty("CurrentDevice");
      if (prop == null) return false;

      var currDevice = prop.GetValue(null);
      if (currDevice == null) return false;

      var sType = currDevice.GetType();

      prop = sType.GetProperty("SystemName");
      if (prop == null) return false;

      var name = prop.GetValue(currDevice) as string;

      return (name == "iOS");
   }
   catch
   {
      return false;
   }
}

The problem is that assmbl.GetType("UIKit.UIDevice") returns correct Type on simulator and this code works as expected, but on real device it returns null.

Also if I call assmbl.GetTypes() the method returns about 1000 types on simulator and 100+ on the device.

UIDevice.CurrentDevice.SystemName works fine on both simulator and device, but I can't use it directly as the code should work in Xamarin and non-Xamarin applications.

What could be the reason of such behaviour?

0 Answers
Related