2

I am working with a game that basically operates on landscape mode. It has an option of changing Profile Pic. The profile pic option opens the Image Picker which is used to display on the profile pic Image View. My issue is

When image picker is opened , the app automatically changes orientation to portrait.

For solving this, I tried to capture the orientation of the user when he clicks the upload image button and after selecting the image , I am forcefully changing the device orientation to previous one. Everything is working fine on iPhone 5 and higher versions. But iphone 4(running on ios7) crashes when the user selects an image from picker view. I am getting the following error -

preferredInterfaceOrientationForPresentation must return a supported interface orientation!

I am using this code to check the orientation of user

-(void)checkCurrentDeviceOrientation
{
if([UIDevice currentDevice].orientation == 
UIDeviceOrientationLandscapeRight || [UIDevice 
currentDevice].orientation == UIDeviceOrientationLandscapeLeft )
{
self.currentOrientation = [UIDevice currentDevice].orientation;
}
else
{
self.currentOrientation = UIDeviceOrientationLandscapeRight;
}
}

and using this code to set the orientation.

 if(SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(@"8"))
 {
 if([UIDevice currentDevice].orientation == 
UIDeviceOrientationPortrait ||[UIDevice currentDevice].orientation == 
UIDeviceOrientationPortraitUpsideDown  )
 {

    NSLog(@"Chnaging Device Orientation to stored orientation");
    NSNumber *value = [NSNumber numberWithInt:self.currentOrientation];
    [[UIDevice currentDevice] setValue:value forKey:@"orientation"];
}
}
else
{
//Code For IPHONE 4
}

Hopefully everything works fine on ios8 and higher but not on iphone 4 running on ios 7.0.1

Thanks In Advance!

Kunal
  • 59
  • 2
  • 4
  • Possible duplicate of [preferredInterfaceOrientationForPresentation must return a supported interface orientation](http://stackoverflow.com/questions/12690963/preferredinterfaceorientationforpresentation-must-return-a-supported-interface-o) – koen May 02 '17 at 13:10

1 Answers1

0

I would try and find the device orientation currently and see if the device needs to be forced to rotate. you also may need to add Portrait and Landscape capibility to your plist file and then any screen you DONT want to auto rotate you can add a method to call.

For Example you caould add a method like this in your app delegate with a accesible variable shouldAllowRotation:

-(UIInterfaceOrientationMask)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window
{
    if(self.shouldAllowRotation)
        return UIInterfaceOrientationMaskAll;
    else
        return UIInterfaceOrientationMaskPortrait;
}

And then in each view you could call

[[AppDelegate instance] setShouldAllowRotation:NO];

Depending on whether it should rotate or not. When the picker is called just call into the app delegate and let it auto rotate and this will allow you to get

- (BOOL)shouldAutorotate
{
    return YES;
}

- (NSUInteger)supportedInterfaceOrientations
{
    return UIInterfaceOrientationMaskLandscape;
}

- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation
{
    return UIInterfaceOrientationLandscapeRight;
}

HOWEVER if you are pushing a view controller to a stack of other view controllers in a navigation controller, requiring landscape only will not work well. You should display the landscape-constraint view controller modally.

fluxingjoe
  • 48
  • 9