Question

I'm a little bit lost in the bitwise ;)

My goal is to retrieve the whole set of orientations supported by an application and to test each result value to update a custom variable. My problem is that I don't know how to make the comparison (I got a conversion/test problem...)

First I read this article : Testing for bitwise Enum values But it doesn't bring me the light...

Let say I have the following orientation declare for my application (following is the log output for my variable supportedOrientations) : supported orientations = ( UIInterfaceOrientationPortrait )

So my first attempt was to try some test on integer values but it does not work (even if the application is declared to be in portrait mode the test return 'false') :

NSArray *supportedOrientations = [[NSBundle mainBundle] objectForInfoDictionaryKey:@"UISupportedInterfaceOrientations"];
NSLog(@"[supported orientations = %@", supportedOrientations);
// for clarity just make a test on the first orientation we found
if ((NSInteger)supportedOrientations[0] == UIInterfaceOrientationPortrait) {
  NSLog(@"We detect Portrait mode!");
}

My second attempt was to try the bitwise thing but this time it always return 'true' (even if the supported orientation is not UIInterfaceOrientationPortrait). :

NSArray *supportedOrientations = [[NSBundle mainBundle] objectForInfoDictionaryKey:@"UISupportedInterfaceOrientations"];
NSLog(@"[supported orientations = %@", supportedOrientations);
// for clarity just make a test on the first orientation we found
if ((NSInteger)supportedOrientations[0] | UIInterfaceOrientationPortrait) { // <-- I also test with UIInterfaceOrientationMaskPortrait but no more success
  NSLog(@"We detect Portrait mode!");
}

So my question is :

  • How to test the orientation in my case?

  • Is it a way to use a test by using a bitwise thing (using | operand)?

Was it helpful?

Solution

The official doc says that UISupportedInterfaceOrientations is an array of strings. https://developer.apple.com/library/ios/documentation/general/Reference/InfoPlistKeyReference/Articles/iPhoneOSKeys.html#//apple_ref/doc/uid/TP40009252-SW10

So the solution is to use NSString comparison for each element found in the array.

NSArray *supportedOrientations = [[NSBundle mainBundle] objectForInfoDictionaryKey:@"UISupportedInterfaceOrientations"];
for (NSString *orientation in supportedOrientations) {        
    if ([orientation isEqualToString:@"UIInterfaceOrientationPortrait"] ||
        [orientation isEqualToString:@"UIInterfaceOrientationPortraitUpsideDown"]) {
        NSLog(@"*** We detect Portrait mode!");
    } else if ([orientation isEqualToString:@"UIInterfaceOrientationLandscapeLeft"] ||
               [orientation isEqualToString:@"UIInterfaceOrientationLandscapeRight"]) {
        NSLog(@"*** We detect Landscape mode!");
    }
}

Note that doing like this we didn't take advantage of the enum values (of type UIInterfaceOrientation), but it works!

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top