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