Refreshing Parse.com data when the app becomes active - ios

[Edited with solution below]
I want to call a Parse.com query whenever the app becomes active, so that if it's brought up from the foreground, a new set a data will be called.
I realize there is a method for this in the AppDelegate
- (void)applicationDidBecomeActive:(UIApplication *)application {
}
But I want to know how I can put the query information obtained in my AppDelegate and put it in my ViewController where I can display the information.
Any ideas on what I might do? Or if there is another way to make the query when the app becomes active? Thanks.
So here is what I came up with. I had my method (called getData) in the ViewController, and in the viewDidLoad method, I inserted this code
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(getData)
name:UIApplicationDidBecomeActiveNotification object:nil];
And now it works just as I had hoped. Thanks!

This is a really broad question, but what you can do is you can post a notification and make your view controller listen to it.
Then after your app re enter from background, you can post the notification and in the observer method that you implemented in the view controller, you can pull the data from Parse.
See this question to understand the sequence of the method calls.

Related

Using NSNotificationCenter to call method in main app from extensions widget?

I have a widget that I would like to call back to my main app so as to make a call to the server to update data. I looked into delegation, but registering the widget's view controller as a delegate didn't seem very practical. So I moved on to trying to use NSNotificationCenter. I have set it up, but the selector is not being called. In my main iOS app I have this in the viewDidLoad method.
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(loadNewData:)
name:kUpdateData
object:nil];
And at the bottom of that file I have this:
/**
* Updates the table when the today widget is called for updated info
**/
- (void)loadNewData:(NSNotification *) notification
{
[self loadTableData];
}
That's in my main app. Now, in my notification center widget/extension, I make this call:
[[NSNotificationCenter defaultCenter]
postNotificationName:kUpdateData
object:nil];
The postNotificationName being passed in, `kUpdateData', is a constant that is resolved to #"updateData". I can see in the debugger that the postNotificationName method is being called, but the main app is not responding to it (regardless of it is in the foreground or the background). What am I doing wrong?
As a side note, the only reason I am doing this is to remove the need for repetitive code and re-implementing things I have already made.
As far as i know extension cant access or call main app methods... what can do is either do the server execution in extension or set a value in shared NSUserDefault so when your app is brought to foreground you can check this value and connect with server accordinly.

How to call method when app enters foreground from background

Is there a simple way to call a method when my app enters the foreground from the background? I have my program call a method "nowYouSeeMe" in the viewDidLoad method, which works great when running the app the first time. When I use the app and then hit home-button it moves to the background as usual. Now when I press app icon and it brings it to the foreground I basically want it to call the nowYouSeeMe method again.
-(void)nowYouSeeMe{
NSLog(#"I'm back");
}
Any ideas??
As already mentioned there is - (void)applicationDidBecomeActive:(UIApplication *)application;in App Delegate, but most of the time you need the information not in your app delegate but your view Controller. Therefore you can use NSNotificationCenter. In your app delegate you could do the following:
NSNotificationCenter *center = [NSNotificationCenter defaultCenter];
[center postNotification:[NSNotification notificationWithName:#"appDidEnterForeground" object:nil]];
Now you can set up listeners in your UIViewControllers setup code:
NSNotificationCenter *center = [NSNotificationCenter defaultCenter];
[center addObserver:self selector:#selector(appDidEnterForeground) name:#"appDidEnterForeground" object:nil];
Now the method appDidEnterForegroundgets called on your view controller whenever the app enters the foreground.
But you don't even need to post the notification yourself because it is already defined in UIApplication. If you scroll down the documentation (UIApplication Class Reference) you see all the notifications declared in UIApplication.h, in your case UIApplicationWillEnterForegroundNotification.
Notice the difference between UIApplicationWillEnterForegroundand UIApplicationDidEnterForeground, but most times you want to use UIApplicationWillEnterForeground, just to set everything up and be ready when the app is displayed.
Of course you should define a constant somewhere for your notifcationname and if you don't need the observer anymore remove it.
Edit: you could also use #selector(nowYouSeeMe) as in your question, missed that
In the appDelegate there is method called applicationDidBecomeActive:(UIApplication *)application it is triggered when your app became active.
Try calling the method via the below app delegate method :
- (void)applicationDidBecomeActive:(UIApplication *)application;
You should implement the applicationWillEnterForeground: method in your AppDelegate, like this:
- (void)applicationWillEnterForeground:(UIApplication *)application
{
[self.controller nowYouSeeMe];
}
provided that you have a reference to that controller as a property in your AppDelegate.
As per everybody else's answer, the application delegate will get a call-out via applicationDidBecomeActive:.
The application will also post the UIApplicationDidBecomeActiveNotification notification. So anyone that isn't the app delegate can just observe that.
You shouldn't use the application delegate as a lazy does-everything singleton if you can avoid it; if it's the one specific view controller that wants to do something only if it is active and the app returns to the foreground then there's no reason to involve the application delegate — it's a behaviour of the view controller, not the application.

Settings Changed Notification In Subclass Of IASKAppSettingsViewController

I have a subclass of IASKAppSettingsViewController that I, of course, use to display and handle various events for the settings I present to the user.
In this subclass, I am trying to receive the notification that settings were changed by adding this line to -(void)viewDidLoad:
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(settingsChanged:) name:kIASKAppSettingChanged object:nil];
Problem is that the method, settingsChanged, is never being called. I figure that there is some other way of doing this or I am doing something wrong here. This procedure does work for me in other classes, so I assume there is something special about IASKAppSettingsViewController that is preventing this from working. Can someone point me in the right direction?
EDIT #1
Received a message from Future Tap via GitHub and he asked me to check the sample app. I did and the sample app does not use a subclass of IASKAppSettingsViewController. I have since added another addObserver and that is also not being called. I verified that both addObserver calls are being executed, so that is not the cause.
EDIT #2
It appears that something is terribly amiss in my app. I have multiple classes that observe this notification and they all use the same name for the method to be called (settingsChanged). When I Command-click the name of the method in one addObserver call, Xcode brings me to another class' settingsChanged method. This is probably what is causing some of my observer methods not to get called.
Working with Future Tap, we discovered that `IASKAppSettingsViewController' was removing all observers when the view disappeared. This was causing the observers that I set up to be removed and, thus, not be called. The code has been fixed and my problem is solved.

How to determine when a user has copied text

I am having a hard time how to figure out when a user has selected & copied text the default iOS way:
canPerformSelector works before presenting this menu, but I am interested in knowing after the user has pressed the copy button.
Thank You
Use NSNotification as observer for UIPasteboardChangedNotification: then every time user copies it will call a method which you specified in Notification observer
Something like this
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(ClipBoardChanged) name:UIPasteboardChangedNotification object:nil];
-(void)ClipBoardChanged{
NSLog(#"ClipBoard data changed %#",[UIPasteboard generalPasteboard].string);
}

Recursive Notifications

My application is IOS application.
I have a scenario typical to the calendar iPad application, when the user goto to a date from a view, each views update itself and notify others. The problem is that notifications between views never ends.
I am using the NSNotificationCenter
[[NSNotificationCenter defaultCenter] postNotificationName:SelectedDateChangedNotification object:self userInfo:nil];
I would like to explore some ideas of how effectively implement this scenario.
Thanks
You need to have two ways of updating each of your views. The first way is when the user updates it and the second way when it gets updated by a different view. The first way sends notifications to the other views and the second way does not. From an implementation standpoint, the first way would just be a wrapper around the second way which first notifies the other views.

Resources