Best way to Supporting Multiple Interface Orientations - iOS8 - objective-c

I have been trying to convert few old apps written in objC long ago in iOS4 which support multiple ViewController with different orientation.
(BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
Above Method is called in app delegate from ViewController and Desire Orientation was set but seems stopped working with iOS8.
I have tried number of solution like creating Category of UINavigation controller etc, but nothing seems working with iOS8.
What is best approach to handle Multiple Orientation for both iPhone and iPad.
Furthermore, I am not using Storyboard with size classes, in plist i am only supporting portrait orientation.

Check all the orientation you want to support in device.
For that go to Project target-> Deployment info -> Device orientation-> Tick mark the desired orientation.

From iOS 6 onwards, you've to use.
- (BOOL)shouldAutorotate {
UIInterfaceOrientation orientation = [[UIApplication sharedApplication] statusBarOrientation];
if (orientation == UIInterfaceOrientationPortrait) {
// your code for portrait mode
}
return YES;
}
Refer to shouldAutorotateToInterfaceOrientation not being called in iOS 6

Related

iOS 6 Game Center Crash on Authentication in landscape mode cocos2d

I have developed a game in cocos2d and all game screens are in Landscape mode. I am trying to implement game Center but getting crash on authentication. I did not find answer of similar type of issues. please suggest right approach...
Crash issue:-'UIApplicationInvalidInterfaceOrientation', reason: 'Supported orientations has no common orientation with the application, and shouldAutorotate is returning YES'
I tried below solution but it also disturb game orientations, game starts work in portrait mode also, that i don't want:-
(NSUInteger)application:(UIApplication*)application
supportedInterfaceOrientationsForWindow: (UIWindow*)window
{
return UIInterfaceOrientationMaskAllButUpsideDown;
}
Make sure you selected landscape in Xcode summary page.
Also add these code in your viewcontroller
-(NSUInteger)supportedInterfaceOrientations {
return UIInterfaceOrientationMaskLandscape;
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
return UIInterfaceOrientationIsLandscape(interfaceOrientation);
}
Update this function in AppDelegate:
- (NSUInteger)application:(UIApplication*)application supportedInterfaceOrientationsForWindow:(UIWindow*)window
{
return UIInterfaceOrientationMaskLandscape;
}
the solution for that is short, i spent a lot of time before finding it:
in the AppDelegate in the method didFinishLaunchingWithOptions put this line:
[[UIApplication sharedApplication] setStatusBarOrientation:UIInterfaceOrientationPortrait];
obviously before call the login game center method, i put that before create the UIWindows

Launching an app in landscape under ios6 while supporting all orientations

I have done a ton of research and am still unable to find a solution for this...
I have an app that supports all orientations. When the app is run on the iPad I want it to launch in landscape mode. The user can then rotate to portrait if they choose. Under ios5, I accomplished this by setting supported orientations for the iPad in the info.plist file to lansdcape left. I then used the shouldAutorotateToInterfaceOrientation method in each view controller to allow all orientations when the app was running.
I then changed my code to support the new orientation methods for iOS6 but when I ran the app on the iPad, it was not rotating at all. I then changed the info.plist file to support all orientations for the iPad. This solved the problem with the iPad not autorotating but now I can't figure out how to force the app to initially open in landscape mode. Any ideas?
Thanks in advance for any help you can give!
I ran into something similar recently, although my problem was that I wanted to force portrait in an iPhone app, but allow movies to play in landscape. Here's what I did:
Add a property to my app delegate to store the orientations I want to allow:
#property (nonatomic, assign) NSUInteger supportedOrientations;
Then in the app delegate, implemented the new iOS 6 method:
- (NSUInteger)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window {
return supportedOrientations;
}
On app launch I forced portrait:
self.supportedOrientations = UIInterfaceOrientationMaskPortrait;
And then once the app had finished launching and I'd shown my root view controller, I set it to support both orientations:
self.supportedOrientations = UIInterfaceOrientationMaskPortrait | UIInterfaceOrientationMaskLandscape;
I had to add the additional step of subclassing my UINavigationController to prevent rotation after that, because I wanted to allow landscape video but not rotation:
- (BOOL)shouldAutorotate {
return FALSE;
}
But that's just for info. You won't need that

Forcing the portrait orientation for my View

I am working on ARC based project. My project is targeted iOS 4.3 I want to force the
Portrait orientation to one of my view. The following doesn't seem to work for me.
[[UIDevice currentDevice] performSelector:NSSelectorFromString(#"setOrientation:") withObject:(id)UIInterfaceOrientationPortrait];
Any help on this?
First of all the viewController to which orientations need to supported must implement the below method passing the supported orientations.
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation
Later to manually rotate the view set the device orientation directly using
[[UIDevice currentDevice] setOrientation:UIInterfaceOrientationPortrait];
if this doesn't work use
[[UIApplication sharedApplication] setStatusBarOrientation:UIInterfaceOrientationPortrait animated:YES];
remember if you return NO for the setting orientation in shouldAutoRotate this may not work. Also check this in both ios5 & ios6, i have tried in ios5.
EDIT: For iOS5 or iOS6 where setOrientation doesn't exist
[[UIDevice currentDevice] performSelector:NSSelectorFromString(#"setOrientation:") withObject:(id)UIInterfaceOrientationPortrait];
this works fine, but this may be a private api now.

ipad rotation in iphone project

I've created a project that is targeted just for iPhone (for now) and I chose supported orientation only Portrait. On iPhone it works great, but when I launch my project on iPad my application is rotating! Actually I tried to make it a universal app and in iPad deployment info I chose supported orientation only Portrait, my app still rotated on iPad. How may I fix that?
To only support Portrait mode in your view, implement the following in each of your views:
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
Also, you need to make sure that have set the Supported Device Orientations for each device as well:
You can manually set the rotation values in the view controllers by implementing this function:
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
return (interfaceOrientation == UIInterfaceOrientationPortrait;
}

Force iOS app to launch in landscape mode

I am developing an app for iOS 5, which have to run in landscape mode. My problem is that I cannot get it to flip initially.
I have tried the adding "Initial interface orientation" set to "Landscape (right home button)"
and adding the following method to my view controller:
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
// Return YES for supported orientations
return (interfaceOrientation == UIInterfaceOrientationLandscapeRight);
}
I can wrap my head around how it is supposed to work. (I found the code here)
I am also wondering how to use the "Supported Device Orientation" available in the Xcode 4.2 project setup, it does not seem to do anything.
I have been looking around the website and have not been able to find an example that solves my problem.
Thank you.
In your application’s Info.plist file, add the UIInterfaceOrientation
key and set its value to the
landscape mode. For landscape
orientations, you can set the value
of this key to
UIInterfaceOrientationLandscapeLeft
or
UIInterfaceOrientationLandscapeRight.
Lay out your views in landscape mode and make sure that their autoresizing options are set correctly.
Override your view controller’s shouldAutorotateToInterfaceOrientation: method and return YES only for the
desired landscape orientation and NO
for portrait orientations.
use the following in appDelegate
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
[[UIApplication sharedApplication] setStatusBarOrientation:UIInterfaceOrientationLandscapeRight];
also set the required orientations in
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
// Return YES for supported orientations
return (interfaceOrientation == UIInterfaceOrientationLandscapeRight);
}
//Here are some iOS6 apis, used to handle orientations.
- (BOOL)shouldAutorotate NS_AVAILABLE_IOS(6_0)
- (NSUInteger)supportedInterfaceOrientations - (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation
When you click on your project's name in XCode and select your target under TARGETS, under Summary tab there's a visual representation of the supported device orientation settings. That was how I managed to do it.
i think if you at the supported interface orientations in the project plist it will work. just add a new line called "Supported interface orientations" and make it of array type and add 2 items for each of teh landsape views u want. havent tested this just a quick guess
Try this this will help you, write this code in you viewdidload.
[[UIApplication sharedApplication] setStatusBarOrientation:UIInterfaceOrientationLandscapeRight];