1

I used magnometer but its values is always different.. I want to detect when the user device is horizontal mean in landscape position and on specific posting want to perform some action.

Blazej SLEBODA
  • 8,936
  • 7
  • 53
  • 93
Ali asghar
  • 29
  • 2

1 Answers1

0

In AppDelegate.swift inside the didFinishLaunchingWithOptions function put the below code :

NotificationCenter.default.addObserver(self, selector: #selector(AppDelegate.rotated), name: UIDevice.orientationDidChangeNotification, object: nil)

then inside the AppDelegate put the following function:

func rotated() {
    if UIDeviceOrientationIsLandscape(UIDevice.current.orientation) {
        print("Landscape")
    }

    if UIDeviceOrientationIsPortrait(UIDevice.current.orientation) {
        print("Portrait")
    }
}

hope it will work for you ... :)

Shivam Parmar
  • 1,520
  • 11
  • 27
  • But i think this will only work if user device is not locked for orientation – Ali asghar Jan 18 '20 at 11:47
  • check it ... https://stackoverflow.com/questions/34936205/how-to-programatically-check-if-orientation-is-locked-on-ios-swift – Shivam Parmar Jan 18 '20 at 11:55
  • I don't want to detect phone's View orientation.. I want to detect orientation of phone it self.....i mean if phone is horizontal i perform some action....i get some values with magnetometer but these are values of magentic field and keep on changing according to envoirment – Ali asghar Jan 18 '20 at 14:15