Reputation: 515
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 ?
Upvotes: 2
Views: 1313
Reputation: 697
I have a similar need to determine the app supported orientations in order to pre cache some resources. But I only need to know if the app supports portrait, landscape or both. This thread led me to the following solution so I thought I might post it.
// get supported screen orientations
NSArray *supportedOrientations = [[[NSBundle mainBundle] infoDictionary]
objectForKey:@"UISupportedInterfaceOrientations"];
NSString *supportedOrientationsStr = [supportedOrientations componentsJoinedByString:@" "];
NSRange range = [supportedOrientationsStr rangeOfString:@"Portrait"];
if ( range.location != NSNotFound )
{
NSLog(@"supports portrait");
}
range = [supportedOrientationsStr rangeOfString:@"Landscape"];
if ( range.location != NSNotFound )
{
NSLog(@"supports landscape");
}
Upvotes: 1
Reputation: 318804
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];
Upvotes: 3