Free Trial

Safari Books Online is a digital library providing on-demand subscription access to thousands of learning resources.


  • Create BookmarkCreate Bookmark
  • Create Note or TagCreate Note or Tag
  • DownloadDownload
  • PrintPrint
Share this Page URL
Help

16. Core Motion > 16.2. Detecting the Availability of a Gyroscope

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

  

You are currently reading a PREVIEW of this book.

                                                                                        

Get instant access to over
$1 million worth of books and videos.

  

Start a Free Trial


  
  • Safari Books Online
  • Create BookmarkCreate Bookmark
  • Create Note or TagCreate Note or Tag
  • DownloadDownload
  • PrintPrint