iOS – How to guarantee that applicationWillTerminate will be executed

Is there A way to guarantee that the applicationWillTerminate method in the AppDelegate delegate will be hit? Something like a key in the info.plist file, etc..?

My goal: I’m working in a beacon app, the piece of code is in this article. My problem is that the message from the didEnterRegion keeps poping even when i’m beside the beacon. To solve that I’m setting a flag to control the message. My code below:

  • iOS 8 beta 5 Game Center Sandbox wont recognize my app
  • How to use swift library in NativeScript?
  • Should I use an individual UINavigationController per Tab Bar
  • iOS8 - prevent rotation on presenting viewController
  • How to programmatically increase UITableView cell's height in iPhone?
  • Implement CoreData into an existing project using Swift
  • if(!UserDefaults.standard.bool(forKey: Constants.EnterZoneMsgShowName)){
    
        let notification = UILocalNotification()
        notification.alertBody = "Hi, you are about to arrive at CIDMA's office. Please open de demo app and turn on the bluetooth on your device to enrich your experience. "
        UIApplication.shared.presentLocalNotificationNow(notification)
    
        UserDefaults.standard.set(true, forKey: Constants.EnterZoneMsgShowName)
    } 
    

    I want to set this flag to false when I close the app. I tried to put it at the applicationWillTerminate but this method is not hit every time.

    I would like to know how to guarantee that this code will be hit or if there is a better place to put the code: UserDefaults.standard.set(false, forKey: Constants.EnterZoneMsgShowName)

    Solutions Collect From Internet About “iOS – How to guarantee that applicationWillTerminate will be executed”

    applicationWillTerminate(_:) – Tells the delegate when the app is about
    to terminate.

    For apps that do not support background execution or are linked against iOS 3.x or earlier, this method is always called when the user quits the app.

    For apps that support background execution, this method is generally not called when the user quits the app because the app simply moves to the background in that case. However, this method may be called in situations where the app is running in the background (not suspended) and the system needs to terminate it for some reason.

    What you want to call is applicationDidEnterBackground if your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.