Search code examples
iosswiftxcodeaccelerometergyroscope

SWIFT: iPhone held “upwards” or is laying on a table


How do I have to use the accelerometer / gyroscope in Swift in order to find out if...

A) the iPhone is held just in front of the users face, i.e. it is held "upwards" or

B) the iPhone has been put for instance on a table, i.e. the display is facing up.

C) make an if statement for when it is put on a table or up in front of the users face.


Solution

  • UIDeviceOrientation will return a value for which way a device is oriented:

       enum UIDeviceOrientation : Int {
            case Unknown
            case Portrait
            case PortraitUpsideDown
            case LandscapeLeft
            case LandscapeRight
            case FaceUp // <- THIS IS THE ONE THAT YOU ARE LOOKING FOR
            case FaceDown
        }
    

    Without seeing your actual code or knowing exactly what you are trying to achieve it is impossible to give you more specific help than that.