Call a Function When Application will enter Foreground in iOS
NOTE: Use NSNotificationCenter and Notify the App will Become the Foreground and Call a function
// AppDelegate.m
- (void)applicationWillEnterForeground:(UIApplication *)application
{
NSLog(@"app will enter foreground");
}
- (void)applicationDidBecomeActive:(UIApplication *)application
{
NSLog(@"app did become active");
}
// ViewController.m- (void)viewDidLoad
{
[super viewDidLoad];
NSLog(@"view did load");
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(appDidBecomeActive:) name:UIApplicationDidBecomeActiveNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(appWillEnterForeground:) name:UIApplicationWillEnterForegroundNotification object:nil];
}
- (void)appDidBecomeActive:(NSNotification *)notification {
NSLog(@"did become active notification");
}
- (void)appWillEnterForeground:(NSNotification *)notification {
NSLog(@"will enter foreground notification");
}
- (void)viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];
NSLog(@"view will appear");
}
- (void)viewDidAppear:(BOOL)animated {
[super viewDidAppear:animated];
NSLog(@"view did appear");
}
//Get Notification whether Keyboard is showing or not [self registerForKeyboardNotifications];
#pragma mark - Notify Keyboard is Showing / Hiding - (void)registerForKeyboardNotifications { // Call this method somewhere in your view controller setup code. [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWasShown:) name:UIKeyboardDidShowNotification object:nil]; [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillBeHidden:) name:UIKeyboardWillHideNotification object:nil]; }
#pragma mark - Handle Keyboard Notification- (void)keyboardWasShown:(NSNotification*)aNotification { // Called when the UIKeyboardDidShowNotification is sent. //Create Animation Block for UIView//NSDictionary* info = [aNotification userInfo]; //CGRect beginFrame = [[info objectForKey:UIKeyboardFrameBeginUserInfoKey] CGRectValue]; //CGRect endFrame = [[info objectForKey:UIKeyboardFrameEndUserInfoKey] CGRectValue]; //DebugLog(@"beginFrame %f",beginFrame.size.height); //DebugLog(@"endFrame %f",endFrame.size.height); } - (void)keyboardWillBeHidden:(NSNotification*)aNotification { // Called when the UIKeyboardWillHideNotification is sent }