Use native iOS compass within an app

theDuncs picture theDuncs · Feb 13, 2012 · Viewed 16.7k times · Source

Is it possible to use the native compass that iOS has within my own application? Or do I need to draw and animate my own compass?

Answer

Keller picture Keller · Feb 13, 2012

There is no native compass UIView. In order to use the magnetometer, you'll have to use CoreLocation and the following delegate method:

- (void) locationManager:(CLLocationManager *)manager
             didUpdateHeading:(CLHeading *)newHeading

to rotate a UIView to point North (bearingView is a UIImageView):

float heading = newHeading.magneticHeading; //in degrees
float headingDegrees = (heading*M_PI/180); //assuming needle points to top of iphone. convert to radians
self.bearingView.transform = CGAffineTransformMakeRotation(headingDegrees);