Titanium JIRA Archive
Titanium SDK/CLI (TIMOB)

[TIMOB-23506] Add event listener for applicationWillTerminate

GitHub Issuen/a
TypeImprovement
PriorityCritical
StatusClosed
ResolutionFixed
Resolution Date2016-06-13T21:17:15.000+0000
Affected Version/sn/a
Fix Version/sRelease 6.0.0
ComponentsiOS
LabelsapplicationWillTerminate, ios, titanium
ReporterUriel Lizama
AssigneeHans Knöchel
Created2016-06-13T16:01:42.000+0000
Updated2016-10-05T16:42:44.000+0000

Description

I have an app that has a background service enabled, the user can elect to get a notification that alerts of the app being killed so that the user can manually restart the app. There are some other apps that have this feature, examples are Mile IQ and Kevo (http://stackoverflow.com/questions/22938387/how-to-detect-if-an-ios-app-is-killed-when-it-is-not-in-forground-ios7). When the app is about to shutdown iOS seems to call *applicationWillTerminate* and gives about 5 seconds, which is enough time to trigger a local notification. Looking at the Titanium SDK code, you are already listening for this event, it would be great if we could surface it as a *Titanium.App.addEventListener('willterminate')* event.

Comments

  1. Hans Knöchel 2016-06-13

    Hey there, I had a quick look into our existing code-base and there is an (undocumented) event close which can be accessed like this:
       Ti.App.addEventListener("close", function() {
           // Application will terminate
       });
       
    So different to the close event in Ti.UI.Window, this one gets triggered by the core when reaching the - (void)applicationWillTerminate:(UIApplication *)application delegate. Give it a try! I will add the docs if it works for you. Thanks!
  2. Hans Knöchel 2016-06-13

    PR: https://github.com/appcelerator/titanium_mobile/pull/8056 Demo: Paste the below code and quit the app using multitasking. The below log applicationWillTerminate should be shown in the console.
       var win = Ti.UI.createWindow({
         backgroundColor : "#fff"
       }); 
       
       Ti.App.addEventListener("close", function() {
           Ti.API.warn("applicationWillTerminate");
       });
       
       win.open();
       
  3. Uriel Lizama 2016-06-13

    What SDK version are you using? I have: {noformat} Titanium.App.addEventListener("close", function() { Ti.API.warn("applicationWillTerminate"); var notification = Ti.App.iOS.scheduleLocalNotification({ alertBody: 'You app is terminating', date: new Date(new Date().getTime() + 100) }); }); {noformat} I open the app, move it to background and then remove it using multitasking, but no warning and no notication. I'm on 5.2.2.GA
  4. Abir Mukherjee 2016-09-28

    With this environment: NPM Version: 2.15.9 Node Version: 4.5.0 Mac OS: 10.11.6 Appc CLI: 5.5.0 Appc CLI NPM: 4.2.7 Titanium SDK version: 6.0.0.v20160927062927 Appcelerator Studio, build: 4.7.1.201609100950 Xcode 8.0 GM I created and installed an app using Han's demo code. I opened it, moved it to the background, and then removed it using multitasking, but there was no warning. This was done on a device (iPhone 6S Plus, iOS 10.0.1). In short, I believe the issue is not resolved.
  5. Hans Knöchel 2016-10-01

    [~amukherjee] Did you use the Console.app to watch the logs? There is an known issue regarding logging (TIMOB-23786) that is currently being investigated. I will retry the PR as well, but to be sure, use the Console.app for device-logs. Thx! *EDIT*: Just tried, looks good:
       2016-10-01 11:22:32.172 Titanium[1147:23479] [INFO] The application is being shutdown
       2016-10-01 11:22:32.173 Titanium[1147:23479] [WARN] applicationWillTerminate
       
  6. Abir Mukherjee 2016-10-01

    I had checked the Console Window in Studio. But I will check Console.app and report back. Update: I tried using iOS SImulator, and did see the warning. I don't have an actual device here; so I'll try that on Monday.
  7. Abir Mukherjee 2016-10-05

    Using this environment: NPM Version: 2.15.9 Node Version: 4.5.0 Mac OS: 10.11.6 Appc CLI: 5.5.0 Appc CLI NPM: 4.2.7 Titanium SDK version: 5.5.0.GA Appcelerator Studio, build: 4.8.0.201609290836 Xcode 8.0 GM I was able to verify that the warning was shown in the console with iOS simulator. Also, with a device, a warning message was shown on the console with an iOS 9.2 phone. There was no warning when using a iOS 10.0 phone.
  8. Hans Knöchel 2016-10-05

    That's only because of TIMOB-23786.
  9. Abir Mukherjee 2016-10-05

    Ok, since we have a ticket for iOS 10 device, I'll close this one.

JSON Source