Handling Applicationdidbecomeactive - "How Can a View Controller Respond to the App Becoming Active"

Handling applicationDidBecomeActive - How can a view controller respond to the app becoming Active?

Any class in your application can become an "observer" for different notifications in the application. When you create (or load) your view controller, you'll want to register it as an observer for the UIApplicationDidBecomeActiveNotification and specify which method that you want to call when that notification gets sent to your application.

[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(someMethod:)
name:UIApplicationDidBecomeActiveNotification object:nil];

Don't forget to clean up after yourself! Remember to remove yourself as the observer when your view is going away:

[[NSNotificationCenter defaultCenter] removeObserver:self 
name:UIApplicationDidBecomeActiveNotification
object:nil];

More information about the Notification Center.

How to tell the active view controller when applicationDidBecomeActive is called?

I would recommend using notifications.

In your app delegate's applicationdidBecomeActive method put in this code:

[[NSNotificationCenter defaultCenter] postNotificationName:@"appDidBecomeActive" object:nil];

In your current active view controller's init method subscribe to the notification.

[[NSNotificationCenter defaultCenter] addObserver:self 
selector:@selector(updateStuff)
name:@"appDidBecomeActive"
object:nil];

Implement the "updateStuff" method in your controller and you should be able to do whatever you want when the app becomes active.

Switching the view after applicationDidBecomeActive

Here's how you can do it. I just made a test app and I confirm that it works beautifully. Code:

#import "AppDelegate.h"
#import "ViewController.h"
#import "theView.h"

NSTimer *theTimer;
UIViewController *theViewController;
BOOL theTimerFired = NO;

@implementation AppDelegate

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
self.viewController = [[ViewController alloc] initWithNibName:@"ViewController" bundle:nil];
self.window.rootViewController = self.viewController;
[self.window makeKeyAndVisible];
return YES;
}

- (void)applicationWillResignActive:(UIApplication *)application
{
// Set a 5 minute timer (300 seconds)
theTimer = [NSTimer scheduledTimerWithTimeInterval:300.0 target:self selector:@selector(presentVC) userInfo:nil repeats:NO];
}

- (void)presentVC
{
// Set a boolean to indicate the timer did fire
theTimerFired = YES;
}

- (void)applicationDidBecomeActive:(UIApplication *)application
{
// Check to see if the timer did fire using the previous boolean we created
if (theTimerFired == YES)
{
theViewController = [[UIViewController alloc]initWithNibName:@"theView" bundle:nil];

[self.viewController presentViewController:theViewController animated:YES completion:NULL];

[theTimer invalidate];
}
}

@end

How can I use applicationDidBecomeActive in UIViewController?

At the time of reactivation, if you want to carry a particular thing for a view controller, you should register a notification in its viewDidLoad method.

UIApplicationDidBecomeActiveNotification will automatically notify your application and given controller, if they registered for it.

 [[NSNotificationCenter defaultCenter]addObserver:self
selector:@selector(yourMethod)
name:UIApplicationDidBecomeActiveNotification
object:nil];

Application delegate event(ex.: applicationWillTerminate) to my custom class

As my knowledge, you should add observer to listen app event look like this

NotificationCenter.default.addObserver(self, selector: #selector(self.appBecomeActive), name: NSNotification.Name.UIApplicationDidBecomeActive, object: nil)

And remember remove observer when your class deinit

How to call viewController's method in appdelegate

This may help you (tested in Swift 4)

class ViewController: UIViewController {

override func viewDidLoad() {
super.viewDidLoad()
NotificationCenter.default.addObserver(self,
selector: #selector(applicationDidBecomeActive),
name: Notification.Name.UIApplicationDidBecomeActive,
object: nil)
}

}

@objc func applicationDidBecomeActive() {
print("UIApplicationDidBecomeActive")
}

Note: Don't forget to remove observer when your view controller is no longer in use/memory (as this is an application level observer and will be called every time your application becomes active, whether your view controller is active or not.

Here is code to remove observer:

NotificationCenter.default.removeObserver(self,
name: Notification.Name.UIApplicationDidBecomeActive,
object: nil)

How to know if ViewController is visible again

Use your print statement in the AppDelegate for application: WillEnterForeground OR application: didBecomeActive both of those are called at different times when the user returns to the application. Use one or the other depending on at what point you'd like to call or reference your code



Related Topics



Leave a reply



Submit