How can I detect when a user exits the application? (hitting the home button)
And how can I detect when the relaunch it? (clicking the icon)
*I'm not talking about users manually quitting the app by holding the home button and then making the icons wiggle and deleting the app instance from the sub-dock. i'm talking about just temporarily exiting the app buy clicking the home button.. maybe sending a text or whatnot then coming back to the app.
Thanks!
- (void)applicationDidEnterBackground:(UIApplication *)application
and
- (void)applicationDidBecomeActive:(UIApplication *)application
In your AppDelegate.m
- (void)applicationWillTerminate:(UIApplication *)application
There's a notification UIApplicationDidGoToBackground that fires when the home button is pressed. A similar notification tells you about going back to foreground.
Related
When my app is running in foreground, If user clicks on home button i want to show some alert like
Do u want to exit the app with Yes/No buttons. Is there any delegate methods called when home button is clicked…
Thank you….
There is no way to do this on iOS. I think you want a similar behavior to Android back button.
Think of iOS home button as Android home button, which doesn't let you prompt user either.
You can not stop the app to go to background, even if you show the alert message, it will go to the background anyways when you hit the home button and you won't see the alert message either.
There is no delegate method that would trigger when the app is going to the background, but there is a delegate method that will trigger when your app has totally gone to background.
Just to show you visually-
In the AppDelegate implementation file(.m), I added an alert message at the end of applicationDidEnterBackground method and put a break point just to check if it is entering that block as I expected. See the result--
You can use these methods in AppDelegate.m
- (void)applicationDidEnterBackground:(UIApplication *)application {
// Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
// If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
}
- (void)applicationWillEnterForeground:(UIApplication *)application {
// Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background.
}
Sliding from the lock screen and/or pressing a notification from the notifications page... do both pass from applicationDidFinishLunchingWithOptions when the app is totally closed?
My concern is because, when the app is closed and not in background, whenever I press on the app from the notification, my app opens and goes where it has to go... but whenever I press slide to open, the app opens but it does not go to the page it should go.
The docs say that if the action button on the notification is pressed, it calls application:didFinishLaunchingWithOptions and passes in the notification payload. Later it says if the app is running in the foreground, it delivers the notification via application:didReceiveRemoteNotification:. This implies to me that when the app is backgrounded or not running, then application:didFinishLaunchingWithOptions is called. Otherwise, application:didReceiveRemoteNotification: is called.
application:didFinishLaunchingWithOptions: will only be called if your app is not launched already. While it is true that the options will include info on notifications if that is what ended up launching the app, what you want is to handle your local notification logic here:
- (void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification
iPhone 6 and 6 Plus comes with a Reachability feature to pull down the screen. Is there an API to notify an app when the user double-taps the home button?
when user double tap home button only below method will be called from AppDelegate.
1) Double tap home
-(void)applicationWillResignActive:(UIApplication *)application
when tap on app below method will be called
- (void)applicationDidBecomeActive:(UIApplication *)application
2) single tap below method will be called
1)- (void)applicationWillResignActive:(UIApplication *)application
2)- (void)applicationDidEnterBackground:(UIApplication *)application
Maybe this will help you.
I want to check every time the app launches whether or not there's a URL in the clipboard, and if so, do something with it. Which method fires that I can override whenever the app launches, whether from a cold launch (it was killed in the background for instance) or if I just press the home button, copy a URL and jump back in.
Is it one of these?
- (void)applicationDidBecomeActive:(UIApplication *)application
- (void)applicationWillEnterForeground:(UIApplication *)application
- (void)applicationDidBecomeActive
- (void)applicationDidFinishLaunching:(UIApplication *)application
Confused.
As #rmaddy says, the correct method to use once the app launched is applicationWillEnterForeground: from your app delegate.
This method will be called when the user jump backs in, but NOT in other circumstances you don't need to respond to (such as the user receiving a text message and dismissing it).
However, from my testing, applicationWillEnterForeground: is not called when an app is launched from cold; you should catch that in applicationDidFinishLaunchingWithOptions:.
So, basically, your app delegate should include code like this:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
[self checkForURL];
...
}
- (void)applicationWillEnterForeground:(UIApplication *)application {
[self checkForURL];
...
}
- (void)checkForURL{
//code for checking for URL goes here
}
Hope that helps.
- (void)applicationDidBecomeActive is called when the app is launched or becomes active from the background.
This doc explains everything pretty well: http://developer.apple.com/library/ios/#documentation/iphone/conceptual/iphoneosprogrammingguide/ManagingYourApplicationsFlow/ManagingYourApplicationsFlow.html
With reference to the UIApplicationDelegate Protocol, the handling of app launches can be handled in 2 methods:
application:willFinishLaunchingWithOptions:
application:didFinishLaunchingWithOptions:
And the handling of app launches from background can be handled with the help of method:
applicationDidBecomeActive:
Based on the above call, you can handle your application.
In your app delegate add it to the methods that the other answers have suggested (applicationDidFinishLaunchingWithOptions:). In your root view controller register for the following notification. This will always be called when your application launches once it has already started running.
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(bringingItBack) name:UIApplicationWillEnterForegroundNotification object:nil];
This will cover both the instances when the app launches and when you are just bringing it back from the background.
When I click the home button on the device. This is a part of my delegate:
- (void)applicationWillResignActive:(UIApplication *)application
{
NSLog(#"resign active");
//[[NSThread mainThread] cancel];
}
- (void)applicationDidEnterBackground:(UIApplication *)application
{
NSLog(#"enter background");
//[[NSThread mainThread] cancel];
}
- (void)applicationWillEnterForeground:(UIApplication *)application
{
NSLog(#"enter foreground");
//[[NSThread mainThread] cancel];
}
- (void)applicationWillTerminate:(UIApplication *)application
{
NSLog(#"terminate");
//[[NSThread mainThread] cancel];
}
I uses NSLog to understand which method are called when I click the Home Button. This is output in console.
2012-01-20 15:55:55.853 MyApp[5955:11f03] enter background
2012-01-20 15:55:55.855 MyApp[5955:11f03] terminate
Program ended with exit code: 0
So, when I click on app in background (clicking two time the home button), it launchs again showing the first image and then my first uiviewcontroller.
In which way I can resolve it, and resume app from uiviewcontroller that was on the top when user clicks home button?
Check your info.plist file and make sure that "Application does not run in background" is not checked.
The image below shows the option in state "on", so that when the user press the home button, the app is effectively finalized.
This is how Apple describes that option:
UIApplicationExitsOnSuspend
UIApplicationExitsOnSuspend (Boolean - iOS) specifies that the application should be terminated rather than moved to the background when it is quit. Applications linked against iOS SDK 4.0 or later can include this key and set its value to YES to prevent being automatically opted-in to background execution and application suspension. When the value of this key is YES, the application is terminated and purged from memory instead of moved to the background. If this key is not present, or is set to NO, the application moves to the background as usual.
This key is supported in iOS 4.0 and later.