[iOS]: detect when the view controller appears after returning from another external application

This is my escenario:

I have a view controller in which the user can switch to another application (Settings) when he clicks the button in this way:

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

So, this code opens the application screen settings, and the following legend is displayed in the upper left corner:

Back to myApplication

I want to determine when the view controller where the user clicks the button is active again. I know that you can detect when an application is activated again using this method in the delegate file

 - (void)applicationWillEnterForeground:(UIApplication *)application 

But I need to define a specific view controller. I tried with -(void)viewWillAppear:(BOOL)animated , but it does not work. Anyone got any about this?

+5
source share
1 answer

Configure 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 application can reactivate for many reasons, rather than just returning from the Settings app.

+7
source

Source: https://habr.com/ru/post/1238664/


All Articles