I am using the UIAccelerotmeterDelegate
method accelerometer:didAccelerate:
but recently that method has been deprecated in iOS 5.0. So what is the alternative way to get the accelerometer data? The documentation does not mention the alternative we are supposed to use.
Direct access to the Apple Watch sensors (which include the accelerometer) is not possible. As always, if this is something you'd like, please file a request for it at https://bugreport.apple.com. Save this answer. Show activity on this post.
All iOS devices have a three-axis accelerometer, which delivers acceleration values in each of the three axes shown in Figure 1. The values reported by the accelerometers are measured in increments of the gravitational acceleration, with the value 1.
Tap Receiver to check the audio, Vibration to try the vibrating feature, or Sensor to test the accelerometer and other sensors. You can also tap Touch and then move your finger around the screen to test the touch screen, or tap Low Frequency to test low-frequency sounds.
The iPhone is equipped with accurate accelerometer and gyroscope hardware.
Here is a useful sample code I found for CoreMotion from this link.
@interface ViewController ()
@property (nonatomic, strong) CMMotionManager *motionManager;
@property (nonatomic, strong) IBOutlet UILabel *xAxis;
@property (nonatomic, strong) IBOutlet UILabel *yAxis;
@property (nonatomic, strong) IBOutlet UILabel *zAxis;
@end
@implementation ViewController
- (void)viewDidLoad
{
[super viewDidLoad];
self.motionManager = [[CMMotionManager alloc] init];
self.motionManager.accelerometerUpdateInterval = 1;
if ([self.motionManager isAccelerometerAvailable])
{
NSOperationQueue *queue = [[NSOperationQueue alloc] init];
[self.motionManager startAccelerometerUpdatesToQueue:queue withHandler:^(CMAccelerometerData *accelerometerData, NSError *error) {
dispatch_async(dispatch_get_main_queue(), ^{
self.xAxis.text = [NSString stringWithFormat:@"%.2f",accelerometerData.acceleration.x];
self.yAxis.text = [NSString stringWithFormat:@"%.2f",accelerometerData.acceleration.y];
self.zAxis.text = [NSString stringWithFormat:@"%.2f",accelerometerData.acceleration.z];
});
}];
} else
NSLog(@"not active");
}
@end
You should use the Core Motion framework (introduced in iOS 4.0) as a substitue. Create an instance of CMMotionManager
and tell it to startAccelerometerUpdatesToQueue:withHandler:
, passing it an NSOperationQueue
and a block that will be executed on the specified queue whenever new accelerometer data is available.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With