Settings Changed Notification In Subclass Of IASKAppSettingsViewController - ios

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.

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.

NSNotificationCenter - Observer selectors not being called

I've gone from having all my observers selectors of multiple NSNotifications being called to none of them working. Is there an XCode/Application setting that I may have disabled that could cause this behaviour?
This issue affects just about every class that I have registered as an observer. Everything was working fine a few days ago. I'm setting the observers mainly in viewDidLoad and removing them in dealloc.
Just to add: All notifications are being posted as I have already tested for this.
I ended up figuring it out. I can't believe I missed this.
The common super class for all observers was removing itself as an observer for all notifications in viewDidDisappear.
I have now changed this to only remove itself as an observer for a specific NSNotification using [[NSNotificationCenter defaultCenter] removeObserver:name:object:];

Delay NSNotification until another is complete

I have a global function that downloads JSON data from a server and parses it. It normally takes about 5 seconds to run, so we do this in the background and then send out a notification, kNotificationDownloadSuccessful.
One of the several observers of the notification is a MapView, who re-draws annotations based on the data it received. This, obviously, has to happen after it's told the data is ready by receiving kNotificationDownloadSuccessful.
In some cases I want to re-center the map on the new data. This, obviously, cannot happen until the map has completed drawing annotations. So I implemented a second notification, kNotificationMapLocationSet, which the callers fire if they want this to occur, and the map listens for it.
Now the problem... kNotificationMapLocationSet is being received before kNotificationDownloadSuccessful, which makes perfect sense really. The Map can't simply re-post it, or just call it's local re-center method, because it doesn't know if the recenter is required.
I thought about having the kNotificationMapLocationSet set a flag in the Map, and then have the handler on kNotificationDownloadSuccessful look at it and recenter if desired. But then that would fail in the case where the messages are received in their current order.
So is there a way to order the notifications? IE, delay this one until that one fires?
I believe you can use addObserver to check when the NSNotification is done. Let me know if this works:
NotificationCenter defaultCenter] addObserver:self
selector:#selector(callNextNotification:) name:#"notificationSent" object:nil];
It looks like you subclassed NSNotification, but this should still work.

Refreshing Parse.com data when the app becomes active

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

what is the function that is called when the app is appearing?

Imagine the app is running and you press the iphone button (the phone button) and you exit the app. then you tap on the app again to enter the app. My problem is that when ever the user does this I want the viewWillAppear or viewDidAppear functions to be called, but unfortunately none of these functions gets called.
I want to know if these function won't get called, then what is the function that is called when the app is appearing again?
How about - (void)applicationDidBecomeActive:(UIApplication *)application in your UIApplicationDelegate?
Look at UIApplicationDelegate. -applicationDidBecomeActive: is what you are looking for.
You can also register for notifications in your classes (UIApplicationDidBecomeActiveNotification). This may be simpler to implement than having your app delegate handle everything since you can have, for example, each view controller manage itself.
(Use NSNotificationCenter's -addObserver:selector:name:object: to register, don't forget to unregister during object cleanup, typically in -dealloc.)

Resources