How to find out the simulator orientations? - iphone

iam developing one app .In that i want to find out the iphone orientations.I written the code for that one.Is there any way to find out the simulator orientations.

Jeremy's answer is missing an exceedingly important piece of information about [[UIDevice currentDevice] orientation] - as per the Apple documents:
The value of this property always returns 0 unless orientation notifications have been enabled by calling beginGeneratingDeviceOrientationNotifications.
Before you call [[UIDevice currentDevice] orientation] you must first call [[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications].
When you're done, you should also call endGeneratingDeviceOrientationNotifications at some point.
However, it is worth noting that the simulator can at times fail to pass orientation changes through: I would strongly recommend testing your orientation changes on a device. There are edge cases you won't see on the simulator that will definitely occur on actual hardware.

This little snippet should work both in the simulator and on a physical device. You also need a few other bits of code for it it work as lxt pointed out.
[UIDevice currentDevice].orientation
Example:
if ([UIDevice currentDevice].orientation == UIDeviceOrientationPortrait)
NSLog("The device orientation is portrait!");
else
NSLog("The device orientation is NOT portrait!");

Related

iPhone rotation on demand

Is there a way to manually check current position of device AND ask device to automatically rotate it without waiting for user to actually rotate the device?
Currently I control the rotation with several IFs in -shouldAutorotateToInterfaceOrientation but in few situations I have to stop the view from rotating and enable it again and I don't want user to rotate the device twice to have the desired orientation.
for checking Orientation
UIInterfaceOrientation orientation = [[UIDevice currentDevice] orientation];
then based on your condition you can change device orientation as you wish..
you can use :
[UIApplication sharedApplication].statusBarOrientation = UIInterfaceOrientationPortrait;
or what ever you want.
or you can go for following but this is not do
[[UIDevice currentDevice] setOrientation:UIDeviceOrientationPortrait];

Inaccurate device orientation returned by UIDeviceOrientationDidChangeNotification

I'm listening to the UIDeviceOrientationDidChangeNotification in order to adjust my UI based on the device's orientation.
The problem is that the device orientation I'm getting from the notification seems inaccurate. If I start with the phone in portrait orientation and vertical (as if taking a picture with it) the orientation I get from the notification is correct. As the tilt of the phone approaches horizontal (as in laying flat on a table top) the orientation switches to landscape. This happens much before the phone is actually flat on the table. And this is without rotating the phone towards landscape at all. Is as if it had a preference for landscape.
When using other apps, like mail, I don't see this same behavior. It seems that mail only switches orientation once it's really sure you've gone to the new orientation.
Any help greatly appreciated.
Thanks,
I found my problem.
In viewWillAppear I have:
[[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications];
[[NSNotificationCenter defaultCenter] addObserver: self selector: #selector(didChangeOrientation:) name: UIDeviceOrientationDidChangeNotification object:
[UIDevice currentDevice]];
And here is the notification handler:
- (void) didChangeOrientation: (id)object {
UIInterfaceOrientation interfaceOrientation = [[object object] orientation];
//DLog(#"val is %i", interfaceOrientation);
if (interfaceOrientation == UIDeviceOrientationLandscapeLeft || interfaceOrientation == UIDeviceOrientationLandscapeRight || interfaceOrientation == UIDeviceOrientationPortrait) {
[self orientInterface:interfaceOrientation];
}
By checking that the orientation is one of the two landscapes or the two portraits I'm ignoring the UIDeviceOrientationFaceUp and UIDeviceOrientationFaceDown orientations. That way setting the phone in one of this two orientations won't have an effect on my interface's orientation.
My problem was that I was no considering the faceUp and faceDown and was handling them both in an else statement which was assuming landscape.
If your only interest is your interface orientation (ie landscape or portrait orientation of the device), you should NOT use UIDevice:orientation (or the UIDeviceOrientation* constants if you will), but rather use the UIApplication:statusBarOrientation, which uses the UIInterfaceOrientation* constants.
I use the following code to check for landscape modus:
static inline bool isOrientationIsLandscape() {
return UIInterfaceOrientationIsLandscape([[UIApplication sharedApplication] statusBarOrientation]);
}
And for portrait modus:
static inline bool isOrientationIsPortrait() {
return UIInterfaceOrientationIsPortrait([[UIApplication sharedApplication] statusBarOrientation]);
}
This cost me a whole morning to figure out, because there is no UIDeviceOrientationFace[Up|Down] on the simulator, only on the real device. So my app worked on the simulator all the time, but on the actual device I had some undefined behavoir during testing every now and then.
Now it works on the actual device like it does on the simulator.
HTH
The notifications are just one way to get at it, you can also read out the accelerometer yourself and implement it in exactly the way you see fit (with a delay and a certain time of non-rotation for example).
Don't know if it's a power drain to get those readouts, but if so, use the notification to know when things are moving, and then fire up the accelometer-reading.

iPad device orientation is null?

([UIDevice currentDevice].orientation);
using the above code in my app, all i get is 0 (unknown)
is there something thats going wrong here? Seems pretty straightforward
edit: more research led me to the problem with using device orientation early in the app, which I am doing. However when I instead use interfaceOrientation, it always returns 1 (portrait)
Are you seeing this in the simulator or on the device?
The Simulator seems to always return 0, regardless of it's orientation.
If you're seeing this on the device, have you made sure that you're calling
[[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications];
From the documentation:
You must call this method before attempting to get orientation data from the receiver.
How do you know it's null? By logging the value?
The orientation property is a numeric value (enum), so you should make sure to log it as such:
NSLog("%d",[UIDevice currentDevice].orientation);

UIDevice currentDevice's "orientation" always null

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.
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];
seems like a silly question, but isn't it
beginGeneratingDeviceOrientationNotifications
( lower case b )
...
If you check [[UIDevice currentDevice] orientation] in - (void)viewDidLoad you will always get nil.
Check it in *- (void) viewDidAppear:(BOOL)*animated method and you
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
}
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.

How to control the orientation of the device in iphone / ipod?

i have to do something on rotation the device on the portrait mode but when i use the
(BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
// Return YES for supported orientations
//[[[UIApplication sharedApplication]keyWindow]subviews removeFromSuperview ];
// return (interfaceOrientation == UIInterfaceOrientationLandscapeRight);
return YES;
}
following code and it work fine on the simulator but when i use to install the code in the device it make the device so much sensitive that if i just a little change the position in the device it execute this code and go to the view that i m showing on the rotation of the phone
than pls anyone tell me that how can i control the sensitivity of the device i mean that i just want when user complete a 90' rotation in the position than my code should execute but it just execute if just shake the phone in the same position .
thanks for any help
Balraj verma
You probably want to buffer the orientation information; that is, only change the orientation after you have received several indications from the sensor in a row that the orientation is different from what you're currently displaying.
From my understanding of what you have written, your application is too sensitive when the device is rotated? It's difficult to understand why this is; the code you've included states that you're application is willing to accept any rotation, but you've not stated how you deal with the rotation events afterwards.
Based on the info, I can only suggest that you add a check to in your code (perhaps where the rotation event is dealt with) and obtain the orientation from the device using:
[[UIDevice currentDevice] orientation];
which will return a UIInterfaceOrientation enum stating the current orientation of the device.
UIDeviceOrientationPortrait
UIDeviceOrientationPortraitUpsideDown
UIDeviceOrientationLandscapeLeft
UIDeviceOrientationLandscapeRight
You can then use this to establish whether there's a need to change the orientation. (Note: this is iPhone OS 2.0. I believe that OS 3.0 has more including lying face up/down, etc.)
- (void) didRotate:(NSNotification *)notification
{
UIDeviceOrientation orientation = [[UIDevice currentDevice] orientation];
if (orientation == UIDeviceOrientationLandscapeRight)
{
NSLog(#"Landscape Right!");
}
}
If you find that the orientation switches too quickly you may want to buffer the information, creating several sample points that you can compare before switching to the new orientation.