Recipe: Using Acceleration to Locate “Up”

The iPhone and iPad provide three onboard sensors that measure acceleration along the device’s perpendicular axes: left/right (X), up/down (Y), and front/back (Z). These values indicate the forces affecting the device, from both gravity and user movement. You can get some neat force feedback by swinging the iPhone around your head (centripetal force) or dropping it from a tall building (freefall). Unfortunately, you might not recover that data after your iPhone becomes an expensive bit of scrap metal.

To monitor accelerometer updates, create a Core Motion manager object, set the interval for updates, and start the manager, passing in a handler block to be processed:

motionManager ...

Get The Core iOS Developer’s Cookbook, Fifth Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.