xcode applicationWillTerminate 不会在 iOS 应用程序强制退出时被调用
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13386505/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
applicationWillTerminate not getting called on force quit of iOS app
提问by Sean Danzeiser
Does anyone have any insights into when/under what conditions applicationWillTerminate is called in iOS 5/6?
有没有人对在 iOS 5/6 中何时/在什么条件下调用 applicationWillTerminate 有任何见解?
I've got some logic i'd like to execute whenever the application terminates (not moves to the background), for example if the user navigates to the application bar at the bottom of the screen by double tapping the home button and force quits the app.
我有一些我想在应用程序终止时执行的逻辑(而不是移动到后台),例如,如果用户通过双击主页按钮导航到屏幕底部的应用程序栏并强制退出应用程序。
when i try to do this on a test device, applicationWillTerminate does not seem to get called. Is there a reason for this?
当我尝试在测试设备上执行此操作时,applicationWillTerminate 似乎没有被调用。是否有一个原因?
My plan B is to tie that logic to some persistent object like a singleton or a static that is automatically destroyed when the app quits.
我的计划 B 是将该逻辑与某些持久对象相关联,例如单例或在应用程序退出时自动销毁的静态对象。
Any suggestions?
有什么建议?
thanks
谢谢
回答by iDev
Have you read the documentation for applicationWillTerminate:,
您是否阅读了applicationWillTerminate:的文档,
It says,
它说,
For applications that do not support background execution or are linked against iOS 3.x or earlier, this method is always called when the user quits the application. For applications that support background execution, this method is generally not called when the user quits the application because the application simply moves to the background in that case. However, this method may be calledin situations where the application is running in the background (not suspended) and the system needs to terminate it for some reason.
对于不支持后台执行或链接到 iOS 3.x 或更早版本的应用程序,在用户退出应用程序时始终调用此方法。对于支持后台执行的应用程序,当用户退出应用程序时,通常不会调用此方法,因为在这种情况下,应用程序只是移动到后台。但是,在应用程序在后台运行(未挂起)并且系统出于某种原因需要终止它的情况下,可能会调用此方法。
There is a "maybe" mentioned there. Probably that answers your question. So it is not necessary that this will get called when you quit the app. Probably you might have to use UIApplicationExitsOnSuspend
to disable multitasking and then it might get called while putting in background. But that again depends on your app requirement. If you cannot disable multitasking, you might have consider doing that in applicationDidEnterBackground
method or so. I am not sure if there are any other delegate methods which will help in identifying the force quit.
那里提到了一个“可能”。大概这回答了你的问题。因此,当您退出应用程序时,它没有必要被调用。可能您可能不得不使用UIApplicationExitsOnSuspend
禁用多任务处理,然后它可能会在进入后台时被调用。但这又取决于您的应用程序要求。如果您无法禁用多任务处理,您可能会考虑在applicationDidEnterBackground
方法中这样做。我不确定是否有任何其他委托方法可以帮助确定强制退出。