16.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):
- (BOOL) application:(UIApplication *)application
didFinishLaunchingWithOptions:(NSDictionary *)launchOptions{
CMMotionManager *motionManager = [[CMMotionManager alloc] init];
if ([motionManager isGyroAvailable]){
NSLog(@"Gryro is available.");
} else {
NSLog(@"Gyro is not available.");
}
if ([motionManager isGyroActive]){
NSLog(@"Gryo is active.");
} else {
NSLog(@"Gryo is not active.");
}
self.window = [[UIWindow alloc] initWithFrame:
[[UIScreen mainScreen] bounds]];
self.window.backgroundColor = [UIColor whiteColor];
[self.window makeKeyAndVisible];
return YES;
}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 the same code on the original iPad, you will get the same results as you get from iOS Simulator. However, if you run this code on an iOS device with a gyroscope, such as the iPhone 4 or iPad 2, the results could be different:
Gyro is available. Gyro is not active. ...
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