I want to close my app after a while in background mode

I am sorry for my poor English. I know that a callback of timer is not called in background mode. I know that I should add capabilities, permissions and keys into the Info file for doing something in background mode. But I do not need any complicated task in background mode, just need to terminate my app automatically after 30 minutes in background mode. Is there a simple way for that purpose?

just need to terminate my app automatically after 30 minutes in background mode.

Why?

The standard lifecycle for an iOS app is that, when the user moves it to the background, the system suspends it. A suspend app lurks in the background indefinitely. That way, if the user brings it to the front, it can be quickly resumed rather than relaunched. However, if the system comes under memory pressure it will terminate the app, removing it from memory.

It sounds like you want to limit the amount of time your app will stay suspended in the background. Why is that?

Share and Enjoy

Quinn “The Eskimo!” @ Developer Technical Support @ Apple
let myEmail = "eskimo" + "1" + "@" + "apple.com"

Thank you eskimo for your reply.

My app communicates my server through http(s). We know that http(s) is connectionless basically. So, My server removes a session for my app after 30 minutes if there is no communications between them. But, my app can not recognize the lost of the session if it sleeps in background. That makes some unpredictable situation. My app in background even can not send alive ping message to my server to maintain the session because it sleeps deeply in background mode in iOS. I think there is a workaround that my app restarts business logic when it is brought up foreground. But my app needs some complicated sequence for startup. I cannot handle a situation if a user send my app to back/foreground quickly many times in few seconds if I apply that workaround. The best simple way is to request iOS that 'please terminate my app if a user does not bring it foreground within 30 minutes'. Or, terminate it myself.

Thanks for the explanation.

You wrote:

The best simple way is to request iOS that 'please terminate my app if a user does not bring it foreground within 30 minutes'.

iOS has no such facility.

The approach I recommend is that you fix your code so that it works correctly after being suspended in the background for a long period of time. If you’re not able to do that, you’ll have to do this:

Or, terminate it myself.

Calling exit from the foreground is not something we support. There’s an ancient, but still valid, Q&A that explains this: QA1561 How do I programmatically quit my iOS application?

However, calling exit from the background avoids the problems discussed in that Q&A. What you can do is:

  1. When you go into the background, start a UIApplication background task.

  2. If you come into the foreground before the task expires, you were never suspended and thus you can just end the background task and carry on.

  3. If the background task expires, call exit.

For more information about UIApplication background tasks, see UIApplication Background Task Notes.

IMPORTANT Background task time is nowhere near 30 minutes. You’ll get a few minutes at best. The above will help if the user is bouncing quickly between your app and some other app, but there are circumstances where it’ll be kinda ugly. Imagine the user is in your app and receives a message. They switch to Messages for a few minutes to reply, and then come back to your app. Your app will have terminated, meaning the user will have to wait for it to launch rather than resume.

And that’s why my main recommendation here is that you fix your code to be tolerant of being suspended for a long period of time.

Share and Enjoy

Quinn “The Eskimo!” @ Developer Technical Support @ Apple
let myEmail = "eskimo" + "1" + "@" + "apple.com"

Thank you for your kindly explanation.

I understand what options I have. The few minutes iOS provide for background task is not good for me. I understood that it causes ugly user experience when my user comes back to my app just after doing something in other apps for few minutes if I apply the terminating my app in background task. OK, I choose a direction that I do refactoring my startup codes to reduce and optimize enough to running it on every bringing up to foreground.

Thank you for your advice again.

I want to close my app after a while in background mode
 
 
Q