iOS 8.3中UIAlertView崩溃了

11

最近我开始收到针对UIAlertView的崩溃报告,仅来自使用iOS 8.3的用户。

Crashlytics报告:

Fatal Exception: UIApplicationInvalidInterfaceOrientation Supported orientations has no common orientation with the application, and [_UIAlertShimPresentingViewController shouldAutorotate] is returning YES

导致崩溃的代码行是[alertView show]:

UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:title
                                                    message:message
                                                   delegate:nil
                                          cancelButtonTitle:cancelButtonTitle
                                          otherButtonTitles:nil];
[alertView show];

这段代码在应用程序中已经存在很长时间,现在开始崩溃了。有人经历过类似的问题并解决了吗?


1
在iOS 8中,它已被弃用。 - Sarat Patel
2
我知道-但是弃用的东西仍应该能够正常工作,并且在iOS 8.0、8.1和8.2中它表现良好。由于我的App还支持iOS 7,而我不想为UIAlertView调用引入条件语句,所以我决定仍然保留旧代码,只要它被支持。 - Simon Meyer
我在使用UIAlertControllers时遇到了相同的错误。子类化UIAlertController(或UIAlertView)应该可以解决这个问题。 - Maxwell
@Maxwell Apple在文档中明确表示不要子类化UIAlertViewUIAlertController。请参见下面的答案。 - stevekohls
@stevekohls 你说得对。我应该在哪个类中重写这些方法?我尝试了呈现UIAlertController的视图控制器,但崩溃仍然存在。 - Maxwell
6个回答

10

主要问题是:

UIApplicationInvalidInterfaceOrientation Supported orientations 与应用程序没有共同的方向

这意味着您在某个地方实现了

- (NSUInteger)supportedInterfaceOrientations {

    return UIDeviceOrientationPortrait; // or UIInterfaceOrientationPortrait
}

UIInterfaceOrientationPortrait = UIDeviceOrientationPortrait = 0

In that function MUST be returned Mask like:

UIInterfaceOrientationMaskPortrait which is 1


9

尝试通过实现这个来解决问题。

- (BOOL)shouldAutorotate {
   return NO;
}

-(NSUInteger)supportedInterfaceOrientations {
    return UIInterfaceOrientationMaskPortrait;
}

还有这个要注意:在 iPhone 上打开相机时,iOS 7 中横向模式的方向问题


请注意,此链接提供有关在iPhone上使用相机时出现方向问题的信息。

我以为我已经做好了,但我不小心使用了UIDeviceOrientationPortrait而不是UIInterfaceOrientationMaskPortrait。 - Simon Meyer
已添加上述方法,但我的应用程序仍存在问题。 - Abhishek
这解决了我的问题。替换Alertview会是一大堆工作 :) - anoop4real

1
比起这个,你应该开始使用 UIAlertController。它具有更好的功能,而且对于 UIAlertAction,你不需要包含委托方法。
UIAlertController * alert=   [UIAlertController
                             alertControllerWithTitle:@"Info"
                             message:@"You are using UIAlertController"
                             preferredStyle:UIAlertControllerStyleAlert];

UIAlertAction* ok = [UIAlertAction
                    actionWithTitle:@"OK"
                    style:UIAlertActionStyleDefault
                    handler:^(UIAlertAction * action)
                    {
                        [alert dismissViewControllerAnimated:YES completion:nil];

                    }];

UIAlertAction* cancel = [UIAlertAction
                        actionWithTitle:@"Cancel"
                       style:UIAlertActionStyleDefault
                       handler:^(UIAlertAction * action)
                       {
                           [alert dismissViewControllerAnimated:YES completion:nil];

                       }];

[alert addAction:ok];

[alert addAction:cancel];

[self presentViewController:alert animated:YES completion:nil];

0

在尝试这些解决方案后,没有一个对我起作用。我在一个应用程序中支持iOS 6-8,并且还使用一些内部使用UIAlertView的库,因此简单地条件编译以在可用时使用UIAlertController不是一个选项。

我想出了一个解决方法,为我解决了这个问题。你可能会有不同的经历。我将头文件包含在头部前缀文件中,以确保它在任何地方都会被包含到UIAlertView显示。

我在这里发布这个问题,希望能对那些遇到这个问题而网上找到的解决方案无效的人有所帮助。希望对你有帮助。

https://gist.github.com/joshhudnall/cdc89b61d0a545c85d1d


0

0

我创建了一个辅助工具,用于在iOS8之前显示UIAlertView,在iOS8之后显示UIAlertController,这解决了旋转崩溃问题,并且在所有版本中都能很好地显示。

https://github.com/dannyshmueli/DSAlertDisplayer


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