applicationDidEnterBackground not running - ios

I am putting this in my ViewController.m file and when my app enters the background the NSLog is never called.
Can anyone explain why?
- (void)applicationDidEnterBackground:(UIApplication *)application {
NSLog(#"Application entered background state.");
}

This is app's delegate method. Put it in the object implementing the UIApplicationDelegate protocol which is by default app's delegate class created for you when you start a new project.
Or use the notification center to get notified about an event of switching to the background. Just register your view controller as an observer of UIApplicationDidEnterBackgroundNotification. It's sent when entering the background.
Example
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(handleDidEnterBackgroundNotification:)
name:UIApplicationDidEnterBackgroundNotification
object:nil];

Related

How to use UIApplicationDidBecomeActiveNotification

How to use UIApplicationDidBecomeActiveNotification?
Should I declare it in viewDidLoad or viewWillAppear to reload data when coming from background to foreground.
Does UIApplicationDidBecomeActiveNotification gets called only when app comes from background to foreground?
Please help.
Thanks.
Sometimes it is useful to have a listener of UIApplicationDidBecomeActiveNotification when you need to make some action in your view controller on wake up from background (in case you entered to background with this view controller on-screen). In such wake up viewWillAppear will not be triggered!
Example of use:
- (void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(someMethod) name:UIApplicationDidBecomeActiveNotification object:nil];
}
- (void)viewWillDisappear:(BOOL)animated
{
[super viewWillDisappear:animated];
[[NSNotificationCenter defaultCenter] removeObserver:self name:UIApplicationDidBecomeActiveNotification object:nil];
}
- (void)someMethod
{
<YOUR CODE AT WAKE UP FROM BACKGROUND>
}
Of course, you can also implement all you need at your app delegate class life cycle.
You get this notification if your app was interrupted by a phone call or push notification. Generally, if your application is getting active on screen after interruption.
You can register any class, that is loaded in memory by the moment application will become active as observer to this notification.
Use following code in viewDidLoad:
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(someMethod:)
name:UIApplicationDidBecomeActiveNotification object:nil];
Use someMethod to handle this notification. And don't forget to remove this class as observer in dealloc:
[[NSNotificationCenter defaultCenter] removeObserver:self
name:UIApplicationDidBecomeActiveNotification
object:nil];
The OP asked about system notifications fired when the app is backgrounded and then foregrounded again. The Notification designed to handle this situation is the UIApplicationDidEnterBackgroundNotification and UIApplicationWillEnterForegroundNotification. If you want a notification that fires for a wider range of situations, such as when you have a system notification, take a phone call, a SMS comes in, or you slide up the control pane, as well as being backgrounded then you will want the UIApplicationWillResignActiveNotification and UIApplicationDidBecomeActiveNotification. It's important to recognize that these are different, since you might only need to react if your app is backgrounded, and not for other scenarios.

didBecomeActive posts a notification, my view controller never hears it

App delegate:
- (void)applicationDidBecomeActive:(UIApplication *)application {
[[NSNotificationCenter defaultCenter] postNotificationName:APP_REFRESH_NOTIFICATION object:nil];
}
In my view controller:
- (void)viewDidLoad {
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(doStuff) postNotificationName:APP_REFRESH_NOTIFICATION object:self];
}
- (void)doStuff never gets called. Why?
I assume that you've typed your question incorrectly and you'd meant to write addObserver:selector:name:object:, instead of addObserver:selector: postNotificationName:object: (such method doesn't exist).
In the documentation of - (void)addObserver:(id)notificationObserver selector:(SEL)notificationSelector name:(NSString *)notificationName object:(id)notificationSender
we can read:
notificationSender
The object whose notifications the observer wants
to receive; that is, only notifications sent by this sender are
delivered to the observer. If you pass nil, the notification center
doesn’t use a notification’s sender to decide whether to deliver it to
the observer.
So in your case, as you're passing object:nil in postNotificationName:object:, you also have to set object:nil in addObserver:selector:name:object:.
According to the documentation you also should replace the method doStuff with:
- (void)doStuff:(NSNotification *)notification
and use #selector(doStuff:) in addObserver:selector:name:object:.
You're passing self as the object parameter to addObserver:selector:name:object:, but doStuff doesn't accept any parameters, so the method call fails (silently). Your viewDidLoad should look like this:
- (void)viewDidLoad {
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(doStuff)
name:APP_REFRESH_NOTIFICATION
object:nil];
}
You're app delegate is posting a notification when the app becomes active, but your view controller isn't subscribing to that until its view gets loaded. If your app delegate is creating your view controller and loading it (which is probable) then your controller doesn't even exist at the time the notification is posted, which is why it isn't receiving it. If you use a storyboard, and that controller is the entry point in the storyboard, AND you use the info.plist for your app to set that storyboard as the main interface, then it will have already instantiated the controller and loaded its view by the time -applicationDidBecomeActive: is called, solving your problem.

Knowing the app. launc in Objective C

The app. delegate has the method that gets invoked when the app launches. Pain is my app is not following MVC and I have to reset many text fields to empty when the app. launches(not concerned about first launch). These txt fields are not created in app delegate so i cannot set them in app delegate (as the txt fields are not accessible). how can i know the app has launched in the new class to reset the fields . Is there any condition like
if(application.HasAppLaunced)
{
}
KIndly help
You could register your view controller as an observer of event
UIApplicationDidEnterBackgroundNotification
or
UIApplicationWillEnterForegroundNotification
in your view controller somewhere in viewDidLoad:
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(appEnteredBackground)
name:UIApplicationDidEnterBackgroundNotification
object:nil];
and add this method to reset your fields, you can reset all fields here:
- (void)appEnteredBackground{
[textField setText:#""];
}
don't forget to unregister your view controller, put this in some appropriate location:
[[NSNotificationCenter defaultCenter] removeObserver:self name:UIApplicationDidEnterBackgroundNotification object:nil];
I don't completely understand what are you asking for, but if the question is how to detect whather app is active you may use following condition:
[[UIAppication sharedApplication] applicationState] == UIAppicationStateActive
Try This
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
// Reset your text fields here
}
Try this one.. You can call a method to empty thetextfields using NSNotificationCenter
You can write the following line in AppDelegate.
[[NSNotificationCenter defaultCenter]postNotificationName:#"EmptyFields" object:nil];
You can write the following line in viewDidLoad method.
[[NSNotificationCenter defaultCenter]addObserver:self selector:#selector(setEmpty) name:#"EmptyFields" object:nil];
You can do textfields empty in the following method. You can write it in your ViewController.
- (void) setEmpty{
NSLog(#"Empty....");
}

UIApplicationDidBecomeActiveNotification filter notifications

I registered my main view controller for listening to UIApplicationDidBecomeActiveNotification because I want to display a UIAlertView each time the user enters my app :
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(someMethod:)
name:UIApplicationDidBecomeActiveNotification
object:nil];
It's working like a charm, my only problem is if my app gets interrupted (by an UIAletView, such as a calendar event, or a popup asking for picture access confirmation), the notification gets called once the alert view's dismissed.
Any idea on how to detect ONLY when my app comes back from background mode ?
why don't you use AppDelegate method,
- (void)applicationWillEnterForeground:(UIApplication *)application
{
//do whatever you want when app comes from background to foreground
}
I know this is an old thread, but there is a UIApplicationWillEnterForegroundNotification. Works like this:
[[NSNotificationCenter defaultCenter]addObserver:self
selector:#selector(myMethod)
name:UIApplicationWillEnterForegroundNotification
object:nil];
Best regards,
Gabriel Tomitsuka
Check state (active/background) of your application by following code:
UIApplicationState state = [[UIApplication sharedApplication] applicationState];
if (state == UIApplicationStateActive)
{
/// your stuff of code:
}
Above code might be useful in your case:

Which delegate function is called within a class on tapping the home-button

I have a pop-up which has to be hidden when the user moves away from the class.
On tapping on the home button, the doesn't happen.
- (void)applicationDidBecomeActive:(UIApplication *)application
- (void)applicationDidEnterBackground:(UIApplication *)application
Other than the above functions is there any other delegate functions which would be called in the same class (not the app-delegate class).
Only the UIApplicationDelegate defines those methods. If you want any other class to handle those events, you need to have the class register for the corresponding notification.
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(backgrounding) name:UIApplicationDidEnterBackgroundNotification object:nil];
And don't forget to remove the observer.
Then you need the method:
- (void)backgrounding {
// App entered background
}

Resources