Does didFinishLaunchingWithOptions happens after:
applicationWillResignActive
applicationDidEnterBackground
applicationWillEnterForeground
Or does it happen only after applicationWillTerminate?
And when applicationDidBecomeActive happens then? Thanks.
From the docs:
It is called after your application has been launched and its main nib
file has been loaded. At the time this method is called, your
application is in the inactive state. At some point after this method
returns, a subsequent delegate method is called to move your
application to the active (foreground) state or the background state.
It happens when the user opens your app. Followed by applicationDidBecomeActive when the app is ready to receive user events.
When the user presses the home button the following methods are called (by this order):
- applicationWillResignActive
- applicationDidEnterBackground
When the user opens your app again, and it is in background:
applicationWillEnterForeground
applicationDidBecomeActive
Finally, applicationWillTerminate is called instead of applicationDidEnterBackground on devices with iOS 3.x or earlier. Or with devices that do not support background apps (like the 3G).
application:didFinishLaunchingWithOptions:
only fires once: when your program starts up. You should typically create the main window/view controller here.
Related
Seems a simple question, but I'm not able to find an answer. I have some user information that I want to save locally using NSUserDefaults whenever the user presses the home button. I've read that I should put this in the appdelegate classes applicationDidEnterBackground method. But how do I make the actual data to be saved (in the view controllers) available to the applicationDidEnterBackground method?
Thanks for the help.
You have the right idea, but the code doesn't have to be in the app delegate. You can observe the notification that is sent when the application enters the background from anywhere and save your data when this notification is received.
I've read a number of questions here on SO regarding receiving push notifications while the application is not running (more than in the background, meaning it is shut down completely). This question in particular is most helpful in figuring out how to determine if one was receiving using the launchOptions dictionary.
However, I'm very confused, and I fully admit this may be a massive oversight on my part: when my device receives a push notification for this application while the app is shut down, and I later open my application, the launchOptions dictionary is a null pointer. From the description of the accepted answer in the previously mentioned link, and other places too, I gather that I should be able to see a notification payload; however there is nothing. I am developing for iOS 5.1.1.
My only other thought is to check the number of badges on start up (greater than zero, do something...), but this seems very unreliable.
Can anyone tell me what I am missing? Thank you in advance for your help!
application:didFinishLaunchingWithOptions: will only be called with payload information when app is launched due to notification. E.g. this could happen if user taps over notification alert (added in notification center) or notification received with content-avialble = 1 in payload (Newsstand notification) & provided your app is not in foreground as well in background.
If your app receives notification when app is in background. If it is Newsstand notification or if user taps over action button of alert below method is called
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo
with [[UIApplication sharedApplication] applicationState] not equal to UIApplicationStateActive.
In above case if user does not tap over action button of notification alert and launch app by tapping over it, neither didFinishLaunchingWithOptions or didReceiveRemoteNotification is called.
If your app receives notification while in foreground didReceiveRemoteNotification is called [[UIApplication sharedApplication] applicationState] will be equal to UIApplicationStateActive.
For badge in notification, if your app is not running no code is executed and the badge is incremented by 1 in app icon. When you launch app (tap on app icon) didFinishLaunchingWithOptions is called with normally. (If app is in background or foreground when notification received, same as above)
So I think this covers every possible case. Also note that the background case is valid for iOS SDK >= 4.0
I am writing an alarm clock app.
Please correct me if I am wrong:
On both events (lock & home button in iOS 4.x) the applicationWillResignActive: method is called. When locked my app can keep on running (forever if DeepSleepPreventer.h is used) to check if the alarm should go off. When home is pressed it has to stop working at some time (apart from some basic background calculations). So in this case I have to set a local UILocalNotification to trigger the alarm.
So my question: How to differentiate between those two events?
Thank you!
Have you tried implementing -applicationDidEnterBackground: in your app delegate?
I am running my app on a device. When I am in second view and terminate the app, the app quits but then when i launch it, it goes straight back to second view and not the first view (login view). I checked to see if the control goes to applicationWillTerminate method, but it does not go into that method. How can i make my program go into it?
This is probably due to iOS 4's multitasking. When you tap the Home button, your application is by default suspended on supported devices (if you double tap Home, you can see all suspended applications). You can set the plist key UIApplicationExitsOnSuspend to force UIKit to quit your application, rather than suspend, when the home button is pressed.
If you want to support the multitasking modes, look at the applicationDidEnterBackground: method of your UIApplicationDelegate implementation.
Maybe someone could tell me why when launch my app in the second time (after pressing the home button) the method viewDidLoad: is not being called?
On iOS 4 apps are no longer exited when pressing the home button. They are moved out of RAM, paused and then continued when you re-enter them, not launched again.
In iOS4 there is:
(void)applicationDidBecomeActive:(UIApplication *)application { }
Which is called when the application becomes active (out of background) you could call things from here that need to happen when you open the app.
Thanks
James
The second time you "launch" your application, in fact you are not launching but only re-activating (your application was in background). Your views are not reloaded.
As said in the previous answers :
You can detect it via (void)applicationDidBecomeActive:(UIApplication *)application and execute some code here (refresh HMI, refetch datas, etc...),
Or move your code to viewWillAppear