Reload webview from app delegate not working - ios

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

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.

How to know if viewWillAppear: or viewDidAppear: are called because app is going to background?

I know you can listen for notifications for such app state, but... is it possible to know that way?
I need to know when the view is going to disappear because of another view is being shown, and not because the app is going to background state.
Thanks
viewWillAppear: and viewDidAppear: will not get called when app is going to background/returning to front as the state of the view in relation to the app isn't changing.
you can only observe the UIApplicationDidEnterBG / willEnterFG notifications
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(onAppWillEnterForeground:)
name:UIApplicationWillEnterForegroundNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(onAppDidEnterBackground:)
name:UIApplicationDidEnterBackgroundNotification object:nil];
NOTE: if you add observers in willAppear and remove them in didDisappear you can fake willDisappear/viewDidAppear yourself
You should register a UIApplicationWillEnterForegroundNotification in your ViewController's viewDidLoad method and whenever app comes back from background you can do whatever you want to do in the method registered for notification. ViewController's viewWillAppear or viewDidAppear won't be called when app comes back from background to foreground.
-(void)viewDidLoad{
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(doYourMethod)
name:UIApplicationWillEnterForegroundNotification
object:nil];
}
-(void)doYourMethod{
// do whatever you want to do when app comes back from background.
}
Don't forget to unregister the notification you are registered for.
-(void)dealloc {
[[NSNotificationCenter defaultCenter] removeObserver:self];
}
You mean viewWillDisappear ? If so, when app is going to background, viewWillDisappearwould not be invoked, so is viewDidDisappear, you can observe the notifications as #Daij-Djan said.
About viewWillDisapper, Apple says
This method is called in response to a view being removed from a view hierarchy. This method is called before the view is actually removed and before any animations are configured.

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...
}

Dimiss ViewController when application inactive

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
}

Can i load second viewDidload in first viewDidload?

Hi guys i have a dual viewcontroller.
My firstviewcontroller have a button and this button send NSNotification and secontviewController receive this notification and NSLOG any string.
But if i dont load secontView; my Notification is dont work ..
This codes in my firstViewController.m
-(IBAction)tapper:(id)sender {
[[NSNotificationCenter defaultCenter] postNotificationName:#"Twitter" object:nil];
}
and this codes in my secontViewController.m
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector( receiveNotificaiton: ) name:#"Twitter" object:nil];
}
-(void)receiveNotificaiton: (NSNotification *) notification {
NSLog(#"TWÄ°TTER");
}
How can i load second viewDidload in first viewDidload or something else?
It sounds like you want your first view controller to present the second one. Correct?
If so you should not be using notifications. You can simply present the second view controller from the first, take a look at "Presenting View Controllers from Other View Controllers"
It's not the right way post a notification to a view controller which doesn't exist yet because (as you said) it can't execute the istruction for becoming observer. So a solution can be present the second view controller and passing the notification information. Using the notification center is a good solution if the second view controller is the top view controleller and something else post the notification. I hope i helped you.
I fixed my problem with prepareForSegue method , thx them all.

Resources