Bartek Pacia
Bartek Pacia

Reputation: 1716

XCUIDevice.shared.orientation returns unknown until I manually rotate the simulator

I have a problem with XCUIDevice.shared.orientation returning UIDeviceOrientation.unknown when the iOS simulator is first started. This effectively makes it impossible to query the iOS simulator for its orientation.

Plot twist

When the iOS simulator's orientation is manually changed (screenshot below), then [XCUIDevice.shared.orientation] starts returning correct value. See repro below for an example.

[XCUIDevice.shared.orientation] also starts returning correct value when it's explicitly assigned a value:

XCUIDevice.shared.orientation = .portrait

Reproduction

Cold boot a iOS simulator (CMD+Q Simulator.app to make sure it starts fresh)

Paste the following code in a UI Testing Target in your iOS project:

import XCTest
import os

final class ExampleUITests: XCTestCase {

  func testDeviceRotation() {
    NSLog("Current orientation: \(XCUIDevice.shared.orientation)")
  }
}

Notice that Current orientation: UIDeviceOrientation(rawValue: 0) is printed in Xcode debug output (to open it: CMD+Shift+Y).

Now rotate the simulator:

no desc

Run the testDeviceRotation test again and see that the correct orientation is returned.

What I want

I want a reliable way to get UIDeviceOrientation at any given time.

Upvotes: 0

Views: 50

Answers (0)

Related Questions