bsorrentino / cordova-broadcaster

Cordova Plugin to allow message exchange between javascript and native (and viceversa)
MIT License
113 stars 53 forks source link

iOS Terminate Event #29

Closed kyle9292 closed 3 years ago

kyle9292 commented 6 years ago

I came across this plugin while looking for a way to catch an app terminated event for iOS in my Ionic 3 project and this broadcaster seemed to be just what I needed. I must not be using it right or something b/c I can get events when the app is put to the background but not when it's killed.

I'm also a super noob when it comes to swift/obj c and the iOS lifecycle so if someone has a better suggestion for an iOS app unload/close/exit/terminated event that would easily work with this package I'm definitely open to it.

Here's what I've tried so far...

This works. I can see the console log in my safari web inspector so I know I have the package installed and working as intended...

this.broadcaster.addEventListener('UIApplicationDidEnterBackgroundNotification').subscribe((event) => {
        console.log('ENTER BACKGROUND');
      });

These DO NOT work. As far I can tell, none of these listeners are fired when the app is closed. The appTerminated function sends a request to my api which updates my database so even if the console closes before I can see the log I should see a change in my database.

this.broadcaster.addEventListener('UIApplicationWillTerminate').subscribe((event) => {
  console.log("UIApplicationWillTerminate", event);
  this.appTerminated();
});

this.broadcaster.addEventListener('UIApplicationWillTerminateNotification').subscribe((event) => {
  console.log("UIApplicationWillTerminateNotification", event);
  this.appTerminated();
});

this.broadcaster.addEventListener('applicationWillTerminate').subscribe((event) => {
  console.log("applicationWillTerminate", event);
  this.appTerminated();
});

window.addEventListener('applicationWillTerminate', () => {
  console.log("WINDOW applicationWillTerminate");
  this.appTerminated();
});

window.addEventListener('UIApplicationWillTerminateNotification', () => {
  console.log("WINDOW UIApplicationWillTerminateNotification");
  this.appTerminated();
});

this.broadcaster.addEventListener('UIApplicationExitsOnSuspend').subscribe((event) => {
  console.log('SUSPENDED EXIT');
  this.appTerminated();
});

Finally, I tried adding this bit to the end of CDVBroadcaster.m file in my platforms/ios directory. Still no listeners getting executed.

- (void)applicationWillTerminate:(UIApplication *)application
{

    [[NSNotificationCenter defaultCenter] postNotificationName:@"applicationWillTerminate"
                                                        object:nil
                                                      userInfo:@{@"data": @"test"}];

}

Any help is appreciated.

bsorrentino commented 6 years ago

Hi, thx for feedback

This plugin does't handle automatically system events but you have to map them, as you have tried to do in the attached code (reported below).

- (void)applicationWillTerminate:(UIApplication *)application
{
    [[NSNotificationCenter defaultCenter] postNotificationName:@"applicationWillTerminate"
                                                        object:nil
                                                      userInfo:@{@"data": @"test"}];
}

I suggest you to don't add this code in CDVBroadcaster.m but in your <AppName>AppDelegate.m

I have a doubt concerning the Cordova Lifecycle because i don't know if during application termination Cordova stuff is still running ... i'll investigate on

kyle9292 commented 6 years ago

Hey @bsorrentino,

I moved that piece of code to my AppDelegate.m file along with a printf. I can see my printed message in the xcode console but still no listener activity. Seems like a universal terminated event would go right along with deviceready, pause and resume but like you said if the cordova lifecycle is already ended then an applicationWIllTerminate doesn't do much good.

Thanks for the quick response and looking into this.

mgatto commented 5 years ago

This thread is from a while ago, but I faced the same issue this month and ended up creating a plugin to handle native ios Events which Cordova just stubs: https://github.com/mgatto/cordova-plugin-lifecycle-events-extra

bsorrentino commented 5 years ago

Hi @mgatto

Your code relies on cordova.fireDocumentEvent() so, if it works well, I start to implement a new version using it instead of cordova channels

obkdev commented 4 years ago

Has there been any more news on this? Do we have an event for onAppTerminate in iOS?

bsorrentino commented 4 years ago

Hi @obkdev

I've added event onAppTerminate named willterminate on branch develop. If you have interest in such feature please install plugin directly from there ( cordova plugin add https://github.com/bsorrentino/cordova-broadcaster.git#develop ) test it and let me know

Thanks in advance

bsorrentino commented 3 years ago

Closed due inactivity