UIDevice currentDevice's "orientation" always null
Asked Answered
L

5

8

As per the title. Calling [[UIDevice currentDevice] BeginGeneratingDeviceOrientationNotifications] has no effect.

DidRotateToInterfaceOrientation etc events are working fine, but I need to be able to poll the device orientation arbitrarily.

How can I fix/do this?

The long story: I have a tab application with a navigation controller on each tab. The root view of tab number one is a graph that goes full screen when the orientation changes to landscape; however this needs to be checked whenever the view appears as the orientation change could have occurred elsewhere, so I was hoping to poll the orientation state whenever this view appears.

Liking answered 17/3, 2010 at 10:8 Comment(0)
C
9

UIDevice's notion of orientation seems to be only available on actual devices. The simulator seems to always return 0 here, regardless of whether the notifications have been enabled as the docs suggest. Irritatingly inconvenient, but there you go.

I find this works fine on the actual device:

    [[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications];
    NSLog(@"orientation: %d", [[UIDevice currentDevice] orientation]);
    [[UIDevice currentDevice] endGeneratingDeviceOrientationNotifications];
Consortium answered 14/8, 2010 at 18:50 Comment(3)
Yep, just wasted 2h trying to figure out what was wrong but indeed it was just the simulator. All working fine on the device.Jaredjarek
[[UIDevice currentDevice] orientation] does not work if the user has locked his device orientation from the Springboard. orientation is always the last value, and there are no notifications for changes.Spae
UIDeviceOrientation interfaceOrientation = [UIApplication sharedApplication].statusBarOrientation; this alternative worked for meSimilar
P
2

seems like a silly question, but isn't it

beginGeneratingDeviceOrientationNotifications

( lower case b ) ...

Purity answered 2/8, 2010 at 14:49 Comment(0)
S
2

If you check [[UIDevice currentDevice] orientation] in - (void)viewDidLoad you will always get nil.

Check it in *- (void) viewDidAppear:(BOOL)*animated method and you

Stylo answered 12/11, 2012 at 9:49 Comment(1)
"...you will always get nil". Is this documented?Alto
T
1

this is as per iMeMyself said in the comments above - this samed me a lot of time and I think is the right answer so I wanted to highlight it here:

UIDeviceOrientation interfaceOrientation = [UIApplication sharedApplication].statusBarOrientation;

if (UIInterfaceOrientationIsLandscape(interfaceOrientation))
{
   //do stuff here
}
else if (UIInterfaceOrientationIsPortrait(interfaceOrientation))
{
//or do stuff here
}
Trygve answered 13/8, 2013 at 2:29 Comment(1)
this worked for me, but shouldnt it be UIInterfaceOrientation interfaceOrientation=[.... ?Diaz
C
0

Wouldn't [[UIDevice currentDevice] orientation] give you the current orientation state? You could check for this in your viewWillAppear method of the view controller that wants to poll.

Edit: Other than that, there are various ways to get the current orientation, such as using the statusBarOrientation property in UIApplication, or interfaceOrientation property in UIViewcontroller.

Cordeliacordelie answered 22/5, 2010 at 0:33 Comment(1)
Looks like you didn't read the question. He's saying that method is returning NULL even though he's calling beginGeneratingOrientationNotifications.Kishakishinev

© 2022 - 2024 — McMap. All rights reserved.