tauheed
tauheed

Reputation: 160

Detect when app killed by user in background state iOS

I am working in a chat application where I need to show user status (offline/online). When my app is in foreground and background then I need to show user as online (managing by VoIP). But when the user kill the app then it should go to offline.

I have to maintain a flag to show offline which I am managing in delegate function applicationWillTerminate but this function only called when app is in foreground state and user kill it by pressing double tap home button and swipe up. This function does not get called when app is in background state. I mean simply press home by single tap (app will go in background) then again double tap to swipe up.

Is there any function where I get 100% call either app is in background/foreground state and user kill the app?

Upvotes: 0

Views: 5594

Answers (2)

Aleem
Aleem

Reputation: 3291

No, As per Apple Document

https://developer.apple.com/documentation/uikit/uiapplicationdelegate/1623111-applicationwillterminate?language=objc

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.

What you can do execute a method(which hit an API for keeping status online) after few seconds(whatever you find suitable time) when you app goes in Background, If method is calling successfully after that specific seconds then user stay online, if its not call after specified second then server update its status to offline. So It require both server and client handling.

Upvotes: 0

matt
matt

Reputation: 536047

Is there any function where I get 100% call either app is in background/foreground state and user kill the app?

No. Just the opposite. If your app is terminated when already in the background, if it is suspended (ie not running in the background due to special entitlement), it is 100% certain you will get no event. You cannot. You are suspended and not running. The app dies in its sleep.

Upvotes: 9

Related Questions