Reputation: 561
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
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:
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
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