我想在我在真正的android \ iOS设备上运行时启用记录器服务。 是否有可能知道我是否在核心项目级别的运行时使用android \ iOS模拟器或真实设备运行?
答案 0 :(得分:2)
在Core项目中创建接口:
public interface IDevicePlatform
{
bool IsSimulator();
}
iOS DependencyService 注册/实施:
public class Platform_iOS : IDevicePlatform
{
public IsSimulator()
{
return ObjCRuntime.Runtime.Arch == ObjCRuntime.Arch.SIMULATOR;
}
}
Android DependencyService 注册/实施:
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;
}
}
将其称为Core:
bool isSimulator = DependencyService.Get<IDevicePlatform>().IsSimulator();
if(isSimulator)
{
//You are running on the Simulator
}
else
{
//You are running on the real device
}
注意:iOS实现很简单,而Android是一个广阔的世界,所以,除了上面简单的Android实现,还要看看SushiHangover's Answer