Jason Hartley
Jason Hartley

Reputation: 2509

Detect iOS Simulator vs. iOS Device

I am working on a project using Xamarin.iOS and I have a situation where a behavior in the simulator inexplicably is not the same on an actual device (setting the region of a mapview centers differently).

I want to be able to set a value for a variable at runtime based on whether the app is running on the simulator or a real device. How can I detect this?

Upvotes: 12

Views: 3552

Answers (2)

Mahab
Mahab

Reputation: 198

Something along this will do:

        public static bool Isiossimulator()
        {
            bool Return = false;
#if IOS
            if (DeviceInfo.DeviceType == DeviceType.Virtual)
                Return = true;
#endif
            return Return;
        }

Also make sure taking this approach is not to hide some bug.

Upvotes: 2

poupou
poupou

Reputation: 43553

You can execute different code at runtime like this:

if (ObjCRuntime.Runtime.Arch == Arch.DEVICE) {
} else {
}

But it's always good to investigate (ask around here, forums, bug reports) why the behaviour differs between the two (just to make sure it does not hide a bug that could bite you later).

Upvotes: 21

Related Questions