iPad device orientation is null? - iphone

([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);

Related

iphone dev setting device orientation programmatically error

I am using this to set device orientation,
[[UIDevice currentDevice] setOrientation:UIInterfaceOrientationLandscapeRight];
but it is throwing a warning:
Implicit Conversion from enumeration type "UIInterfaceOrientation" to different enumeration type "UIPrintInfoOrientation"
Plus there is a problem which I assume is occurring due to that warning is that when I run the app, the simulator launches but remains black and app doesn't runs, and it says "Attaching to 'aap name'", and nothing happens, now I want to set device orientation for certain views please. guide me, is there any way or work around to do it?
I think you should be using:
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
to inform each of your app's view controllers of the accepted device orientation(s), e.g.
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
return UIDeviceOrientationLandscapeRight;
}

How to set device orientation programmatically in iOS

I have a ebook reader app where in, i need to programmatically adjust the device orientation according to the layout of the page... i.e, The device should support all orientations for some pages and only landscape for some other pages. Is there anyway in which i can force a orientation change programatically
You can use the answer from jbat100 plus a call to setStatusBarOrientation:animated in UIApplication to achieve the effect.
Note that the behavior for this has changed as of iOS 6.0. For further info, see: Autorotate in iOS 6 has strange behaviour
The gist is that should/will/did/etc have been deprecated and have been replaced with a cleaner, more dynamic implementation. Which we should all be thankful for.
NSNumber *value = [NSNumber numberWithInt:UIInterfaceOrientationLandscapeRight];
[[UIDevice currentDevice] setValue:value forKey:#"orientation"];
This can set the device orientation to LandscapeRight programmatically.
You cannot force the device orientation to some value, the system tells you the device's orientation, not the opposite. You can rotate your views using CGAffineTransform and such (a related post here).
Actually you can force any rotation. At least I was able to do it. Solution I used: https://stackoverflow.com/a/10146270/894671
This works for me on Xcode 6 & 5.
- (BOOL)shouldAutorotate {return YES;}
- (NSUInteger)supportedInterfaceOrientations {return (UIInterfaceOrientationMaskPortrait);}

Monotouch OpenGL orientation event

I'm trying to do something that should be very simple, but the interface builder in xCode is doing some stuff behind the curtains that makes it all very unclear.
Basically I want to allow my OpenGL application to be orientation aware, and from what I understand I need to catch these kind of events in an UIViewController.
So, to make it simple, assuming I just created a new project using the standard MonoTouch OpenGL template, what code should I add to catch the orientation events?
Or even better, a template for starting OpenGl without the Interface Builder at all, since I am new to Interface Builder and it only seems to get in the way.
I am not sure if that is what you mean by "orientation aware" but you can have access to the current orientation of the device by calling the following code:
UIDeviceOrientation curOrientation = [[UIDevice currentDevice] orientation];
This will tell you whether the device orientation is Portrait, LandscapeLeft, etc. You can then rotate your views/images accordingly, depending on what you want to achieve.
Please note that UIDeviceOrientation refers to the orientation of the physical device while UIInterfaceOrientation refers to the orientation of the user interface as mentioned in this SO post:
UIDEVICE orientation
You can change the UI orientation by calling the following function:
[[UIDevice currentDevice] setOrientation:someUIInterfaceOrientation];
Hope this helps,

How to find out the simulator orientations?

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!");

Guide me on UIDevice currentDevice

I am using the following code to set the device orientation
[[UIDevice currentDevice] setOrientation:UIInterfaceOrientationLandscapeRight];
when i used i got the warning and i found the below code to fix that warning.
#interface UIDevice (MyPrivateNameThatAppleWouldNeverUseGoesHere)
- (void) setOrientation:(UIInterfaceOrientation)orientation;
#end
Now what i would like to know is ...
Can the app store accepts this code to be in an application?
Thanks for any help!.
oh god, no. The warning you're getting is because this is not a readwrite property; merely adding a category that declares the method will not let you set the orientation. Not only will the AppStore not accept this, it will crash the first time it's called, as there's no accessor. (well, it will PROBABLY crash. There may be an undocumented API here, in which case you'll JUST get rejected).
If you are trying to rotate the view programmatically, you should look at shouldAutorotateToInterfaceOrientation and if you just want the App to be of a specific orientation, try using UIInterfaceOrientation set in plist.
Another useful post:
Forcing UIInterfaceOrientation changes on iPhone
Instead of setting an orientation, the proper way to do it is by having your application listen for when the user rotates the phone, then return YES or NO to indicate that the app should, in fact, rotate (i.e. always return NO if you want the app to always remain in its initial state.) The shouldAutorotateToInterfaceOrientation: method is automatically called whenever the user changes orientation.
For example, in your view controller, implement the method to only allow the phone to be used in landscape right/left:
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
if (UIDeviceOrientationIsLandscape) { return YES };
return NO;
}
You will also want to set your app's default orientation (so it doesn't start in portrait mode) by adding the UIInterfaceOrientation tag to your app's info.plist file with the value UIInterfaceOrientationLandscapeRight. Otherwise, the default value is portrait, and the user will have to tilt the phone to get it into the expected orientation.