21.2. Detecting the Availability of a Gyroscope
Problem
You want to find out whether the current iOS device that is running your program has gyroscope hardware available.
Solution
Use the isGyroAvailable
method of an instance of CMMotionManager to detect the
gyroscope hardware. The isGyroActive method is also available if you
want to detect whether the gyroscope hardware is currently sending
updates to your program (in other words, whether it is active):
#import "AppDelegate.h"#import <CoreMotion/CoreMotion.h>@implementationAppDelegate-(BOOL)application:(UIApplication*)applicationdidFinishLaunchingWithOptions:(NSDictionary*)launchOptions{CMMotionManager*motionManager=[[CMMotionManageralloc]init];if([motionManagerisGyroAvailable]){NSLog(@"Gryro is available.");}else{NSLog(@"Gyro is not available.");}if([motionManagerisGyroActive]){NSLog(@"Gryo is active.");}else{NSLog(@"Gryo is not active.");}self.window=[[UIWindowalloc]initWithFrame:[[UIScreenmainScreen]bounds]];self.window.backgroundColor=[UIColorwhiteColor];[self.windowmakeKeyAndVisible];returnYES;}
iOS Simulator does not have gyroscope simulation in place. If you run this code on the simulator, you will receive results similar to these in the console window:
Gyro is not available. Gyro is not active.
If you run this code on an iOS device with a gyroscope, such as the new iPhone, the results could be different:
Gyro is available. Gyro is not active.
Discussion
If you plan to release an application ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access