André Luiz André Luiz - 1 year ago 295
Swift Question

iOS - How to guarantee that applicationWillTerminate will be executed

Is there A way to guarantee that the

method in the
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:

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
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)

Answer Source

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.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download