Search code examples
objective-cxcodeipaduideviceorientation

Check if current orientation is supported orientation or not


In my property list file, I have mentioned all the supported orientations. I have many applications which support different orientations. In order to handle all the UI related stuff with all these applications, I have a common project. So, I cannot do any app specific stuff in this common project as it would affect other apps too. In one of files in this common project, I need to check if the device orientation is supported or not.

I have retrieved the supported orientations in an array using

NSArray *supportedOrientations = [[[NSBundle mainBundle] infoDictionary]     objectForKey:@"UISupportedInterfaceOrientations"]; 

my method has signature

-(BOOL) isInvalidOrientation: (UIDeviceOrientation) orientation 

I need to check if current orientation is supported or not, i.e I nee to check if current orientation is there in supportedOrientations array or not.

I am unable to do that because when I use

[supportedOrientations containsObject:self.currentOrientation];

I get an error saying Implicit conversion of UIDeviceOrientation to id is disallowed with ARC.

It is because they are incompatible types. How do I check it ?


Solution

  • The problem is that the UISupportedInterfaceOrientations info key gives you an array of strings. While self.currentOrientation gives you an enum value from UIDeviceOrientation. You need a way to map the enum values to the string values. Also note that you are dealing with device orientations and interface orientations.

    - (NSString *)deviceOrientationString:(UIDeviceOrientation)orientation {
        switch (orientation) (
            case UIDeviceOrientationPortrait:
                return @"UIInterfaceOrientationPortrait";
            case UIDeviceOrientationPortraitUpsideDown:
                return @"UIInterfaceOrientationPortraitUpsideDown";
            case UIDeviceOrientationLandscapeLeft:
                return @"UIInterfaceOrientationLandscapeLeft";
            case UIDeviceOrientationLandscapeRight:
                return @"UIInterfaceOrientationLandscapeRight";
            default:
                return @"Invalid Interface Orientation";
        }
    }
    
    NSString *name = [self deviceOrientationString:self.currentOrientation];
    
    BOOL res = [supportedOrientations containsObject:name];