-1

UIDevice.current.orientation doesn't work anymore in swift 3 It always returns unknown

I didn't find another way to get the orientation except the following code

func isLandscape() -> Bool{
    let scale = UIScreen.main.scale
    let nativeSize = UIScreen.main.currentMode?.size
    let sizeInPoints = UIScreen.main.bounds.size

    if scale * sizeInPoints.width == nativeSize?.width{
        return false
    }else{
        return true
    }
}

see What is the best way to detect orientation in an app extension?

Community
  • 1
  • 1

1 Answers1

0

EDITED: Frédéric Dal Bo wanted to be able to pass the current device's orientation into the getOrientation method, as he couldn't call UIDevice.current inside the extension he was using. Hopefully this will work, instead of detecting the UIDeviceOrientation inside the getOrientation method using UIDevice.current, you can determine that from the Notification, and then when you handle the notification, pass along the information:

func getOrientation(orientation: UIDeviceOrientation) {

    switch orientation {
    case .portrait:
        print("Portrait")
    case .landscapeLeft:
        print("Lanscape Left")
    case .landscapeRight:
        print("Landscape Right")
    case .portraitUpsideDown:
        print("Portrait Upside Down")
    case .faceUp:
        print("Face up")
    case .faceDown:
        print("Face Down")
    case .unknown:
        print("Unknown")
    }
}

When you initialize the class, or in viewDidLoad(_:) if you're wanting to handle the change in a ViewController:

NotificationCenter.default.addObserver(self, selector: #selector(deviceRotated(_:)), name: NSNotification.Name.UIDeviceOrientationDidChange, object: nil)

func deviceRotated(_ notification: Notification) {
    let device = notification.object as! UIDevice
    let orientation = device.orientation
    getOrientation(orientation: orientation)
}

Then whenever the device rotates, the deviceRotated(orientation:) will be called and it will pass the current device's orientation into the method. You can then handle the orientation change accordingly.

Pierce
  • 3,148
  • 16
  • 38
  • Thanks so much Pierce but I noticed the both methods you proposed don't work in a today extension. It's why I used UIScreen.main. Perhaps I'm wrong – Frédéric Dal Bo Jan 11 '17 at 16:50
  • @Frédéric Dal Bo - I will update my answer to hopefully work for your current circumstance. – Pierce Jan 11 '17 at 17:07
  • @Frédéric Dal Bo - Please see my updated answer, it should work for your needs, and be able to use `UIDevice` – Pierce Jan 11 '17 at 17:12
  • Thank you so much Pierce. I just tried the way you proposed but unfortunately it doesn't work. The extension's viewController is never notified. If I open the Today extension from portrait and switch to landscape mode the extension stays in portrait mode. To open a Today Extension in landscape I've to open an application that accepts landscape, switch to landscape mode and open the today extension. In my case the view is reloaded and never notified. – Frédéric Dal Bo Jan 13 '17 at 16:17