Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

[iOS]: detect when view controller appears after back from another external app

This is my escenario:

I have a view controller where the user can go to another application (Settings) when push a button in this way:

    -(void) goToSettings{
       [[UIApplication sharedApplication] openURL:[NSURL URLWithString:UIApplicationOpenSettingsURLString]];
    }

So, this code open the app's screen settings and it shows in the upper left corner a legend like this:

Back to myApplication

I wish to detect when the view controller where user push the button is active again. I know you can detect when app is active again with this method in the delegate file

- (void)applicationWillEnterForeground:(UIApplication *)application

But I need detect in specific the view controller. I have tried with -(void)viewWillAppear:(BOOL)animated but It not works. Anyone have any idea about this?

like image 730
Javier Landa-Torres Avatar asked Dec 18 '15 18:12

Javier Landa-Torres


People also ask

How to detect when the back button is tapped swift?

The solution is simple: create a Boolean property called goingForwards in your view controller, and set it to true before pushing any view controller onto the navigation stack, then set it back to false when the view controller is shown again.

How to remove a presented view controller?

To dismiss a modally presented view controller, call the view controller's dismiss(animated:completion:) method.

How to know when app goes to background swift?

There are two ways to be notified when your app moves to the background: implement the applicationWillResignActive() method in your app delegate, or register for the UIApplication. willResignActiveNotification notification anywhere in your app.


1 Answers

Setup your view controller to listen for the UIApplicationDidBecomeActiveNotification notification.

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

Then add the becomeActive: method:

- (void)becomeActive:(NSNotification *)notification {
    // App is active again - do something useful
}

And be sure to remove the observer at the appropriate point.

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

Of course your app may become active again for lots of reasons, not just returning from the Settings app.

like image 194
rmaddy Avatar answered Oct 11 '22 04:10

rmaddy