Darshan
Darshan

Reputation: 561

Is there any function for finding when app is killed from the multitasking bar when the app state is suspended?

I want to clear the app data when the user removes the app from the app switcher by swiping up after the app is kept in the background about 3 to 4 hours.

Is there any function or delegate to know when the app is killed without coming into the foreground.

I want the login page to be open when the user kills the app from the app switcher. Otherwise, it should open the home page.

Upvotes: 3

Views: 1320

Answers (2)

Jochen Holzer
Jochen Holzer

Reputation: 1736

Solution for Swift:

In your AppDelegate.swift File add the following function (if not present):

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
    // clean your apps data here
    cleanAppData()
    //show the login
    showLogin()
}

This function is only called if the app is "cold started".

This means:

  • it is called if the app was started for the first time
  • if the app was "killed" by iOS
  • if the app was "killed" by the user
  • it is not called if the app runs in the background and is opened again

Then add the following function (if not present) to AppDelegate.swift:

func applicationWillEnterForeground(_ application: UIApplication) {
    // Called as part of the transition from the background to the active state; here you can undo many of the changes made on entering the background.
    // show the homepage
    showHomePage()
}

This function is only called if the app is "warm started" which means if the app was running before and sent to the background by the user.

Upvotes: 3

orkoden
orkoden

Reputation: 19996

You can save the current time in UIApplicationDelegate.applicationWillTerminate(_:) and UIApplicationDelegate.applicationDidEnterBackground(_:) to the user defaults. When the app comes back to the foreground or is launching you read this time from user defaults.

Upvotes: 0

Related Questions