Dimiss ViewController when application inactive - ios

How to dismiss a view controller when the user is minimize/inactive the application.
Situation
- when user pressed on button. the app will be minimize, when user active back the application, user need to sign in again the application.

Use NSNotificationCenter to be alerted when the application will enter the background. Add the following in the view controller you want to handle this stuff in, such as the init or viewDidLoad methods:
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(methodToHandleItHere) name:UIApplicationWillEnterBackgroundNotification object:nil];
Then handle it accordingly:
- (void)methodToHandleItHere {
// handle it
}

Related

[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?
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.

Dealing with the same NSNotification in multiple view controllers

I'm using the below notifications to reload ViewControllerA when my app comes back from background mode. It works correctly, but the applicationEnteredForeground: method gets called every time when I open the app again. For example if I close the app when ViewControllerB or ViewControllerC is on the screen and open it again the method will be called despite the viewDidLoad of ViewControllerB doesn't contain applicationEnteredForeground: method. I would like to know that how could solve this issue? My goal is to use applicationEnteredForeground: only when ViewControllerA was on the screen before I closed the app.
As a possible solution I would just remove the NSNotificationCenter in the viewDidDisappear, but since the observer is in the viewDidLoad it won't work when the user navigates back, because viewDidLoad won't be called again. Is there any fix for this?
- (void)viewDidLoad {
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(applicationEnteredForeground:)
name:UIApplicationWillEnterForegroundNotification
object:nil];
}
- (void)applicationEnteredForeground:(NSNotification *)notification {
// do stuff...
}
You should remove ViewController A's event listener on viewWillDisappear and add it in viewWillAppear. That way, VC A will only be listening when it is the visible view controller.
You can check if a view controller is on screen by checking the window property of it's view. It will work in most standard cases.
- (void)applicationEnteredForeground:(NSNotification *)notification
{
if (self.view.window == nil) {
// Not on screen
return;
}
// do stuff...
}

iOS: Do I need to unregister in a navigation controller from keyboard events every time the view disappears?

I'm working on my embedded table views. They should scroll up when the keyboard hides some textfields. But I have several view controllers in my navigation view controller with this behaviour. So far my code registration and unregistration code is:
- (void)viewDidLoad
{
[super viewDidLoad];
[self registerForKeyboardNotifications];
}
// Call this method somewhere in your view controller setup code.
- (void)registerForKeyboardNotifications
{
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(keyboardWasShown:)
name:UIKeyboardDidShowNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(keyboardWillBeHidden:)
name:UIKeyboardWillHideNotification object:nil];
}
- (void)dealloc
{
[[NSNotificationCenter defaultCenter] removeObserver:self];
}
But if I'm showing the keyboard in let's say view controller number 3 in my navigation controller, the keyboardWasShown method is called three times. Doesn't this matter or do I need to unregister everytime the viewWillDisappear?
You should move [self registerForKeyboardNotifications]; to viewDidAppear and unregister in viewDidDisappear.
By registering in viewDidLoad and unregistering in dealloc, especially within a navigation controller, the notification will fire once for every view controller on the navigation stack. You only need to call it for the currently visible view.
Alternatively, you could subclass the navigation controller and have it call a method on its currently visible controller. Then you don't have to do all this registering and unregistering. Just register once in a nav controller subclass and have that class pass the message along to the proper view controller.

Reload webview from app delegate not working

I want to reload a webview in my view controller when my app comes to the foreground. I have created a view controller object in my application delegate's -applicationWillEnterForeground and -applicationDidBecomeActive methods. But I am not able to refresh my webview.
applicationWillEnterForeground may not be the best way to do it.
In the class that contains the webView add a notification method like below
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(didEnterForeground)
name:UIApplicationWillEnterForegroundNotification
object:nil];
-(void) didEnterForeGround
{
//[webView reload];
}
When your application comes to Foreground is app is active state , in particular view controller on viewdidload or viewdidAppear Method [WEBVIEW ReloadData]; thats it , you donot wan to do any thing in appdelegate class

How to manage view navigation in iphone?

Suppose, in my xiphone project on Xcode, I have one splash screen "a", 1st view "b", 2nd view "c", a button "back" on 2nd view. Now my task is, when my app will launch at the first time, splash screen will be launched. then "b" will be shown. Then go to "c". After that when again I will launch the app, the view "c" will be shown. And if I want to see the view "b", I have to press "back" button on view "c". Otherwise view "b" will never been shown.
This is my problem.
how could I solve the problem?
You can use notifications. Thus, in view controller C, you can:
- (void)viewDidAppear:(BOOL)animated
{
[super viewDidAppear:animated];
// add notification to be informed if the app becomes active while
// this view controller is active, and if so, to invoke the
// appDidBecomeActive method.
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(appDidBecomeActive)
name:UIApplicationDidBecomeActiveNotification
object:nil];
}
- (void)appDidBecomeActive
{
// do whatever you want in this method. for example, if you did
// a modal segue (or presentViewController) to present this
// view controller's view, then you'd dismiss like thus:
[self dismissViewControllerAnimated:NO completion:nil];
}
- (void)viewWillDisappear:(BOOL)animated
{
// when this view disappears, we should remove the observer on
// the UIApplicationDidBecomeActiveNotification
[[NSNotificationCenter defaultCenter] removeObserver:self
name:UIApplicationDidBecomeActiveNotification
object:nil];
}

Resources