My friend uses a Smartphone compass. I had a theory that it works by GPS comparing the position of the phone with known north. Turns out that this isn't true. The phones have magnetometers in them. These are often Hall probes which are doped semi-conductor. When this is placed in a magnetic field, the charges in the semi-conductor are displaced laterally relative to the current, creating a PD across the Hall probe. PD is proportional to magnetic flux density. I have seen suggestions that small arrays of Hall probes inside measure the flux density differently so allowing software to calculate the direction of greatest flux density - ie north.