How do you detect simulator at runtime?


I’m surprised there’s not more on this topic online already, but so far my searches have come-up empty. Here’s the question:

How do you detect if a NativeScript app is running on a iOS/Android simulator?

In my case, I have some code in my app I only want to run when on an actual device (development or production). Is there any way to detect this condition?


Hey Todd!

The entire point of emulators is to be as close to physical devices as possible. In Android you could check for certain system properties, but they vary between different Android environments.

Is there anything in particular that you are trying to prevent when the app’s ran on emulators?


I just ran into this requirement for iOS because the nativescript-secure-storage and nativescript-email plugins can’t run on a Simulator so I wanted to provide a nice fallback for both.

In the past you were able to read the device name and it contained “Simulator” on iOS, but recently that changed to reporting the host name (the name of your Mac).

Many SO answers advise to use a macro, but we can’t do that in JS. However, the following code works fine and is compatible with all iOS versions: