Use of background/foreground methods in AppDelegate - iphone

I'm planning to implement multi-task in my app.
I can see many methods here to do that in the AppDelegate like applicationWillResignActive, applicationDidEnterBackground, applicationWillEnterForeground, ...
But.... I don't see the way they should be used, nor why they are not in the ViewControllers... Nor what they are here for.
I mean : when the app enter in background, i don't know on which view my user is.
And back, when the app comes into foreground, how would I know what to do and what I may call, to update the view for example ?
I would have understood if those methods where in each view controller, but here, I don't see what they can be used for in a concrete way...
Can you help me to understand the way to implement things into those methods ?

Each object receive a UIApplicationDidEnterBackgroundNotification notification when the app goes in background. So to run some code when the app goes in background, you just have to listen to that notification where you want :
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(appHasGoneInBackground:)
name:UIApplicationDidEnterBackgroundNotification
object:nil];
Don't forget to release the listener when you don't need to listen to it anymore :
[[NSNotificationCenter defaultCenter] removeObserver:self];
And best of the best, you can play the same way with the following notifications :
UIApplicationDidEnterBackgroundNotification
UIApplicationWillEnterForegroundNotification
UIApplicationWillResignActiveNotification
UIApplicationDidBecomeActiveNotification

They are not in any of your view controllers because iOS adopts a 'delegate' design pattern, where you can be assured that a method will fire upon a class (in this case, the App Delegate for your application) when required.
As a learning process, why don't you just put NSLog's in those methods to see when they're fired?
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
// Override point for customization after application launch.
NSLog(#"didFinishLaunchingWithOptions");
[self.window makeKeyAndVisible];
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.
*/
NSLog(#"applicationWillResignActive");
}
- (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.
*/
NSLog(#"applicationDidEnterBackground");
}
- (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.
*/
NSLog(#"applicationWillEnterForeground");
}
- (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.
*/
NSLog(#"applicationDidBecomeActive");
}
- (void)applicationWillTerminate:(UIApplication *)application
{
/*
Called when the application is about to terminate.
See also applicationDidEnterBackground:.
*/
NSLog(#"applicationWillTerminate");
}

Related

How to properly minimize an iOS sprite kit application?

I've tried this iOS Sprite Kit tutorial and have created a similar app. However, I notice that when I press the home button to go to iOS home screen, I get a bad access exception in xCode. When I go back into the app, it starts from the beginning.
How can I properly close/minimize a Sprite Kit app to avoid that exception?
I tried this within the view controller presenting the scene, but it does not get called:
-(void)viewWillDisappear:(BOOL)animated
{
SKView * skView = (SKView *)self.view;
skView.paused = YES;
[super viewWillDisappear:animated];
}
I found out that there's Kobold Kit sprite engine built on top of Sprite Kit, after porting my project to that, i can minimize the app and restore it with the same stuff on the screen!
I believe that the proper way to handle minimizing an app is in the AppDelegate via these methods :
- (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 inactive 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.
}

iPhone viewwillappear on application entering foreground

I have an application where there are 3 tabs to calculate distance and all. When I first launch the app, on clicking 3 rd tab some network call is happening. Now I put the application to background.
When the application comes to foreground, it should call viewwillappear to go for the network call again. but it is not happening. it is not calling viewwillappear.
How can I check when application comes to foreground, it should check for 3rd tab and call network method
Please help me
When application comes to foreground,
- (void)applicationWillEnterForeground:(UIApplication *)application;
of the app delegate is called.
You can restart all your paused tasks in:
- (void)applicationDidBecomeActive:(UIApplication *)application;
- (void)applicationDidEnterBackground:(UIApplication *)application
{
//save in NSUserDefaults (or wherever) which tab is currently active
}
- (void)applicationWillEnterForeground:(UIApplication *)application
{
// read from NSUserDefaults which tab was active before,
// and use an IF statement to control the further behavior
}
- (void)applicationDidEnterBackground:(UIApplication *)application {
if(tab3){
[viewController3 netWorkCallFromHere];
}
}
in this approach you will have to declare BOOL tab3 in Appdelegate.
set it true in third viewController and set it false in another viewController .
when it returns from the background then it will check the flag and it will work accordingly.
- (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
{
NSLog(#"%d",tabBar.selectedIndex);
if (tabBar.selectedIndex == 2) {
NSLog(#"Your work");
}
}

Differentiate between application launch and application awake

I need to be able to differentiate between application didFinishLaunching and application entering background and being loaded back into the foreground. I noticed that if I register for the UIApplicationDidBecomeActiveNotification, it is called in both instances. How do I distinguish between the two?
Use the didFinishLaunchingWithOptions vs applicationWillEnterForeground to distinguish if app is starting cold or returning from background.
I tried listening to the UINotification's in one of my apps, but the notification was being delivered too late, so I resorted to listening to these method calls directly.
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
// only call when app is freshly launched
}
- (void)applicationWillEnterForeground:(UIApplication *)application
{
// Only called when app is returning from background
}
I need to be able to differentiate between application
didFinishLaunching
UIApplicationDidFinishLaunchingNotification
application entering background
UIApplicationDidEnterBackgroundNotification
being loaded back into the foreground
UIApplicationWillEnterForegroundNotification

how to invoke a function while the application goes into suspend mode in iOS?

I m new to the iOS. When i press home button in iPhone, application goes into suspend mode is what i know, in the program, how can i capture this event, and clear my local datas?
Kindly advice.
You can use the following method from within your app delegate:
- (void)applicationWillResignActive:(UIApplication *)application
Apple explains when this delegate method is called:
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.
Inside your delegate, put the code you want to call inside of these. One fires every time you background the application and the other fires when you come back.
- (void)applicationDidEnterBackground:(UIApplication *)application
- (void)applicationDidBecomeActive:(UIApplication *)application
While you can implement the UIApplicationDelegate methods discussed by others, it is often more convenient (and arguably cleaner) to have objects that need to do clean up register themselves for the corresponding notifications:
UIApplicationDidEnterBackgroundNotification
UIApplicationWillResignActiveNotification
E.g.
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(myCleanupMethod:)
name:UIApplicationDidEnterBackgroundNotification
object:nil];
(If you go this route, don't forget to remove the observer when the observing object is deallocated.)
Use app delegate applicationWillResignActive method and UIBackgroundTaskIdentifier if needed. For example:
- (void)applicationWillResignActive:(UIApplication *)application {
UIBackgroundTaskIdentifier backgroundTaskIdentifier = [application beginBackgroundTaskWithExpirationHandler:^(void) {
[application endBackgroundTask:backgroundTaskIdentifier];
//your cleanup code
}];
}

- (void)applicationWillResignActive:(UIApplication *)application never gets called

I'm trying to handle phone calls and standby and things of that nature. I added the function
- (void)applicationWillResignActive:(UIApplication *)application
and
- (void)applicationDidBecomeActive:(UIApplication *)application
to my UIApplicationDelegate. When coming out of standby, "applicationDidBecomeActive" always gets called. However the issue is "applicationWillResignActive" never gets called.
I was wondering if anyone has run into this issue and whether or not anyone found a reason.
EDIT
More info, I discovered that my engine's update loop that gets run from applicationDidFinishLaunching was causing me to miss the message. I call
while(CFRunLoopRunInMode(kCFRunLoopDefaultMode, .002, FALSE) == kCFRunLoopRunHandledSource);
to catch all iphone messages but it doesn't seem to catch the resignActive message before becoming inactive.
Attempting to fork a thread for my update loop is causing weird crash bugs. Anyone have any quick fix suggestions?
Its getting called in iOS 4.0 , when the Home button is hit.
The following delegate methods are called when the Home button is hit in iOS 4.0
- (void)applicationWillResignActive:(UIApplication *)application
{
NSLog(#"Application Did Resign Active");
}
- (void)applicationDidEnterBackground:(UIApplication *)application
{
NSLog(#"Application Did Enter Background");
}
And when you double tap the home button and again relaunch the App , the following delegate methods are called .
- (void)applicationWillEnterForeground:(UIApplication *)application
{
NSLog(#"Application Will Enter Foreground");
}
- (void)applicationDidBecomeActive:(UIApplication *)application
{
NSLog(#"Application Did Become Active");
}
I don't think
- (void)applicationWillResignActive:(UIApplication *)application
is called when a phone call is received. I think the OS waits for the user to either answer or declines the phone call. If it i declined, then the app says alive and
- (void)applicationDidBecomeActive:(UIApplication *)application
is called. If it is answered, then your app is told to exit and it will receive
- (void)applicationWillTerminate:(UIApplication *)application
Be sure to allow
- (void)applicationDidFinishLaunching:(UIApplication *)application
to return before running your game loop. One technique is to use the function
- (void)performSelector:(SEL)aSelector withObject:(id)anArgument afterDelay:(NSTimeInterval)delay
on the application delegate and run your game loop after a delay of say ".01" After you do this, the message should be able to fire properly. I believe the reason for the message stomping was because the run loop was stuck on applicationDidFinishLaunching and wasn't able to push any other UIApplicationDelegate messages onto the queue.
- (void)applicationWillResignActive:(UIApplication *)application {
/*
Sent when the application is about to move from active state 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.
*/
}