2011-06-02 161 views

回答

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

} 

也看到我这篇博客就知道,你可以在iOS设备 http://www.makebetterthings.com/blogs/iphone/check-ios-device-capabilities/

+0

使用#ifdef有什么优势? – jonsibley 2012-01-07 22:35:31

+1

@jonsibley CMMotionManager仅适用于iPhone OS 4 ..如果我们尝试在早期的操作系统上使用它,它将无法编译 – Saurabh 2012-01-08 09:25:52

+0

明白了,谢谢。 – jonsibley 2012-03-24 13:10:28

3

CoreMotion的运动经理类内置了用于检查硬件可用性属性检查不同的能力。 Saurabh的方法会要求您在每次发布带有陀螺仪的新设备(iPad 2等)时更新您的应用程序。下面是使用苹果的记录属性检查陀螺仪可用性的示例代码:

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

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

更多信息,请参见the documentation

1

我相信@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 
} 

SYSTEM_VERSION_LESS_THAN()this StackOverflow answer定义。

+0

I通过查看这个页面上的所有答案,我完全感到困惑。 @jonsibley“gyroAvailable”方法只适用于IOS4 +吗? – ShayanK 2012-04-23 11:32:07