The iPhone compass works
via an internal magnetometersensor that measures the strength and direction of Earth's magnetic field, allowing it to determine magnetic north. Combined with GPS and the accelerometer, it maps this data to show cardinal directions and location data