0

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:

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)

Rashwan L
  • 38,237
  • 7
  • 103
  • 107
André Luiz
  • 6,642
  • 9
  • 55
  • 105
  • No there is not, but what are you actually attempting to achieve? – Firo Oct 07 '16 at 15:13
  • Thanks. I have a flag to control is I show some messages to the user. I just show this message once for the user during the app usage. So, when I close my app I want to set this flag so when the user opens again the app I can show the message again. it is a beacon app And I'm getting enter/leave region very often – André Luiz Oct 07 '16 at 15:17
  • 1
    You can achieve the same approch doing the opposite: when the application starts you set a flag to "false", after the user shows the message you set it to "true" and you don't display it anymore. During the next application startup it will be changed to false again and you will be able to do show it again: while you are not sure about the execution of applicationWillTerminate, you are sure about applicationDidFinishLaunching – Marco Pace Oct 07 '16 at 15:19
  • 3
    @AndréLuiz I would edit your question to reflect what you are actually trying to achieve, not asking a question about how you _think_ you should. As Marco pointed out, most of these things are handled on application launch. – Firo Oct 07 '16 at 15:27

1 Answers1

3

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.

Rashwan L
  • 38,237
  • 7
  • 103
  • 107
  • Nice, looks like just what I need. I'm trying to implement but xcode8 give me this message: invalid redeclaration. I can't see this in the list of suggestions as well. Did it change for swift 3? – André Luiz Oct 07 '16 at 16:59
  • Do you mean for `applicationDidEnterBackground`? – Rashwan L Oct 07 '16 at 17:00
  • Yes, I'm trying func applicationDidEnterBackground(_ application: UIApplication){} in the AppDelegate and I'm getting the error. And I can't find in the suggestions as well – André Luiz Oct 07 '16 at 17:03
  • Weird, it should be a standard method, try to clean your project and restart it. It´s Swift 3.0 I´m running. – Rashwan L Oct 07 '16 at 17:05
  • 1
    Sorry, the method was there and I didn't see. Thanks for taking some of your time to answer my question. Have a nice day! – André Luiz Oct 07 '16 at 17:18