GameCenter认证在仅支持横屏的应用程序中抛出UIApplicationInvalidInterfaceOrientation异常

23
问题: 如果用户没有登录GameCenter帐户,则会在纵向模式下启动GameCenter身份验证视图(在iOS 5中存在模态对话框),要求进行登录。但是,如果我在xcode中禁用了竖屏模式(项目摘要)或在supportedInterfaceOrientationsForWindow:中禁用了它(因为我的应用程序仅在横向模式下运行),则会出现以下错误: Terminating app due to uncaught exception 'UIApplicationInvalidInterfaceOrientation', reason: 'Supported orientations has no common orientation with the application, and shouldAutorotate is returning YES' 如果我为iPad / iPhone启用Portrait模式(和/或注释掉supportedInterfaceOrientationsForWindow:),则可以正常工作,但我不想启用Portrait模式。

我知道你已经找到了一个解决方法,但这听起来像是一个 bug,你应该在 http://bugreporter.apple.com 上向苹果报告它。 - Robotic Cat
这是一个已知的问题,详见 iOS 6.0 发布说明 中的 Game Center 部分。这个答案 提供了官方的解决方法。 - voyce
5个回答

27

在撰写这个问题并尝试了一些代码后,似乎我找到了解决方案:在项目概述中启用所有方向,并删除应用程序:supportedInterfaceOrientationsForWindow。

将以下代码添加到 ViewController 中:

- (NSUInteger)supportedInterfaceOrientations {
    return UIInterfaceOrientationMaskLandscape;
}

现在它能够无缝运行。


怎么做呢?也许你忘了什么?总结?或者你有很多视图(而我的应用程序只使用OpenGL视图)? - Tertium
2
这个解决方案稍微好一些:https://dev59.com/vGcs5IYBdhLWcg3w64Ca - jonathanpeppers
我曾经遇到过这个问题,但在模拟器上运行良好,只有在硬件上崩溃了。 - i_am_jorf
@Tertium,你知道在cocos2d游戏中如何实现相同的功能吗?非常感谢你的帮助。 - Tuhin Bhatt
1
@jonathanpeppers链接的解决方案(https://dev59.com/vGcs5IYBdhLWcg3w64Ca)在我的情况下效果更好。这个解决方案修复了iOS 6中的问题,但在iOS 5下导致了应用程序的方向异常(我认为这与在项目摘要中激活所有方向有关)。另一方面,所链接的解决方案似乎修复了这些问题,并且在摘要中显示了正确的方向。(最后一点说明:如果实施其他解决方案,请阅读我在其下面的评论)。 - Steph Thirion
显示剩余2条评论

6

添加到应用程序委托:

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

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

}

1

我发现问题出在我的情况下是来自游戏中心。当在模拟器中时,我还没有初始化游戏中心,它想要弹出登录视图,但是以纵向模式。一旦到达这个点,如果我不允许纵向方向,则会崩溃。操作系统中的奇怪错误,因为游戏中心应该只采用允许的方向,以符合我们横向用户界面的意图。

我还没有解决方案,但如果我找到了,我会发布的。


1
是的,就像帖子标题所写的那样,“仅限横屏的GameCenter身份验证”中出现了问题。似乎GC在iOS6上没有为iPhone的身份验证屏幕提供横向资源,因此我怀疑您不会找到真正的解决方案-只有在这里描述的解决方法。这就是为什么当GC想要时,您必须允许应用程序以纵向模式启动-但在主视图中拒绝纵向-如果您不想要纵向的话。顺便说一句,在iOS6上的iPad中没有这样的问题。 - Tertium

0

我跟你遇到了同样的问题,但是我找到了一个有点丑陋的解决方法:在我的应用程序中有一个全局变量,我使用它来选择有效的界面方向。在……

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

要声明全局变量,请将以下代码放入您的appDelegate.m文件中:

          int orientationIndicator = 1;

要导入全局变量,请使用:

          extern int orientationIndicator;

然后您可以更改方向指示器的值,它将允许您以不同的接口类型运行。因此,我首先通过使orientationIndicator = 1来开始。当您验证玩家并启动登录视图控制器时,将方向指示器设置为2。 当您解除视图(验证玩家)时,然后可以将其更改回1。

这是一个巧妙的解决方法,但对我有用。

希望这能帮助到您!


0

对我来说,捕获异常似乎完全正常:

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

iOS 6.0中会抛出异常,但如果你捕获了它,则viewController仍将显示,并且GameCenter在横向方向上的行为与预期相符。

另一种解决方案是只针对iOS 6.1及以上版本进行定位,因为Apple在该版本中修复了该错误。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接