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:

  • “Library not loaded: /usr/local/opt/openssl/lib/libssl.1.0.0.dylib” with Xcode 8
  • Xcode crashing on a specific project
  • Strange Errors on Archive Validation
  • UITextView with hyperlink text
  • error: Invalid bitcode version (Producer: '800.0.35.0_0' Reader: '703.0.31_0')
  • Why is 'Simulate Background Fetch' leading to crash (libsystem_kernel.dylib`mach_msg_trap) since Xcode 8?
  • 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. "
        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.