Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
772 views
in Technique[技术] by (71.8m points)

iphone - applicationWillTerminate when is it called and when not

Hi I have read several questions on SO about applicationWillTerminate getting called and not getting called.

I wanted to summarize what I understood as there are several posts that speak differently.

  1. For IOS (without multitasking) it is called always when home button is pressed.

  2. For IOS 4 and above

    a. it is not called when pressing home button (as the app moves to background)

    b. it is called when closing the app from the multi tasking dock and if the app has a sudden terminate flag in info.plist disabled else it is not called. ( I set the "Application should get App Died events" and even then on closing the app from the multitasking dock the terminate function did not get called)

Based on that I had a couple of questions

Is it a good practise to set the Application should get App Died events flag? ( I set the "Application should get App Died events" and even then on closing the app from the multitasking dock the terminate function did not get called)

or

Is registering for "UIApplicationWillTerminateNotification" a better thing to do than the info.plist setting?

Basically I need to do some work only when the app terminates and NOT when it moves to background.

or

EDIT (1): When the app is terminated the following is sent to the APP. How do I catch it?

Program received signal: “SIGKILL”.

EDIT (2):

Please note : It is not getting called in IOS 4 and above when removing from the multitasking dock. You might think it is. But in my case it is not.

I am asking if anyone knows why? Is there something else I am missing.

Also Note I set the "Application should get App Died events" and even then it is not getting called.

EDIT (3):

The answer for the following question also did not work. applicationWillTerminate does not get invoked

Anybody facing the similar issue as me?

Question&Answers:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

In short, unless you have UIApplicationExitsOnSuspend in your Info.plist set to YES, in iOS4 and above there is no guarantee that applicationWillTerminate: will ever get called.

As the documentation says:

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 called in situations where the application is running in the background (not suspended) and the system needs to terminate it for some reason

(Emphasis mine.)

If you need to do something before the app exits you need to do it in applicationDidEnterBackground:. There is no way to catch SIGKILL.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...