Recursive Notifications - ios

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.

Related

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.

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.

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.

Best way of handling interruption (such as phone call, battery warning) when in the middle of a task

I'm aware of the method described here with applicationWillResignActive:, but that's a method within the app delegate. If I'm in a view controller that is obviously separate and doing a task, how do I tell that view controller to pause its activity from within the app delegate?
Is it best just to do this through delegates? Or is there another, more preferred way?
For the view controllers which require it you can make them observers of the application notifications. There are a range of notifications such as UIApplicationWillResignActiveNotification and UIApplicationWillEnterForegroundNotification which are all posted to the defaultCenter (NSNotificationCenter).

Is it possible to detect if a user takes a screenshot in iOS? [duplicate]

Is there a notification or other mechanism of being informed that the user is taking a screenshot with the home/power buttons?
I've seen threads about wanting to disable the taking of screenshots, but that's not what I'm looking to do.
I have a photographer client who's concerned that his works will be copied by means of users taking screenshots and I thought that if there was an opportunity to put a watermark across the image before the screenshot was taken, that would allay his fears.
The PictureWasTakenNotification Darwin notification will be sent when the user takes a screenshot. However, this is sent after the screenshot is taken.
(No notifications will be sent before the screenshot was taken.)
Here's a way which might work, although it will totally go against user interface guidelines I'm sure. If you force the user to have their finger on the screen for the image to show then I don't think they can create screenshots. Because as soon as you press the home+lock keys to actually take the screenshot, the screen seems to behave as if there are no fingers touching it. Try taking a screenshot while moving between home screens to see what I mean.
Not a perfect solution by any means but you may be able to work it into your app design if you're really clever without it detracting too much from the user experience (a tough challenge though!). Nevertheless, I believe this may allow you to display artwork/photos without allowing users to take screenshots.
Since iOS 7 the UIApplicationUserDidTakeScreenshotNotification exists. So doing something like this should detect the screenshots:
- (void)viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(userDidTakeScreenshot) name:UIApplicationUserDidTakeScreenshotNotification object:nil];
}
- (void)userDidTakeScreenshot {
// Screenshot taken, act accordingly.
}
Finally, don't forget to remove the observer:
- (void)viewWillDisappear:(BOOL)animated {
[super viewWillDisappear:animated];
[[NSNotificationCenter defaultCenter] removeObserver:self name:UIApplicationUserDidTakeScreenshotNotification object:nil];
}
What's really needed is a notification that is sent before the actual screen capture happens. A delegate method or some other means of giving the app a screenshotting-in-flight opportunity to redraw your content before the grab happens.
And there isn't one.

Resources