I want to design it so that if the user clicks home button then comes back to the application, the application will recognize that action and generate a message for the user to see (e.g., welcome back). I guess I should modify appdelegate for this, but I don't know how to go about coding it...can anyone lend a hand?
Whenever app wake up from background.. it come in this delegate in app delegate -
- (void)applicationWillEnterForeground:(UIApplication *)application
{
/*
Called as part of the transition from the background to the active state; here you can undo many of the changes made on entering the background.
*/
}
You can show a msg to user in this delegate
Also check these delegates -
- (void)applicationWillResignActive:(UIApplication *)application
{
/*
Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game.
*/
}
- (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 active state; here you can undo many of the changes made on entering the background.
*/
}
- (void)applicationDidBecomeActive:(UIApplication *)application
{
/*
Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
*/
}
- (void)applicationWillTerminate:(UIApplication *)application
{
/*
Called when the application is about to terminate.
Save data if appropriate.
See also applicationDidEnterBackground:.
*/
}
Related
I'm new to xcode and ios development. I'm learning using Brad Larson online courses. Now, he doesn't use storyboard to create interface. In his delegate file, he's using code for loading the rootViewController.
Now, when I create a new one page ios project, its seems like it's not the XYZAppDelegate that is responsible for loading the controller.
I know that in the mainstoryboard.storyboard file, my XYZViewController appears in there. I'm just wondering why it's not the XYZAppDelegate that is in charge of loading the viewController ?
In the appDelegate, there is nothing in the didFinishLaunchingWithOptions method but the program still loads?
However, the main.m file indicates that XYZAppDelegate is the delegateClassName.
Finally, my question is who's in charge in that case for loading the XYZViewController if it's not the AppDelegate ?
These are the default xcode generated files
main.m
return UIApplicationMain(argc, argv, nil, NSStringFromClass([XYZAppDelegate class]));
XYZAppDelegate
#import "XYZAppDelegate.h"
#implementation XYZAppDelegate
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
// Override point for customization after application launch.
return YES;
}
- (void)applicationWillResignActive:(UIApplication *)application
{
// Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
// Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game.
}
- (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 active state; here you can undo many of the changes made on entering the background.
}
- (void)applicationDidBecomeActive:(UIApplication *)application
{
// Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
}
- (void)applicationWillTerminate:(UIApplication *)application
{
// Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
}
#end
The answer lies in your Info.plist file. If you look, you'll see two keys called Main storyboard file base name and Main storyboard file base name (iPad) for which the values should be the name of your storyboard files for the respective devices. If these keys are present at launch, the OS will automatically load the storyboard file and insert the first view controller from the storyboard into the app's window. Docs here for the Info.plist key explanations (the raw key is called UIMainStoryboardFile).
How can I know whether app is terminated by user?
(Double-clicking Home button and press red minus '-' button)
I need sample or pseudo code...
Sometimes you do get the UIApplicationWillTerminateNotification (you can also implement the applicationWillTerminate: method in your app delegate), but it's not guaranteed that you do get any notification at all in this case. So don't rely on it.
You won't receive the notification if your app is "suspended", that is it is in background but not running. In the suspended case you simply get killed with the non-catchable SIGKILL. Same for when you get killed due to out-of-memory.
From the documentation for applicationWillTerminate: (emphasis mine):
For applications that do not support background execution or are linked against iOS 3.x or earlier, this method is always called when the user quits the application. For applications that support background execution, this method is generally not called when the user quits the application because the application simply moves to the background in that case. However, this method may be called in situations where the application is running in the background (not suspended) and the system needs to terminate it for some reason.
When user kills the app:
- (void)applicationWillTerminate:(UIApplication *)application{
}
Application will enter inactive state:
- (void)applicationWillResignActive:(UIApplication *)application
{
NSLog(#"Application Did Resign Active");
}
User pushes Home button:
- (void)applicationDidEnterBackground:(UIApplication *)application
{
NSLog(#"Application Did Enter Background");
}
Try this:
- (void)applicationWillTerminate:(UIApplication *)application
{
/*
Called when the application is about to terminate.
Save data if appropriate.
See also applicationDidEnterBackground:.
*/
}
You should implement the method above in your class that implements UIApplicationDelegate
Some more notes: that method won't be called if you are stopping your app via Xcode (stop debug button). So be patient when testing.
Also there is this:
- (void)applicationDidEnterBackground:(UIApplication *)application {
}
Could be helpful if you want to know did it enter background ;)
What is the sequence of methods that gets called when we tap on the home button on iPhone, i.e. when an application is going in background.
Basically, I want to make a server call before my app goes in background. I am writing my server call code in applicationWillResignActive:, but sometimes it executes and sometimes it doesn't.
I need a solution for this.
From the app delegate file itself:
- (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, called instead of applicationWillTerminate: when the user quits.
Superclass implementation saves changes in the application's managed object context before the application terminates.
*/
[super applicationDidEnterBackground:application];
}
Try using this method.....
Let say someone is using my app and the change the settings in the settings bundle, when they come back to my app I would like my view to update (via my updating method) according to those settings. I've tried many different things but I just can't get it to work.
What is the best way to implement this kind of behavior for my iPhone app?
in your AppDelegate put these methods:
- (void)applicationWillEnterForeground:(UIApplication *)application {
/*
Called as part of transition from the background to the active state: here you can undo many of the changes made on entering the background.
*/
}
- (void)applicationDidBecomeActive:(UIApplication *)application {
/*
Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
*/
}
Then you have hooks to do whatever you want.
It's worth noting that you're not limited to the AppDelegate; you can listen for these events from anywhere in your code with an NSNotification. See this answer for more details on how to listen for UIApplicationDidBecomeActiveNotification.
The applicationWillTerminate delegate method is not getting called in iOS 4.0
When I hit the Home button I am seeing the applicationWillResignActive and applicationDidEnterBackground delegate methods getting called.
- (void)applicationWillResignActive:(UIApplication *)application
{
NSLog(#"Application Did Resign Active");
}
- (void)applicationDidEnterBackground:(UIApplication *)application
{
NSLog(#"Application Did Enter Background");
}
And when I double Tap the Home button and again launch the Application the i find the applicationWillEnterForeground and applicationDidBecomeActive delegate methods are getting called.
- (void)applicationWillEnterForeground:(UIApplication *)application
{
NSLog(#"Application Will Enter Foreground");
}
- (void)applicationDidBecomeActive:(UIApplication *)application
{
NSLog(#"Application Did Become Active");
}
But I want to know when the applicationWillTerminate delegate method will be called , where I do some DB/file backup routines.
- (void)applicationWillTerminate:(UIApplication *)application{
}
I even tried to hit the minus sign and deleted the App running in the Background , but still it did not call any delegate method.
Any Ideas ???
From the iPhone Application Programming Guide:
Even if you develop your application using iPhone SDK 4 and later, you must still be prepared for your application to be terminated. If memory becomes constrained, the system might remove applications from memory in order to make more room. If your application is currently suspended, the system removes your application from memory without any notice. However, if your application is currently running in the background, the system does call the applicationWillTerminate: method of the application delegate. Your application cannot request additional background execution time from this method.
So yes, applicationWillTerminate: will generally not be called very often in iOS 4. If you have to save data, you should do so in both applicationWillTerminate: and applicationDidEnterBackground:.
The WWDC 2010 Session Adopting Multitasking on iPhone OS (Part 2) explains the application state transitions extremely well.
I got one solution for terminating apps when user hits the Home button in iOS4.
This will call the applicationWillTerminate delegate method instead of entering into background process.
Open your info.plist file
Add The Key UIApplicationExitsOnSuspend
Set the new key to YES
Actually you can also use this step to do so.
Open your info.plist file
Add The Key -> Application does not run in background
Set this key value to YES