Alexey Alexey - 1 month ago 8
iOS Question

Listen for application terminate from a library/framework?

Is it possible to listen for the iOS application terminate/going to background mode from a library/framework, which is included into that app?

As I see,

dealloc
method (for a class which implemented in the library) does not called in case if whole app terminating (correct me if I'm wrong).

UPD.: The question is about libraries/frameworks. I know about
applicationWillTerminate
method for AppDelegate, but it's for the app itself, not for a library/framework.

Answer

Registering for UIApplicationWillTerminateNotification via NotificationCenter will work.

Code:

-(void)addAppTerminationNotifier
{
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(appWillTerminate) name:UIApplicationWillTerminateNotification object:nil];
}

-(void)appWillTerminate
{
    //..... task to done before termination
}