Swift 5: Detect if a user exits your iOS App

Eric Gustin
1 min readJul 12, 2020

Detecting whether a user has exited your application (e.g. pushed the home button) can be highly beneficial in creating an iOS app that is reactive to what its users are doing. For example, if your app has an online status for its users, then detecting whether a user has exited the app is required for this feature to work.

Upon creating a new Xcode project, you’ll notice that it comes with the SceneDelegate.swift file. The SceneDelegate is essentially in charge of your app’s lifecycle, and it is here where we can detect if a user exits your iOS App. Navigate through SceneDelegate and find the sceneDidEnterBackground function. This is where you will put the code that needs to be executed every time a user exits the app.

As a quick followup, if you would like to detect when the user comes back to your app, then navigate through SceneDelegate.swift once again, but this time direct your attention to the sceneDidBecomeActive function.

If you’d like to learn more about the other functions and uses of SceneDelegate.swift, then check out the official Apple documentation for UISceneDelegate found here: https://developer.apple.com/documentation/uikit/uiscenedelegate

--

--

Eric Gustin

I make tutorials and articles on Programming, iOS Development, and Mathematics github.com/EricGustin