GameCenter authentication in landscape-only app throws UIApplicationInvalidInterfaceOrientation

自古美人都是妖i 提交于 2019-11-26 12:28:03

问题


Problem: If user is not logged into GameCenter account - GameCenter authentication view is launched in portrait mode (in ios 5 there were a modal dialog) asking to log in. But if I disable Portrait mode in xcode (Project Summary) or in supportedInterfaceOrientationsForWindow: (as my app supposed to run in landscape mode ONLY) I get:

Terminating app due to uncaught exception \'UIApplicationInvalidInterfaceOrientation\', reason: \'Supported orientations has no common orientation with the application, and shouldAutorotate is returning YES\'

If I enable Portrait for ipad/iphone (and/or comment out supportedInterfaceOrientationsForWindow:) it works without crash, but I don\'t want portrait mode to be enabled.


回答1:


While writing this question and experimenting with code, it seems that I've found a solution: enable all orientations in project summary and remove application:supportedInterfaceOrientationsForWindow.

Add this code to ViewController:

- (NSUInteger)supportedInterfaceOrientations {
    return UIInterfaceOrientationMaskLandscape;
}

Now it works seamlessly.




回答2:


Add to app delegate:

- (NSUInteger)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)w {

return (NSUInteger)[application supportedInterfaceOrientationsForWindow:w] | (1<<UIInterfaceOrientationPortrait);

}



回答3:


I have found that the problem is coming from the Game Center in my case. When in the simulator I do not have the Game Center initialized yet, it would like to pop up the login view, but in portrait mode. Once it is reaching this point it crashes if I disallowed portrait orientation. Strange bug in the OS as Game Center should take the allowed orientations only to be inline with our intention of landscape user interface.

I do not have the solution yet, but I will post if I find it.




回答4:


I had the same issue as you and I fixed it with a kinda, ugly work around, basically I have a global variable in my app that I use to choose what the valid interface orientations are. In the

    - (NSInteger)application : (UIApplication *)supportedInterfaceOrientationsForWindow:(UIWindow *)window{
          if(orientationIndicator == 1){
               return UIInterfaceOrientationMaskAllButUpsideDown;
          }
          else if(orientationIndicator == 2){
               return UIInterfaceOrientationMaskLandscape;
          }
     }

To declare the global variable put this in your appDelegate.m file :

          int orientationIndicator = 1;

To import the global variable use :

          extern int orientationIndicator;

Then you can change the value of orientation indicator and it will allow you to run in different interface types. So what I did was I start by making the orientationIndicator = 1. When you authenticate a player and initiate the login view controller set the orientation indicator to 2. When you dismiss the view (authenticate the player) then you can change it back to 1.

This is a slimy work around but it has worked for me.

Hope this helps!




回答5:


Catching the exception appears to work just fine for me:

@try {
    [rootNavigationController pushViewController:viewController animated:YES];
}
@catch (NSException *exception) {
    //somehow, catching this exception just allows the view controller to be shown?
}

In iOS 6.0, the exception is thrown, but if you catch it then the viewController will still be shown and GameCenter will behave as expected in landscape orientation.

An alternate solution is just to target iOS 6.1 and above, as Apple fixed the bug by that release.



来源:https://stackoverflow.com/questions/12427979/gamecenter-authentication-in-landscape-only-app-throws-uiapplicationinvalidinter

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!