iphone - 出现错误后如何以用户友好的方式退出应用程序?

标签 iphone objective-c error-handling nsassert

我需要保护我的代码免受可能的错误的影响。如果它们出现,则没有必要进一步运行应用程序,因此我需要向用户提供一些消息,然后退出应用程序。因此,我正在检查条件,然后发出警报:

if (someError){    
  UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Error" message:@"No database file exist. App will close now." delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil, nil];
  [alert show];
  [alert release]; 
}

在委托(delegate)方法中,我使用 NSAssert 关闭应用程序:

-(void)alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex {
  if (buttonIndex == 0) {
    NSAssert(0, @"closing");
  }
}

此外,我在 header 中包含了委托(delegate)协议(protocol)。但是,该应用程序只是带来警报,但按“确定”后它只是卡住,并且我收到一些消息“CoreAnimation:忽略异常:关闭”。我缺少什么或者还有什么其他选择?

最佳答案

您不应该这样做,这违反了 Apple HIG(人机界面指南):

iPhone applications should never quit programmatically because doing so looks like a crash to the user.

最好向用户提供有关发生的错误的某种反馈,并提供一种无需重新启动应用程序即可重新启动进程的方法。但是,如果您真的非常想要,您可以使用

exit(0);

关于iphone - 出现错误后如何以用户友好的方式退出应用程序?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7481252/

相关文章:

iphone - AudioServicesPlaySystem声音音量控制

c++ - Ubuntu 中对 espeak-ng header 的 undefined reference

php - PHP Whoops错误处理程序电子邮件

windows - 在Windows上安装mxnet时出现无法满足的错误。我正在使用ANACONDA 5.1.0版本和PYTHON 3.5.3版本

ios - 关闭具有多个文本字段的第一响应者/键盘

ios - Objective C 可变数组

iphone - 如何在 UItextView 中添加 placeHolder 文本?在 iPhone SDK 中

iphone - 我应该保持 Sqlite 连接打开吗?

ios - 比较 UITextField 的文本和字符串时 if 语句出错

objective-c - 值/对象的 NSDictionary 键?