skip to Main Content

I’m working on an iOS application and I want the app to terminate after hanging in the background for more than 5 minutes and when the user opens the app again it should display the login. For now the app terminates immediately when it reaches the background and the login screen when the user re-opens the app. I did this by adding a property called "Application does not run in background" to the info.plist file and I set the value to "YES". As I mentioned at the start what I want to do is to terminate the app after 5 minutes in background but not immediately. And the login screen appearing after the user re-opens the app is working fine.

Application does not run in background property in info.plist

2

Answers


  1. I guess you this is managed by the operating system that for how long the app runs in the background, and the os terminates it when it require resources to get free.

    A workaround that could be done is, store the time value in UserDefaults whenever the app goes to background, and whenever the app comes foreground, check for the previous time value. If the difference is greater than 5 minutes, you may direct to login.

    Login or Signup to reply.
  2. You can request background time when you are told that you are being suspended. I believe the max you will get is 3 minutes, not 5. That will prevent your app from being terminated, and keep your app getting CPU time during that time. (Which will use extra battery power.) You will get a notice when that time expires, and you can save your app state and log off at that point.

    Search on "Extending Your App’s Background Execution Time" in the Xcode help system or Apple’s docs for more information. The overview section of that document reads:

    Overview
    Extending your app’s background execution time ensures that you have adequate time to perform critical tasks. For tasks that require more background time, use Background Tasks.
    When your app moves to the background, the system calls your app delegate’s applicationDidEnterBackground(_:) method. That method has five seconds to perform any tasks and return. Shortly after that method returns, the system puts your app into the suspended state. For most apps, five seconds is enough to perform any crucial tasks, but if you need more time, you can ask UIKit to extend your app’s runtime.

    You extend your app’s runtime by calling the beginBackgroundTask(withName:expirationHandler:) method. Calling this method gives you extra time to perform important tasks. (You can find out the maximum background time available using the backgroundTimeRemaining property.) When you finish your tasks, call the endBackgroundTask(_:) method right away to let the system know that you are done. If you do not end your tasks in a timely manner, the system terminates your app.

    The key bits of that are:

    "When your app moves to the background, the system calls your app delegate’s applicationDidEnterBackground(_:) method."

    And "You extend your app’s runtime by calling the beginBackgroundTask(withName:expirationHandler:) method"

    Note that you should really implement an app level "heartbeat" where the app sends periodic "I’m still here" messages to the server to keep the user logged in. Your server should log the user off if they miss more than one heartbeat message.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search