what method within a ViewController’s class can I call to check when it has been brought to the foreground?
For example Im looking at a page on my application and I decide to close the application and go back to it later. When I go back to it the same view as I was looking at was on the screen. However… As soon as I open the application I want to segue over to another view.
How can I do this?
Currently trying this:
- (void) applicationDidBecomeActive:(NSNotification*) notification
{
[self checkActivity];
// Do your stuff here
}
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(applicationWillEnterForeground:)
name:UIApplicationWillEnterForegroundNotification
object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(applicationDidBecomeActive:)
name:UIApplicationDidBecomeActiveNotification
object:nil];
}
return self;
}
- (void)checkActivity{
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
NSLog(@"Checking if re-authentication required...");
if([[defaults objectForKey:@"shouldgotologin"] isEqualToString:@"yes"]){
NSLog(@"View Should go to login...performing segue");
[defaults setObject:@"no" forKey:@"shouldgotologin"];
[defaults synchronize];
[self performSegueWithIdentifier:@"backtologin" sender:self];
} else {
NSLog(@"Should go to login is not true.");
}
}
Register your view controller to observe
UIApplicationWillEnterForegroundNotification:1) Inside view controller’s init method:
2) Inside view controller’s dealloc method:
3) Also, have your view controller implement this method:
If the timing of
UIApplicationWillEnterForegroundNotificationdoesn’t suit you, check all the available notifications forUIApplicationhere:http://developer.apple.com/library/ios/#DOCUMENTATION/UIKit/Reference/UIApplication_Class/Reference/Reference.html