UPGRADE YOUR SKILLS: Learn advanced Swift and SwiftUI on Hacking with Swift+! >>

SOLVED: SwiftUI - Any way to tell that the app has terminated

Forums > SwiftUI

You have an app open, then swipe the home button to go to the home page. App goes into the backgorund. Lots of things you can do with code here.

App is in the background (has background enabled). You swipe up the home button and then swipe up the app, throwing it away. With iOS13, is there any place that code can be run (a quick 1 second of code)?

In the appDelegate the old "applicationWillTerminate" no longer functions. The notification for "UIApplication.willTerminateNotification" doesn't appear to work.

I believe because of the new "Scene" way of doing things, this is no longer an option. Just making sure that is correct. (I read on Stack Overflow that you can change the behavior of the entire app, sort of making it like the old way. I think it started with deleting the "Application Scene Manifest" from the info.plist, or an entry in that. That seems extreme, so I'm not sure I would want to go there.)

2      

Yes, there is new UIScene notification to use:

NotificationCenter.default.addObserver(self, selector: #selector(onBackground), name: UIScene.willDeactivateNotification, object: nil)

2      

So I tested the above on a real project. In this project, when you swipe up the home button, it goes into the background and stays running (and I confirmed that it stays running).

in the @Objc func onBackground, I just put a badge count change (changes it to 888 for testing purposes). As soon as I swiped just the home button, the badge changed to 888. So the UIScene.willDeactivateNotification notified it went into the background, but the app was still running at the time, which isn't what I'm looking for. I'm looking a notification that tells me app that is supposed to running in the background stops running, so I can do something. I'm not sure it exists for iOS13.

2      

If you create a new SwiftUI app in the created Scene Delegate is a lot of boilerplate code. This doesn't fit your needs or did I misunderstand your problem?

    func sceneDidDisconnect(_ scene: UIScene) {
        // Called as the scene is being released by the system.
        // This occurs shortly after the scene enters the background, or when its session is discarded.
        // Release any resources associated with this scene that can be re-created the next time the scene connects.
        // The scene may re-connect later, as its session was not neccessarily discarded (see `application:didDiscardSceneSessions` instead).
    }

...

2      

TAKE YOUR SKILLS TO THE NEXT LEVEL If you like Hacking with Swift, you'll love Hacking with Swift+ – it's my premium service where you can learn advanced Swift and SwiftUI, functional programming, algorithms, and more. Plus it comes with stacks of benefits, including monthly live streams, downloadable projects, a 20% discount on all books, and free gifts!

Find out more

Sponsor Hacking with Swift and reach the world's largest Swift community!

Archived topic

This topic has been closed due to inactivity, so you can't reply. Please create a new topic if you need to.

All interactions here are governed by our code of conduct.

 
Unknown user

You are not logged in

Log in or create account
 

Link copied to your pasteboard.