How can I check if a gyroscope is present on the device?

Just wondering, can I check if the device (iPhone, iPad, iPod ie iOS) has a gyroscope?

+6
source share
3 answers
- (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 blog post to find out that you can check out the various features on iOS devices http://www.makebetterthings.com/blogs/iphone/check-ios-device-capabilities/

+13
source

The CoreMotion engine management class has a feature built-in to check for hardware availability. The Saurabh method will require you to update your application every time a new device with a gyroscope is released (iPad 2, etc.). Here's a sample code using Apple's documented property to check the availability of the gyro:

 CMMotionManager *motionManager = [[[CMMotionManager alloc] init] autorelease]; if (motionManager.gyroAvailable) { motionManager.deviceMotionUpdateInterval = 1.0/60.0; [motionManager startDeviceMotionUpdates]; } 

See the documentation for more details.

+3
source

I believe that 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 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 SYSTEM_VERSION_LESS_THAN() is defined in fooobar.com/questions/8514 / ....

+1
source

Source: https://habr.com/ru/post/889646/


All Articles