6

Just wondering can I check if the device (iPhone, iPad, iPod i.e. iOS devices) has a Gyroscope ?

Black Frog
  • 11,595
  • 1
  • 35
  • 66
user757812
  • 581
  • 1
  • 7
  • 10

3 Answers3

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

}

See also my this blog entry to know you can check for different capabilities in iOS devices http://www.makebetterthings.com/blogs/iphone/check-ios-device-capabilities/

Saurabh
  • 22,743
  • 12
  • 84
  • 133
  • What advantage does using #ifdef have here? – codeperson Jan 07 '12 at 22:35
  • 1
    @jonsibley CMMotionManager is only available on iPhone os 4 ..if we try to use it on earlier os it will not compile – Saurabh Jan 08 '12 at 09:25
  • 3
    I believe __IPHONE_4_0 is just a defined constant. It seems the proper way to do this would be using `__IPHONE_OS_VERSION_MIN_REQUIRED >= 40000` (according to this StackOverflow question: http://stackoverflow.com/questions/3955331/if-iphone-4-0-on-ipad) – codeperson Mar 24 '12 at 13:16
3

CoreMotion's motion manager class has a property built in for checking hardware availability. Saurabh's method would require you to update your app every time a new device with a gyroscope is released (iPad 2, etc). Here's sample code using the Apple documented property for checking for gyroscope availability:

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

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

See the documentation for more info.

Andrew Theis
  • 933
  • 7
  • 15
1

I believe the answers from @Saurabh and @Andrew Theis are only partially correct.

This is a more complete solution:

- (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
}

Where the SYSTEM_VERSION_LESS_THAN() is defined in this StackOverflow answer.

Community
  • 1
  • 1
codeperson
  • 8,050
  • 5
  • 32
  • 51
  • I am completely confused by looking at all these answers on this page. @jonsibley Is it true that the method "gyroAvailable" is only available in IOS4+? – ShayanK Apr 23 '12 at 11:32