When the user leaves my iOS app, I want to schedule a local notification to remind him about my app. What is a correct place to do it? I cannot choose between
- (void)applicationWillResignActive:(UIApplication *)application
and
- (void)applicationDidEnterBackground:(UIApplication *)application
Or there's no difference in the case?
I also have a second question. When my app launches (either if user pressed notification, or from launchpad), I should obviously remove all that reminding notifications. I guess I should do it in
- (void)applicationDidBecomeActive:(UIApplication *)application
or
- (void)applicationWillEnterForeground:(UIApplication *)application
or maybe another method? Or it makes no difference again?
Do it all in applicationWillEnterForeground. Remove any old notifications that don't matter now the user it engaged with the app and install any new notifications for after this use session. If the notification fires when the user is still using the app then you don't need to display anything (and the system won't display anything either).
Related
In birthday app which is the best place to check birthdates and set its UILocalNotification in iOS.
Iss didfinishlaunching the best place? or any other.
That is a good place to schedule the notification if you need to check it only once.
If data changes often I suggest you put it in applicationDidBecomeActive: since iOS might not remove the app from memory each time.
You also need to make sure that you schedule only one notification, and not each time the application is started.
- (void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification {
NSLog(#"Notification fired"!);
}
if your in foregroud it will call this method. then you can use this method also.
I am developing an app where I need to alert the user using UILocalNotification for a specific time interval
The problem is that I place the code in applicationDidEnterBackground..
So evrytime the user close the app. a New notification is created, resulting in multiple notifications created. How do I go about this problem? Thank you
- (void)applicationDidEnterBackground:(UIApplication *)application
{
}
When the user force kills the iOS application I need to reset some data in my app. But according to Apple's documentation post ios SDK 4.0 applicationWillTerminate callback method does not get called and the application is killed without being notified. So I would appreciate if anyone helps me find some solution/ other way to capture the event.
You should persist your state by, in your appDelegate, implementing
- (void)applicationWillResignActive:(UIApplication *)application
This will be called when the the app is about to enter background.
Read more in the Apple documentation here
Edit
You can't know if the app is terminated by the user or the system while running in background. If you need to clean up stuff, do it when the app is launched in:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
When I receive a local notification, everything works fine when I click on it. However if for example the user does not click on it, I want to trigger the same events compared to if they did. How would I achieve this. Im thinking there might be an if statement within the
(void)applicationDidBecomeActive:(UIApplication *)application
method to test if there is a notification active that has not been clicked on?
Do everything with localnotification in application:didReceiveLocalNotification:. And then put other stuff in (void)applicationDidBecomeActive:(UIApplication *)application
I would like to record a timestamp whenever the user opens the app. Now, I was able to record in the ViewDidLoad method, however, once the user closes the app and it goes to the background and then open the app once more, I can't get the timestamp, because ViewDidLoad doesn't run anymore. Does any of you have any idea where to put my timestamp code?
You could put it inside - (void)applicationDidBecomeActive:(UIApplication *)application , but it isn't a good approach, since the user starting the app isn't the only activity that will cause the timestamp to be saved, as there are other situations when this method might get called.
From Apple's Documentation
applicationDidBecomeActive: Tells the delegate that the application
has become active.
This method is called to let your application know that it
moved from the inactive to active state. This can occur because your
application was launched by the user or the system. Applications can
also return to the active state if the user chooses to ignore an
interruption (such as an incoming phone call or SMS message) that sent
the application temporarily to the inactive state.
The correct approach is to put the code that stores the timestamp both inside application:didFinishLaunchingWithOptions: and applicationWillEnterForeground:
The first method will get called on the first launch and the second one when the application is launched from the background. Since an ignored interruption wouldn't send the app to the background, applicationWillEnterForeground would not get called in this case, so you wouldn't have a timestamp stored after the user ignores a phone call for example.
Check this answer, since it very well summarizes the role of the methods involved in handling sending an app to the background and getting it back to the foreground and what should you use each of them for.
Implement applicationDidBecomeActive: in your app delegate or observe the UIApplicationDidBecomeActiveNotification notification.
Try using - (void)viewDidAppear:(BOOL)animated:
instead. It is called whenever the view appears on the screen.
Or you can look into the UIApplicationDelegate and try implementing something like:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
or
- (void)applicationDidBecomeActive:(UIApplication *)application