Iphone 如何检查设备上是否存在陀螺仪?

Iphone 如何检查设备上是否存在陀螺仪?,iphone,ios,gyroscope,Iphone,Ios,Gyroscope,只是想知道我是否可以检查设备(iPhone、iPad、iPod,即iOS设备)是否有陀螺仪 - (BOOL) isGyroscopeAvailable { #ifdef __IPHONE_4_0 CMMotionManager *motionManager = [[CMMotionManager alloc] init]; BOOL gyroAvailable = motionManager.gyroAvailable; [motionManager release];

只是想知道我是否可以检查设备(iPhone、iPad、iPod,即iOS设备)是否有陀螺仪

- (BOOL) isGyroscopeAvailable
{
#ifdef __IPHONE_4_0
    CMMotionManager *motionManager = [[CMMotionManager alloc] init];
    BOOL gyroAvailable = motionManager.gyroAvailable;
    [motionManager release];
    return gyroAvailable;
#else
    return NO;
#endif

}
另请参阅我的此博客,了解您可以检查iOS设备中的不同功能

CoreMotion的运动管理器类内置了一个属性,用于检查硬件可用性。Saurabh的方法要求你在每次发布带有陀螺仪的新设备(iPad2等)时更新你的应用程序。以下是使用Apple documented属性检查陀螺仪可用性的示例代码:

CMMotionManager *motionManager = [[[CMMotionManager alloc] init] autorelease];

if (motionManager.gyroAvailable)
{
    motionManager.deviceMotionUpdateInterval = 1.0/60.0;
    [motionManager startDeviceMotionUpdates];
}

更多信息请参见。

我相信@Saurabh和@Andrew Theis的答案只部分正确

这是一个更完整的解决方案:

- (BOOL) isGyroscopeAvailable
{
// If the iOS Deployment Target is greater than 4.0, then you
// can access the gyroAvailable property of CMMotionManager
#if __IPHONE_OS_VERSION_MIN_REQUIRED >= __IPHONE_4_0
    CMMotionManager *motionManager = [[CMMotionManager alloc] init];
    BOOL gyroAvailable = motionManager.gyroAvailable;
    [motionManager release];
    return gyroAvailable;
// Otherwise, if you are supporting iOS versions < 4.0, you must check the
// the device's iOS version number before accessing gyroAvailable
#else
    // Gyro wasn't available on any devices with iOS < 4.0
    if ( SYSTEM_VERSION_LESS_THAN(@"4.0") )
        return NO;
    else
    {
        CMMotionManager *motionManager = [[CMMotionManager alloc] init];
        BOOL gyroAvailable = motionManager.gyroAvailable;
        [motionManager release];
        return gyroAvailable;
    }
#endif
}
-(BOOL)是否可用
{
//如果iOS部署目标大于4.0,则
//可以访问CMMotionManager的可用属性
#如果需要IPHONE操作系统版本最低版本>=\uiPhone 4\u0
CMMotionManager*motionManager=[[CMMotionManager alloc]init];
BOOL gyroAvailable=motionManager.gyroAvailable;
[motionManager发布];
返回陀螺仪可用;
//否则,如果您支持iOS版本<4.0,则必须检查
//访问前设备的iOS版本号可用
#否则
//Gyro在iOS<4.0的任何设备上都不可用
如果(系统版本小于(@“4.0”))
返回否;
其他的
{
CMMotionManager*motionManager=[[CMMotionManager alloc]init];
BOOL gyroAvailable=motionManager.gyroAvailable;
[motionManager发布];
返回陀螺仪可用;
}
#恩迪夫
}

在中定义了
SYSTEM\u VERSION\u LESS\u THAN()

在这里使用ifdef有什么好处?@jonsibley CMMotionManager仅在iPhone os 4上可用。如果我们尝试在早期操作系统上使用它,它将无法编译。我相信iPhone\u 4\u 0只是一个定义的常量。这样做的正确方法似乎是使用
\uIphone\uOS\uVersion\uMin\uRequired>=40000
(根据这个StackOverflow问题:)我完全被这页上的所有答案弄糊涂了@Jonsible方法“gyroAvailable”仅在IOS4+中可用,这是真的吗?