Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get accelerometer data in IOS?

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.

like image 300
NSCry Avatar asked Apr 16 '12 09:04

NSCry


People also ask

How do you get the accelerometer data on Apple Watch?

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.

Is there an accelerometer in iPhone?

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.

How do I test the accelerometer on my iPhone?

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.

Does iPhone 12 have accelerometer?

The iPhone is equipped with accurate accelerometer and gyroscope hardware.


2 Answers

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
like image 119
user2431285 Avatar answered Sep 28 '22 21:09

user2431285


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.

like image 23
Ole Begemann Avatar answered Sep 28 '22 20:09

Ole Begemann