12

Can anyone confirm that to support both iOS 6 and iOS 5 there is no point to adding the new iOS 6 autorotation methods, since the Apple docs suggest that these methods are completely ignored if you are also implementing the iOS 5 methods?

In particular, I talking about the methods - (NSUInteger)supportedInterfaceOrientations and - (BOOL) shouldAutorotate -- that these are ignored and synthesized by the compiler if you also implement the - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation

johnbakers
  • 24,158
  • 24
  • 130
  • 258

6 Answers6

8

You need to add new callback for autorotation if you are packaging your app in the new sdk. However, these callbacks will be received only when such an app is run on iOS 6 devices. For devices running on earlier iOS versions, earlier callbacks would be received. If you dont implement the new callbacks, the default behavior is that your app runs on all orientation on iPad and all except UpsideDown orientation on iPhone.

Varun Bhatia
  • 4,326
  • 32
  • 46
8

For iOS5 Rotation. Check for your desired orientation and return YES

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{  
    if ((interfaceOrientation==UIInterfaceOrientationPortrait)||(interfaceOrientation==UIInterfaceOrientationPortraitUpsideDown)) {
        return YES;
    }
    else return NO;
}

iOS 6.0 Autorotation Support

- (BOOL)shouldAutorotate
{
    return NO;
}

- (NSUInteger)supportedInterfaceOrientations
{
    return UIInterfaceOrientationIsPortrait(UIInterfaceOrientationMaskPortrait|| UIInterfaceOrientationMaskPortraitUpsideDown);

}

- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation
{
    return UIInterfaceOrientationIsPortrait(UIInterfaceOrientationPortrait|| UIInterfaceOrientationPortraitUpsideDown);

}
Rob
  • 415,655
  • 72
  • 787
  • 1,044
NaXir
  • 2,373
  • 2
  • 24
  • 31
  • On the iOS 6 code, shouldn't `supportedInterfaceOrientations` just return `UIInterfaceOrientationMaskPortrait`? And shouldn't `preferredInterfaceOrientationForPresentation` just return `UIInterfaceOrientationPortrait`? The above iOS 6 code doesn't look right to me. – Rob Sep 29 '13 at 16:47
  • @Rob: I am only suggesting methods, please return your own orientation what ever you want. If you don't want UIInterfaceOrientationPortraitUpsideDown you can return just UIInterfaceOrientationPortrait. – NaXir Nov 25 '13 at 09:53
  • I agree with the fact that these are the methods one should implement, but I'm just pointing out that your implementation for these are incorrect (do not use `UIInterfaceOrientationIsPortrait`, which returns a boolean value, in these methods). The `supportedInterfaceOrientations` should return a bitwise mask value, not a boolean. Likewise, `preferredInterfaceOrientationForDevice` should return a single `UIInterfaceOrientation` value, not a boolean. – Rob Nov 25 '13 at 13:57
  • BTW, my apologies because I proceeded to edit your answer, but I really shouldn't have done that without your permission, so I reverted back to your original code. – Rob Nov 25 '13 at 13:58
2

I think the most elegant solution is:

- (NSUInteger)supportedInterfaceOrientations {
    return UIInterfaceOrientationMaskPortrait;
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation {
    return ((1 << toInterfaceOrientation) & self.supportedInterfaceOrientations) != 0;
}

Do I miss something?

DanSkeel
  • 3,853
  • 35
  • 54
  • 2
    or just `return ((1 << toInterfaceOrientation) & self.supportedInterfaceOrientations) != 0;` – user102008 Apr 22 '13 at 20:54
  • @danskeel How does your answer help with the original question: Supporting *both* iOS 5 and 6? Invoking `supportedInterfaceOrientations` causes a crash when running in iOS 5 because it is an iOS 6 feature. `*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[MYMainMenuTableViewController supportedInterfaceOrientations]: unrecognized selector sent to instance 0x68649f0'` – Basil Bourque Aug 24 '13 at 06:10
  • Did you add both methods that I mentioned? I guess you forgot to add the first one to the your class `MYMainMenuTableViewController`. It can't be an issue because it's just a method and I can call it on my own. – DanSkeel Sep 06 '13 at 16:50
  • 2
    Code smell. See [Primitive Obsession](http://c2.com/cgi/wiki?PrimitiveObsession). – dooleyo Oct 04 '13 at 07:48
  • What? Why? Don't get the connection... You mean `NSUinteger`? Say that to apple, not me if you like. [`supportedInterfaceOrientations`](https://developer.apple.com/library/ios/documentation/UIKit/Reference/UIViewController_Class/Reference/Reference.html#//apple_ref/doc/uid/TP40006926-CH3-SW120) – DanSkeel Oct 04 '13 at 11:28
1
 - (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation
{
    return UIInterfaceOrientationMaskAll;
}

-(void)viewWillLayoutSubviews
{
if([self interfaceOrientation] == UIInterfaceOrientationPortrait||[self interfaceOrientation] ==UIInterfaceOrientationPortraitUpsideDown)
    {
       if (screenBounds.size.height == 568)
        {
          //set the frames for 4"(IOS6) screen here
         }
        else

        {
         ////set the frames for 3.5"(IOS5/IOS6) screen here
        }

}
        else if ([self interfaceOrientation] == UIInterfaceOrientationLandscapeLeft||[self interfaceOrientation] == UIInterfaceOrientationLandscapeRight)
        {
         if (screenBounds.size.height == 568)
        {
          //set the frames for 4"(IOS6) screen here
         }
        else

        {
         ////set the frames for 3.5"(IOS5/IOS6) screen here
        }


    }
//it is for IOS5
 - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
    {
     return YES;
    }

-(void)viewWillLayoutSubviews this method will call for ios5/ios6. this code is usefull to both ios6/ios5/ios6 with 3.5" screen/4" screen with ios6.

Krish Allamraju
  • 703
  • 1
  • 7
  • 29
  • - (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation { return UIInterfaceOrientationMaskAll; } Will cause crash. Because return value should be an concrete orientation but not mask – Evgeniy S Aug 23 '14 at 10:30
0

You have to set a flag somewhere (I believe in your Info.plist) that indicates which of the two you use. If you use the new one, you can't build for iOS 5 (or at least, it won't work on iOS 5). If you use the old one, the new methods aren't being called. So yeah, you pretty much have to choose which method(s) you want to use, and if you want to support iOS 5, you cannot use the new methods.

Scott Berrevoets
  • 16,921
  • 6
  • 59
  • 80
  • are you sure about that, regarding the flag? I didn't see any mention of that in the iOS 6 release notes; they simply state that the new methods are ignored if the old methods are implemented. – johnbakers Sep 15 '12 at 06:26
  • Have a look at WWDC Session 200, around 26' they talk about this. Also look at session 236, 23'45" onwards. Not sure if this is still under NDA, so please look directly there – Olaf Sep 15 '12 at 08:37
  • 3
    Both callbacks are supported for the new iOS 6. New callbacks are received on apps packaged with new sdk and running on iOS 6 devices. For all other cases, earlier callbacks are well received. Have tested this in numerous cases. – Varun Bhatia Sep 17 '12 at 09:35
  • 100% agree with @vabhatia , it works perfect for both iOS 5 and 6 devices if you implement relevant methods – swiftBoy May 08 '13 at 07:16
0

to support autorotations in both ios5 and ios6 we need to provide call backs in case of ios6....

[[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications];
[[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(orientationChanged:) 
    name:UIDeviceOrientationDidChangeNotification object:nil];

and we need to call

- (NSUInteger)supportedInterfaceOrientations {
return UIInterfaceOrientationMaskPortrait;

} 

-(BOOL)shouldAutoRotate{
    return YES;
  }

for ios5

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation
{
return ((toInterfaceOrientation == UIInterfaceOrientationPortrait) || (toInterfaceOrientation == UIInterfaceOrientationPortraitUpsideDown));
}
kleopatra
  • 51,061
  • 28
  • 99
  • 211
  • i don't think it is necessary to hardcode the callbacks, they are automatic in iOS if you use UIKit and have setup the project settings correctly. – johnbakers Aug 22 '13 at 23:45