December 2012
Intermediate to advanced
834 pages
27h
English
You want to ask iOS to send accelerometer data to your application.
Use the startAccelerometerUpdatesToQueue:withHandler:
instance method of CMMotionManager.
Here is the header file of a view controller that utilizes CMMotionManager to get accelerometer
updates:
#import <UIKit/UIKit.h>#import <CoreMotion/CoreMotion.h>@interfaceRetrieving_Accelerometer_DataViewController:UIViewController@property(nonatomic,strong)CMMotionManager*motionManager;@end
We will now implement our view controller and take advantage of
the startAccelerometerUpdatesToQueue:withHandler:
method of the CMMotionManager
class:
#import "Retrieving_Accelerometer_DataViewController.h"@implementationRetrieving_Accelerometer_DataViewController-(void)viewDidLoad{[superviewDidLoad];self.motionManager=[[CMMotionManageralloc]init];if([self.motionManagerisAccelerometerAvailable]){NSOperationQueue*queue=[[NSOperationQueuealloc]init];[self.motionManagerstartAccelerometerUpdatesToQueue:queuewithHandler:^(CMAccelerometerData*accelerometerData,NSError*error){NSLog(@"X = %.04f, Y = %.04f, Z = %.04f",accelerometerData.acceleration.x,accelerometerData.acceleration.y,accelerometerData.acceleration.z);}];}else{NSLog(@"Accelerometer is not available.");}}-(BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation{returnYES;}@end
The accelerometer reports three-dimensional data (three axes) that ...
Read now
Unlock full access