项目中遇到的一个头疼的问题。
在ios6.0的模拟器上一启动就会崩溃,第一个界面刚刚显示出来就崩溃,项目是cocos2d的项目,cocos2d的版本位cocos2d-iphone-2.1-rc1。崩溃后的log显示:Terminating app due to uncaught exception 'UIApplicationInvalidInterfaceOrientation', reason: 'Supported orientations has no common orientation with the application, and shouldAutorotate is returning YES'
网上也有很多人有遇到这个错误的,大家给出的答案大部分都是
-(NSUInteger)supportedInterfaceOrientations {
// iPhone only
if( [[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone )
return UIInterfaceOrientationMaskLandscape;
// iPad only
return UIInterfaceOrientationMaskLandscape;
}
// Supported orientations. Customize it for your own needs
// Only valid on iOS 4 / 5. NOT VALID for iOS 6.
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
// iPhone only
if( [[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone )
return UIInterfaceOrientationIsLandscape(interfaceOrientation);
// iPad only
// iPhone only
return UIInterfaceOrientationIsLandscape(interfaceOrientation);
}
检查这两个函数,还有检查info.plist的Supported interface orientations的项目是否添加了相应的支持。我也相应检查了这几项,发现一切都正常。最后还是纠结了很久才发现问题。
1.我在项目启动的时候加入了GameCenter的鉴权,如果用户GameCenter可用,并且没有登录,首先会弹出一个GameCenter的登陆的Controller,这个是系统弹出的。
2.由于我的项目是横屏的,但是这个GameCenter弹出是以竖屏方式弹出的,我的项目里面只是提供了横屏的支持,这个时候就会崩溃。
做了多方面的改进,发现两个解决办法
1.在delegate中加入
- (NSUInteger)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window
{
return UIInterfaceOrientationMaskAll;
}
2. gameCenter的鉴权方式
将
if([GKLocalPlayer localPlayer].authenticated == NO)
{
[[GKLocalPlayer localPlayer] authenticateWithCompletionHandler:^(NSError *error)
{
if (error == nil)
{
}
}];
authenticateWithCompletionHandler这个方法已经被标注位__OSX_AVAILABLE_BUT_DEPRECATED。
改成:
[GKLocalPlayer localPlayer].authenticateHandler =
^(UIViewController *viewController, NSError *error)
{
if (error == nil)
{
}
};
上面这两种方法只需要有一种就可以解决这个问题,ios6.0的模拟器,6.1的sdk的出现的问题。真机没有那么多版本的机器,也没能在真机上测试下。希望能遇到这个问题的同学们一个错误查找的方向