it sort of has a compass, but an electronic one, in the form of a 3-axis Hall effect magnetometer
This generates three signals that depend on the strength of the magnetic field, including the earth’s
however yes, it does absolutely get disrupted by all the metal in the phone, but since that doesn’t really change as the phone rotates, it rotates with the phone and the sensor, always reading the same, the phone can isolate only the part that moves and get a relatively accurate compass
Have you ever tried to use your compass and your phone asks you to move it around in a circle kind of motion? It’s calibrating the compass readings to account for the metal in the phone. By getting measurements from all different angles, it can determine how much of the reading is coming from metal in the phone and how much is from Earth’s magnetic field.
Latest Answers