question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

iOS: No way to handle app termination on JS side

See original GitHub issue

I’m trying to do some clean up on JS side (mainly - send a single HTTP request) when app is closed by user. As some answers on SO suggest, I tried to implement applicationWillTerminate method in AppDelegate.m and pass a prop to rootView:

- (void)applicationWillTerminate:(UIApplication *)application
{
  NSMutableDictionary *newProps = [NSMutableDictionary dictionary];
  if (self.rootView.appProperties != nil) {
    [newProps addEntriesFromDictionary:self.rootView.appProperties];
  }
  newProps[@"appWillTerminate"] = @YES;
  self.rootView.appProperties = newProps;
}

and then in my Root view on JS side:

componentWillReceiveProps(nextProps) {
    if (nextProps.appWillTerminate) {
        // Do clean up
    }
}

but JS code inside if clause is not executed.

So I was wondering if there is any other way to do something on JS side when app terminates. I was also looking into AppState component, but it also doesn’t support app termination.

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Comments:9 (7 by maintainers)

github_iconTop GitHub Comments

2reactions
aleclarsoncommented, Mar 4, 2017

Could a Facebook employee that knows more about the native bridge help us?

It would be ideal to handle applicationWillTerminate and background tasks from the JS side.

Edit: It looks like asynchronous code will never complete from within applicationWillTerminate , which means the native bridge cannot work.

Right now, the best you can do is synchronously write your data on the native side. Then, when the application is opened later, read the data and pass it to the JS side. Of course, this means you cannot do any cleanup on the JS side before the application is terminated. You’ll need to send the wanted data from JS to native before applicationWillTerminate is called.

Seems like Apple will have to “fix” this. When/if the native bridge becomes synchronous, you could at least shoot off an HTTPS request (from the JS side) before the application terminates. But asynchronous code would still be off limits.

0reactions
jcharletcommented, Jun 12, 2017

hi @aleclarson , do you have some code to share to show how to write data on native side to handle app termination as you explained…? I’m using asyncStorage at the moment to store data, and I have no idea how to access/store stuff on native side. Any hint/ things you would recommend looking at?

Read more comments on GitHub >

github_iconTop Results From Across the Web

iOS: No way to handle app termination on JS side - React Native
iOS : No way to handle app termination on JS side. Nicolas Charpentier. Moved from https://github.com/facebook/react-native/issues/9348 · November 13, 2016.
Read more >
ApplicationWillTerminate - how do i run JS callback?
Whenever the user is killing the app by home-double-click/swipe up, ApplicationWillTerminate method from AppDelegate is being called, but it ...
Read more >
Determine if the App is terminated by the User or by iOS
In practice, -applicationWillTerminate IS NOT called when your application is terminated, either by the system or by the user, on any recent iOS...
Read more >
How to terminate app in IOS during test - Appium Discuss
Oh… then, I guess apple does not allow to terminate the process in XCTest session… So far, I assume uninstalling the app is...
Read more >
Storing user settings with UserDefaults - a free Hacking with iOS
Go ahead and give the app a try and see what you think – you ought to be able tap the button a...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found