Reputation: 1193
I want to enable a logger service only when I'm running on a real android\iOS device. Is it possible to know if I'm running with a android\iOS simulator or a real device at run time in core project level?
Upvotes: 0
Views: 101
Reputation: 2604
Create an Interface in the Core project:
public interface IDevicePlatform
{
bool IsSimulator();
}
iOS DependencyService Registration/Implementation:
public class Platform_iOS : IDevicePlatform
{
public IsSimulator()
{
return ObjCRuntime.Runtime.Arch == ObjCRuntime.Arch.SIMULATOR;
}
}
Android DependencyService Registration/Implementation:
public class Platform_Android : IDevicePlatform
{
public bool IsSimulator()
{
if (Build.Fingerprint != null)
{
if (Build.Fingerprint.Contains("vbox") || Build.Fingerprint.Contains("generic") || Build.Fingerprint.Contains("vsemu"))
return true;
}
return false;
}
}
Call it in Core as:
bool isSimulator = DependencyService.Get<IDevicePlatform>().IsSimulator();
if(isSimulator)
{
//You are running on the Simulator
}
else
{
//You are running on the real device
}
NOTE: iOS implementation is straight forward while, Android is a wide world, so, apart from above simple android implementation, also have a look at SushiHangover's Answer
Upvotes: 2