问题描述
在我的应用程序的某个时刻,我已经完成了 exit(0)
,这会导致我的应用程序崩溃。但是我还没弄清楚当执行时会调用什么方法。
At some point in my application I have done this exit(0)
which crashes my app. But I haven't figured out what method gets called when this executes.
我已将消息放入:
(void)applicationWillTerminate:(UIApplication *)application
(void)applicationDidEnterBackground:(UIApplication *)application
但这些似乎都没有被调用!当 exit(0)
完成后,有什么方法可以调用吗?
But none of this seem to get called! Any idea about what method is called when exit(0)
is done?
推荐答案
来自Apple的人类用户指南......
From Apple's Human User Guidelines...
显示一个有吸引力的屏幕,描述问题并建议
更正。屏幕提供反馈,向用户保证
您的应用程序没有任何问题。它让用户掌控,
让他们决定是否要采取纠正措施,
继续使用你的应用程序或按Home键并打开
不同的应用程序
Display an attractive screen that describes the problem and suggests a correction. A screen provides feedback that reassures users that there’s nothing wrong with your application. It puts users in control, letting them decide whether they want to take corrective action and continue using your application or press the Home button and open a different application
如果只有部分应用程序功能不起作用,请在人们激活该功能时显示屏幕或警报
。仅当人们尝试访问不是
功能的功能时,才会显示
警报。
If only some of your application's features are not working, display either a screen or an alert when people activate the feature. Display the alert only when people try to access the feature that isn’t functioning.
如果你已经决定你要以编程方式退出...
在C中,退出(0)
将停止执行应用程序。这意味着不会调用任何委托方法或异常处理程序。因此,如果目标是确保在关闭时调用某些代码,即使在强制关闭时,也可能有另一种选择。在 AppDelegate
中实现一个名为 - (void)applicaitonIsgoingAway
的自定义方法。在您希望调用现有代码的任何地方调用此方法:
If you've decided that you are going to quit programmatically anyway...
In C, exit(0)
will halt execution of the application. This means that no delegate methods or exception handlers will be called. So, if the goal is to make sure that some code gets called when the closes, even on a forced close, there may be another option. In your AppDelegate
implement a custom method called something like -(void)applicaitonIsgoingAway
. Call this method from within anywhere you want your exiting code to be called:
-
applicationWillTerminate
-
applicaitonDidEnterBackground
-
onUncaughtException
applicationWillTerminate
applicaitonDidEnterBackground
onUncaughtException
前两个是您在问题中已经提到过的。第三种可以是各种各样的。它是一个全局异常处理程序。接下来的内容来自。
The first two are ones that you already mentioned in your question. The third can be a catch-all of sorts. It's a global exception handler. This next bit comes from a question on that very topic.
这个异常处理程序将被调用任何无法处理的异常(否则会导致应用程序崩溃)。在此处理程序中,您可以调用 applicaitonIsgoingAway
,就像在其他两种情况下一样。从我上面提到的另一个问题,你可以找到类似的答案。
This exception handler will get called for any unhanded exceptions (which would otherwise crash your app). From within this handler, you can call applicaitonIsgoingAway
, just like in the other 2 cases. From the other question that I mentioned above, you can find an answer similar to this.
void onUncaughtException(NSException* exception)
{
[[AppDelegate sharedInstance] applicationIsgoingAway];
}
但为了使其正常工作,您需要将此方法设置为像这样的异常处理程序...
But in order for this to work, you need to set this method up as the exception handler like so...
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
NSSetUncaughtExceptionHandler(&onUncaughtException);
//There may already be more code in this method.
}
现在,您可以通过调用以编程方式退出应用程序NSAssert(FALSE,@以编程方式退出应用程序。);
只要没有其他异常处理程序来捕获它,您的应用程序将开始崩溃,并且您的异常处理程序代码将是调用。转而致电 applicationIsGoingAway
。
Now, you can quit the app programmatically by calling NSAssert(FALSE, @"Quitting the app programmatically.");
As long as there is no other exception handler in place to catch this, your app will begin to crash, and your exception handler code will be called. in-turn calling applicationIsGoingAway
.
这篇关于在iPhone应用程序中调用exit(0)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!