I have a Tab bar app with 3 view controllers and I use "viewWillAppear" to detect which view is opend.
When I close the app, it still works "in the background" and when I open the app again "viewWillAppear" is not detecting this opening.
Is there any other option to detect this opening? Thanks
You can observe the UIApplicationWillEnterForegroundNotification or implement applicationWillEnterForeground: in your app delegate.
Firstly, You should see the necessary delegation method in UIApplicationDelegate
When you close application that currently open, It will call this method:
- (void)applicationDidEnterBackground:(UIApplication *)application
After the application has been closed but still in dock, you open them again. In the transition state before entering the application, It will call this method:
- (void)applicationWillEnterForeground:(UIApplication *)application
When the application completely presented on previous state before you closed them. It finally call thid method:
- (void)applicationDidBecomeActive:(UIApplication *)application
If you would like to do something in viewWillAppear you should implement in applicationDidBecomeActive to send some message to your current view or other to do what do you want to do after application became actived.
When your app is resumed from the background, it will receive the applicationWillEnterForground: method. It'll get the applicationDidEnterBackground: when it is suspended too.
- (void)applicationWillEnterForeground:(UIApplication *)application {
NSLog(#"app will enter foreground");
[viewController refresh:NULL];
}
i think this will work.
write this in your app delegate
Related
Is there a way to run an action when the iPhone gets locked?
I simply want to run a little bit of code when the iPhone gets locked by the user. I'm looking for a solution that works without a jailbreaked iPhone.
https://stackoverflow.com/a/7888507/267892
May be you need to implement following methods in AppDelegate:
Tells the delegate that the application is now in the background.
- (void)applicationDidEnterBackground:(UIApplication *)application
Tells the delegate that the application has become active.
- (void)applicationDidBecomeActive:(UIApplication *)application
Tells the delegate that the application is about to become inactive.
- (void)applicationWillResignActive:(UIApplication *)application
See UIApplicationDelegate Protocol Reference for more info on these.
Take a look at UIApplicationDelegate Protocol Reference
especially applicationProtectedDataDidBecomeAvailable:
and applicationProtectedDataWillBecomeUnavailable:
I want to handle APNs differently whether my application come from background or is already in the foreground. Do you know what methods could help me ?
These are the messages that handle the notification about a changing application state.
– application:didFinishLaunchingWithOptions:
– applicationDidBecomeActive:
– applicationWillResignActive:
– applicationDidEnterBackground:
– applicationWillEnterForeground:
– applicationWillTerminate:
– applicationDidFinishLaunching:
You must implement the one's you need in the designated Delegate! Take a look at the "Task" section of the following link for further assistance, especially the "Monitoring application state changes" part ;-)
Apple's UIApplicationDelegate Protocol Reference
- (void)applicationDidBecomeActive:(UIApplication *)application
It is a method which is executed when app comes to foreground from background. So you can write your code here under the above method about whatever you want to do when app comes to foreground from being in background.
(void)applicationWillResignActive:(UIApplication *)application;
(void)applicationDidEnterBackground:(UIApplication *)application;
(void)applicationWillEnterForeground:(UIApplication *)application;
(void)applicationDidBecomeActive:(UIApplication *)application ;
Please have a look at these methods written in delegate.m file
You must implement the application:didReceiveLocalNotification: method. This will be called for both cases. You can differentiate between the cases by checking the applicationState property of [UIApplication sharedApplication]: if it's UIApplicationStateInactive then the app was in background, and if it's UIApplicationStateActive then the app was in foreground.
When the iPhone application is running in background and it receive a remote notification. So it will execute the didReceiveRemoteNotification call back. In that I am going to push to a new UIViewController. But before that its noticed that its calling the applicationWillEnterForeground callback.
In that I am also doing some location update using a modal dialog. So when this notification arrives this both scenarios happens and the app is getting crashed. So is there any way to block the applictiaonWillEnterBackground processing on remote notification. As the moment is little bit hard cos this processing is done after applicationWillEnterBackground controller.
Thank you.
The callback application:didReceiveRemoteNotification: should only be invoked when the application is running in the foreground. When running in the background you should instead get a call to application:didFinishLaunchingWithOptions:.
Since you are asking the question and also using core location it might be that application:didReceiveRemoteNotification: is called when the application is in the background but I would think that would be a bug. At least according to Apple's documentation.
Anyway, NO, you can't block applicationWillEnterForeground:. Without knowing exactly what you are doing in the different callbacks I would recommend that you set a flag in applicationWillEnterForeground: if you are doing something there and then check that flag in application:didReceiveRemoteNotification:
- (void)applicationWillEnterForeground:(UIApplication *)application {
if (somehingHappend) {
somethingHappended = YES;
}
}
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo {
if (!somethingHappened) {
// push your view controllers or whatever
}
}
Where somethingHappened is a BOOL defined in the same class as an ivar.
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
I want to implement this function on my apps but i cant seem to figure out how to use this line of codes.
- (void)applicationWillResignActive:(UIApplication *)application {
//our app is going to loose focus since there is an incoming call
[self pauseGame];
}
- (void)applicationDidBecomeActive:(UIApplication *)application{
//the user declined the call and is returning to our app
[self resumeGame];
}
I've read that this must be placed in appdelegates but i cant seem to figure out how could i call my pause action when the game is currently in the viewcontroller.
Thank you.
Instead of sending the messages to self (which is the app delegate), you would send them to your view controller.
For example, if your app delegate had a property for your main game view controller named "gameViewController" (where the methods to pause and resume were implemented):
- (void)applicationWillResignActive:(UIApplication *)application {
// our app is going to loose focus since there is an incoming call
[self.gameViewController pauseGame];
}
- (void)applicationDidBecomeActive:(UIApplication *)application {
// the user declined the call and is returning to our app
[self.gameViewController resumeGame];
}
I know that this was answered a long time ago. But I wanted to add that another (more scalable) solution is to have interested parties (e.g., UIViewControllers) register an interest in UIApplicationDidEnterBackgroundNotification and UIApplicationWillEnterForegroundNotification.
This approach has the advantage that the Application Delegate doesn't need to have direct knowledge of the objects that might need to respond to entering background/foreground.