applicationDidEnterBackground and applicationWillTerminate both called when user swipes up to quit app - objective-c

I am adding some notifications for when the user sends the application to the background or when they completely quit the app. However when they quit the app, both the methods applicationDidEnterBackground and applicationWillTerminate are called. Why is this so and how can I just have applicationWillTerminate get called when the user quits the app?
This is objective-c for if anyone is wondering.

applicationWillTerminate method will be called only when application is not in suspended state while being terminated.
Following links may help you -
Which function is called when iPhone app is terminated?
applicationWillTerminate when is it called and when not

Seeing that applicationWillTerminate is called after applicationDidEnterBackground I have decided to set a notification.fireDate of 1sec from the point at which applicationDidEnterBackground is called. When applicationWillTerminate is called it first cancels the notification that was scheduled within applicationDidEnterBackground.
Pseudocode:
-applicationDidEnterBackground()
{
[self scheduleLocalNotificationAtTimeIntervalFromNow:1.0f identifier: #"someIdentifier"];
}
-applicationWillTerminate
{
[self cancelLocalNotificationWithIdentifier: #"someIdentifier"];
[self scheduleLocalNotificationAtTimeIntervalFromNow:0.0f identifier: #"irrelevantIdentifier"];
}

Related

Application session timeOut in cocoa application

Hi friends i'm developing MAC desktop application using cocoa. I want to add session time out for in the app. Example my application running in background user dont touch and do nothing in app. After 20(we need to set) app will return home page(login page) for session time out.
Will help me how to set session in cocoa application
Use a custom NSApplication class and override sendEvent:. Something like this:
- (void)sendEvent:(NSEvent *)event
{
[super sendEvent:event];
[NSObject cancelPreviousPerformRequestsWithTarget:self selector:#selector(applicationSessionTimeout:) object:nil];
[self performSelector:#selector(applicationSessionTimeout:) withObject:self afterDelay:SESSION_TIMEOUT];
}
Basically all mouse and keyboard events enter your app through this method. You just need to override it to set your timers.

Is it possible to trigger the didRecieveLocalNotifcation method if the app is closed

In app delegate, I have written some code in the didRecieveLocalNotification Method which firstly determines which local notification was triggered, and then generates a UIAlert once the app re opens after clicking the notification banner.
If my app is closed, the local notification is still received, and clicking on it does re open the app from its terminated state, however the code inside of the didRecieveLocalNotification method is not triggering at all. I can't even get a NSLog to work.
Anything I can do to fix this?
Have a look at https://developer.apple.com/library/ios/documentation/iphone/Reference/UILocalNotification_Class/Reference/Reference.html
You can get this information in application:didFinishLaunchingWithOptions, but only if user taps the local notification.
When the system delivers a local notification, several things can happen, depending on the application state and the type of notification. If the application is not frontmost and visible, the system displays the alert message, badges the application, and plays a sound—whatever is specified in the notification. If the notification is an alert and the user taps the action button (or, if the device is locked, drags open the action slider), the application is launched. In the application:didFinishLaunchingWithOptions: method the application delegate can obtain the UILocalNotification object from the passed-in options dictionary by using the UIApplicationLaunchOptionsLocalNotificationKey key. The delegate can inspect the properties of the notification and, if the notification includes custom data in its userInfo dictionary, it can access that data and process it accordingly. On the other hand, if the local notification only badges the application icon, and the user in response launches the application, the application:didFinishLaunchingWithOptions: method is invoked, but no UILocalNotification object is included in the options dictionary.
If the application is foremost and visible when the system delivers the notification, no alert is shown, no icon is badged, and no sound is played. However, the application:didReceiveLocalNotification: is called if the application delegate implements it. The UILocalNotification instance is passed into this method, and the delegate can check its properties or access any custom data from the userInfo dictionary.
When you app is neither running nor in the background, your notification is received in application:didFinishLaunchingWithOptions: method in your app delegate.
You can use the below code to access the notification object.
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
UILocalNotification *localNotif = [launchOptions objectForKey:UIApplicationLaunchOptionsLocalNotificationKey];
if (localNotif) {
// Show Alert Here
}
}

iOS Handle notifications after starting app from not-running state

I've been trying to handle receiving notifications in my app, but its not really working out.
When I use didReceiveLocalNotification:(UILocalNotification *)notification. I can receive and use the notification that is used to enter the app, without any problems
However, this function is only fired when the app is already running (active, inactive, background, and possibly suspended, but I haven't tried that yet).
Now, there is this function didFinishLaunchingWithOptions:(NSDictionary *)launchOptions where you can use [launchOptions objectForKey:UIApplicationLaunchOptionsLocalNotificationKey] which would return a UILocalNotification.
However, when you launch the app from not-running state, this event is not fired. The LocalNotification then opens the app, but I can not use it in any way.
Now, my question is: How can I make it work, so I can receive and process notifications when starting the app, from a notification, when the app is in not-running state? Is there perhaps something I'm doing wrong here?
Here is a bit of sample code from my app:
First, the didFinishLaunchingWithOptions function, which, unfortunatly does not work. The function [sharedLocalNotificationsInstance processNotification:notification] is never launched...
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions{
LocalNotificationsController *sharedLocalNotificationsInstance = [LocalNotificationsController sharedLocalNotificationsInstance];
[sharedLocalNotificationsInstance checkNotifications];
UILocalNotification *notification = [launchOptions objectForKey:UIApplicationLaunchOptionsLocalNotificationKey];
if ( notification != nil ) {
// Process the received notification
[sharedLocalNotificationsInstance processNotification:notification];
application.applicationIconBadgeNumber = 0;
}
return YES;
}
And a second piece of code: The didReceiveLocalNotification function, which works perfectly: I receive the notification, and [sharedLocalNotificationsInstance processNotification:notification] works perfectly.
- (void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification{
// Used when the application launches from a notification
LocalNotificationsController *sharedLocalNotificationsInstance = [LocalNotificationsController sharedLocalNotificationsInstance];
// Process the received notification
[sharedLocalNotificationsInstance processNotification:notification];
}
This is how iOS handles local notification. It depends on which state of your app, e.g. active, running in background, or not started yet. The iOS will invoke either didFinishLaunchingWithOptions or didReceiveLocalNotification, or won't touch your app at all.
Please see this article for clarification - http://www.thekspace.com/home/component/content/article/62-uilocalnotification-demystified.html
<Matrix-Morpheus-Meme title="WHAT IF I TOLD YOU">
When an application is launched from the "not-running" state because a user tapped on a local notification alert, the application has been started by iOS, not by Xcode, thus IT IS NOT RUNNING UNDER THE DEBUGGER. You cannot breakpoint inside it and neither does NSLog() send anything to the Xcode console. Test with a UIAlertController.
</Matrix-Morpheus-Meme>

customize an action of iphone home button to submit score in gamecenter

i have a button in my app a button that submit score to gamecenter and works.
this is the code:
-(void)subScore{
GKScore *scoreRepoter = [[[GKScore alloc] initWithCategory:#"123456"] autorelease];
scoreRepoter.value=100;
[scoreRepoter reportScoreWithCompletionHandler:^(NSError *error) {
if (error!=nil) {
NSLog(#"errr submitting");
}else
NSLog(#"ok!");
}];
now i'd like to submit score before app is closed with home button.
i thought to customize an action of home button (if it is possible)
or perhaps i make the same line of code in viewDidUload...or something like that...
will i be sure that that action will be performed before unloading the app?
i should make that code in dealloc method?
thanks
You can't customize behaviour of Home button directly, but iOS provides some methods in your application's delegate, by which you can control lifecycle of the application.
Method called right before the application goes to background is applicationWillResignActive: in your application's delegate (usually this method is located in AppDelegate.m file).
I think you can get needed effect by calling your method like that:
- (void)applicationWillResignActive:(UIApplication *)application {
[mygame subScore];
}
Also please note that iOS has time limit of execution for this method: you must do all saving-the-game work in less that five seconds or your application will be killed.

Redirection of alert box button

I would like to know if we can actually redirect the alert box to a specific view. Meaning that when they clicked on "View" which is on the notification alert, it will redirect them to a particular view, just like the text message notification pop up. Is there any idea on how this works?
From your question, you could mean two types of alert dialogs:
The generic "alert box" you mention, or UIAlertView
A UILocalNotification alert dialog, shown when the application is in the background ("just like the text message notification pop up")
I will address them in order.
First, how to handle a UIAlertView "View" button click.
Implement the alertView:didDismissWithButtonIndex: method of the UIAlertViewDelegate protocol in your controller class, and when you init the UIAlertView set its delegate to self. Then when the user clicks a button marked e.g. "View", do this:
- (void)alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex
{
if([[alertView buttonTitleAtIndex:buttonIndex] isEqualToString:#"View"])
{
// take the user to a specific view
} else { // handle other cases if you have any
}
}
Secondly, how to handle a UILocalNotification which triggers an application launch.
Apple docs on UILocalNotification state:
If the notification is an alert and the user taps the action button (or, if the device is locked, drags open the action slider), the application is launched. In the application:didFinishLaunchingWithOptions: method the application delegate can obtain the UILocalNotification object from the passed-in options dictionary by using the UIApplicationLaunchOptionsLocalNotificationKey key. The delegate can inspect the properties of the notification and, if the notification includes custom data in its userInfo dictionary, it can access that data and process it accordingly.
On the other hand, if the local notification only badges the application icon, and the user in response launches the application, the application:didFinishLaunchingWithOptions: method is invoked, but no UILocalNotification object is included in the options dictionary.
You need to write code for handling this launch case in your app delegate class, in the application:didFinishLaunchingWithOptions: method.
IF you happen to get a UILocalNotification while the app is running, Apple docs state:
If the application is foremost and visible when the system delivers the notification, no alert is shown, no icon is badged, and no sound is played. However, the application:didReceiveLocalNotification: is called if the application delegate implements it. The UILocalNotification instance is passed into this method, and the delegate can check its properties or access any custom data from the userInfo dictionary.
EDIT: To take the user to a specific view straight away, you can manually push something onto a UINavigationController stack (if your app usually operates with navigation controllers, it makes sense to do this), or present a modal view controller. I've linked there to guides for both.