iphone dev setting device orientation programmatically error - iphone

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;
}

Related

Device orientation - change in legal way (iOS 4.0+)

My app has navigation bar, where 1st screen returns YES to orientation, the second one is set to some orientation basing on what user choose in 1st screen. After going back to 1st screen from 2nd one, if user had device in hand in portrait but interface was in landscape, 1st screen is set to landscape. This happens because of
(BOOL)shouldAutorotateToInterfaceOrientation(UIInterfaceOrientation)interfaceOrientation
Is called only after changing device orientation.
I want to check what is device orientation atm and set interface orientation to this one.
Tried:
//1st method:
UIViewController *rotateTheScreen = [[UIViewController alloc]init];
[self presentModalViewController:rotateTheScreen animated:NO];
[self dismissModalViewControllerAnimated:NO];
[rotateTheScreen release];
//2nd method:
UIInterfaceOrientation interfaceOrientation = self.interfaceOrientation;
//3rd method:
UIInterfaceOrientation orientation = [[UIDevice currentDevice] orientation];
1st is acting strange, rotates all cases besides coming back from interface orientation = landscape and device orientation = landscape (here is a bug, he rotates to landscape)
2nd checks interface, like the name tells, and tho doesnt work for my problem
3rd as far as i heard is private and Apple rejects apps using this.
Take a look at this thread.
Basically, there's no way to force a device orientation and get your application approved by Apple.
Shortly, the method exists but is an undocumented method of the UIDevice class.
[[UIDevice currentDevice] setOrientation: UIInterfaceOrientationLandscapeRight animated:YES];
This gives a compiler warning that you can get rid of with a category.
#interface UIDevice (MyAwesomeMethodsThatAppleWillNeverAllowMeToUse)
-(void)setOrientation:(UIInterfaceOrientation)orientation animated:(BOOL)animated;
-(void)setOrientation:(UIInterfaceOrientation)orientation;
#end
Also, some say that you can call these methods indirectly using performSelector to get around Apple's static code analysis, as you can read in the comments here.
as I know there are no legal way. If I wrong, please, correct me somebody!
change the 'Initial interface orientation' in your project plist file

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.

Launching application in landscape orientation for IPad

Facing one issue with launching application in landscape orientation for IPad.
I have developed IPhone application which later I ported to IPad.
I have made setting regarding orientation in info.plist
[ UISupportedInterfaceOrientations~ipad ] to support all orientation UIInterfaceOrientationPortrait , UIInterfaceOrientationPortraitUpsideDown , UIInterfaceOrientationLandscapeLeft , UIInterfaceOrientationLandscapeRight.
but when I start IPad application in the landscape mode, it always start in the potrait mode.
Along this
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{ return YES; }
help me, if I am missing something with this..
Thanks,
Sagar
here's something I also discovered: setting the initial interface orientation in your info.plist is being ignored if you have Supported interface orientations set up with another orientation in the first slot! Put your initial orientation there as well - and the simulator will launch correctly, as will the app. this drove me nuts for a long time!
Put UISupportedInterfaceOrientations into your -Info.plist, with a setting for each orientation you support. This is used to see which orientation the app can start in. From there onwards it will ask your view controllers.
Sagar - I had the same issue but was able to resolve it.
Like yours, my app started as an iPhone app which I "upgraded" to a Universal app using the XCode wizard. I noticed that when running on the actual iPad, starting in landscape, the app would start in Portrait, then maybe rotate to Landscape. On the simulator, starting in landscape, the app would start in Landscape, then the simulator would rotate to Portrait.
On the iPad, my app is a split-view app with TabBarControllers on left and right. Each tab is a view controller that returns YES to shouldAutoRotateToInterfaceOrientation.
I noticed that a brand-new wizard-generated, simple-case with a splitviewcontroller, Universal app didn't have this problem.
The difference I found between my app and the simple-case was that I wasn't adding my splitview-controller's view to the app window in applicationDidFinishLaunchingWithOptions. Instead I was showing a "loading" view at this stage, then later when an initialization thread completed I'd add my splitviewcontroller's view (and hide the "loading" view).
When I added my splitviewcontroller's view to the app window during the call to applicationDidFinishLaunchingWithOptions everything started working fine.
There must be some magic that happens on return from applicationDidFinishLaunchingWithOptions???
Is your app similar to mine in that it isn't adding the main view controller's view to the window during applicationDidFinishLaunchingWithOptions?
As pointed out in a number of posts, you must set up the info.plist with both the supported and the initial interface orientations. However, the bigger issue is when does the initial orientation become effective? The answer is NOT when your view controller receives the "viewDidLoad" message. I found that on the iPad-1, running iOS 5.0, the requested initial orientation becomes effective only after several "shouldAutorotateToInterfaceOrientation"
messages are received.(This message passes the UIInterfaceOrientation parameter to the receiver.) Furthermore, even if the orientation says it is in Landscape mode, it may not be! The only way I found to be sure that the view is in Landscape mode is to test that the view height is less than the view width.
The strategy that worked for me was to lay out the subViews I wanted when the "viewDidLoad" message was received but to delay actually adding those subViews to the view until the controller received a valid "shouldAutorotate.." message with the orientation set to Landscape mode. The code looks something like:
(BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
// Return YES for supported orientations
// N.B. Even when the interface orientation indicates landscape mode
// this may not really be true. So we insure this is so by testing
// that the height of the view is less than the width
if (interfaceOrientation == UIInterfaceOrientationLandscapeLeft ||
interfaceOrientation == UIInterfaceOrientationLandscapeRight)
{
CGRect viewBounds = [[self view] bounds];
if ( viewBounds.size.height < viewBounds.size.width )
[self addMySubViews];
return YES;
}
else
return NO;
}
Apple has just released iOS 5.1, so this behavior may have changed. But I expect the code that is here should still work.

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

UI autorotation works only on some devices?

I'm having quite interesting (but very annoying) problem with (not only) UI autorotation. I'm working on the app which contains tabbar controller with couple of navigation controllers as tab items. One of those controllers allows for autorotation to landscape mode.
Interesting is that it perfectly works in Simulator as well as on my own device, but it's not working on other testing devices. All devices are the same - iPhone 3G 16 GB, OS version 3.1.3.
When I'm looking for a log messages which report orientation change events in console, I can find it again only on my device, but not on other devices, so it seems that other devices do not report orientation change at all. Which is very unlikely on the other hand, because other apps are working normally... Also some other features which are working on my phone are not working on other devices too (touch events handling in tableview for instance).
I've never seen something like this and cannot find any reason why the hell it should not work on all devices the same way! So the question is - am I mad or what?
I use subclass of UITabBarViewController with overriden method
- (BOOL) shouldAutorotateToInterfaceOrientation: (UIInterfaceOrientation) toInterfaceOrientation {
BOOL shouldRotate = (toInterfaceOrientation == UIInterfaceOrientationPortrait || toInterfaceOrientation == UIInterfaceOrientationLandscapeLeft);
NSLog(#"Should rotate: %d", shouldRotate);
return shouldRotate;
}
Then I handle orientation change animation in respective methods (willRotate..., willAnimateRotation...). But as I said - it works fine on my device, but on other devices the shouldAutorotateToInterfaceOrientation: method gets never called. Weird!
Please give me some hint before I'll end in a madhouse. Thanks for any tip.