How can i detect when user closing app ?

Hey there ! I am working at BLE Connection app. I developing my project with Swift. I made connection , send data , receive data , etc.

Question 1:

I am pairing my BLE device on my iPhone with using CoreBluetooth. I wanna send a message when user closing to app. But i can not find this trigger function. How can i do this ? or It is possible ? When i research this subject on internet, i find something like "applicationWillTerminate" .etc. But when i write this function , xcode not complete my sentence "applicationWillTerminate". I am thinking about this function maybe deprecated ? I can not find any information about this. How can i solve this ?

Question 2:

When is pair my BLE device, i get close my application and open again my app can not find my BLE. Because first time those are paring and when i closed the app those not disconnect. How can i solve this ?
Answered by Claude31 in 650909022
applicationWillTerminate is not deprecated.

You do not call it directly, you include it in the appDelegate class
Code Block
class AppDelegate: UIResponder, UIApplicationDelegate {
    func applicationWillTerminate(_ application: UIApplication) { }
        print("End App")
}

If you have a Scenedelegate, this func may not be present. So you may also call in sceneDidDisconnect

Code Block
class SceneDelegate: UIResponder, UIWindowSceneDelegate {
    func sceneDidDisconnect(_ scene: UIScene) {
        print("End Session")
}
}

Accepted Answer
applicationWillTerminate is not deprecated.

You do not call it directly, you include it in the appDelegate class
Code Block
class AppDelegate: UIResponder, UIApplicationDelegate {
    func applicationWillTerminate(_ application: UIApplication) { }
        print("End App")
}

If you have a Scenedelegate, this func may not be present. So you may also call in sceneDidDisconnect

Code Block
class SceneDelegate: UIResponder, UIWindowSceneDelegate {
    func sceneDidDisconnect(_ scene: UIScene) {
        print("End Session")
}
}

How can i detect when user closing app ?
 
 
Q