I am working on an application which will not work if terminated. It has some background tasks. I want to show a local notification if the app is terminated. There are applications which do this which means this is doable. But I am not able to find out a way.
I have tried to set up a local notification in applicationWillTerminate: method of appdelegate as well as added a notification of app termination in my viewcontroller but none of the methods get called when app is actually terminated.
- (void)applicationWillTerminate:(UIApplication *)application
{
NSLog(@"terminated");
UIApplication * app = [UIApplication sharedApplication];
NSDate *date = [[NSDate date] dateByAddingTimeInterval:15];
UILocalNotification *alarm = [[UILocalNotification alloc] init] ;
if (alarm) {
alarm.fireDate = [NSDate date];
alarm.timeZone = [NSTimeZone defaultTimeZone];
alarm.repeatInterval = 0;
alarm.alertBody = @"This app does not work if terminated";
alarm.alertAction = @"Open";
[app scheduleLocalNotification:alarm];
}
[app presentLocalNotificationNow:alarm];
// Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
}
Any help would be great.
Thanks in Advance !!!
You will not receive any notice when it gets terminated, when your app is suspended in the background.
iOS will send a kill -9 signal for your apps progress and you app is just killed, this is the same thing that happens when the user kills your app from the quicklaunch tray.
From the Apple documentation:
From the app double press on home button and swipe to kill app always calls applicationWillTerminate. If you do single press and send the app in background and then double press again to swipe and kill the app, applicationWillTerminate may not be called every time.
I was able to schedule local notification in the appWillTerminate, using below code:
Applications can create a local notification at a "future" date and time which could be used to notify the user that the application was terminated. If they then tap the application they can then restart your app.
This is working in my app that uses/requires Bluetooth Central in the info.plist (so it will run in the background). I assume that you will have configured your application to run in the background in your info.plist as well.
I was having the same issue as yourself. However, I discovered if I didn't set a fireDate it started to work.
I must warn you though, it doesn't work on an empty application. It does work on my application which has plenty of views in memory, therefore it must be related to the amount of time it takes to dealloc its memory.
W