Responding to EKEventStoreChangedNotification - iphone

I'm trying to listen for EKEventStoreChangedNotification to check if the calendar is changed while my app is in background.
I register the observer in a view controller's initWithNibMethod like this:
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(calendarChanged:) name:EKEventStoreChangedNotification object:nil];
The calendarChanged method just logs a message on the console to check if it's called.
Problem is my observer method never gets called (the observer object is still valid). From what I understand, unless an app is registered to do background execution (my app is not set up for this) the notifications of that type should be coalesced and delivered on entering foreground.

I think the "object:" needs to set with the EventStore object you're using.
Yes, you won't get called whilst you're in the background but your "calendarChanged:" selector will be called when your app' comes in to foreground.

Related

How to make custom notification so that whenever event occurs it will handle in iOS

Hi I want to make custom local notification so that whenever popover is visible it will handle by that notification.
so that as multiple time the popover is visible it will handle by that notification
Currently i have partially done this but problem is that if i want to run notification's selector method multiple times i have to post that notification wherever i want.
used this link to implement it
i want to send some notifications to observers when some event occurs. and i also want to know how observer catch/handle/receive that notification?
Can i make notification like, once i postNotification in viewDidLoad it will handle as many times that event occurs?
Note-See the answer posted by me
Use this
line before presenting popover
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(dissmissPop:) name:#"popOver" object:popOver.contentViewController];//popOver is your name of popover
-(void)dissmissPop:(id)sender{
//method to be called
}
To call notification from popover use this
[[NSNotificationCenter defaultCenter] postNotificationName:#"popOver" object:self];
Notifications are same as broadcast receivers. If we register as broadcast receiver whenever new email arrives. But do you actually post the event notification? No. Someone else does. Similarly when keyboard is shown the notification is posted by the system. Send you only get notified.
The system posts whenever the keyboard is gonna appear. This should make it clear that if you want to post custom notifications you have to post it every time there's need for it to.
So if you make custom notification you have to postNotification when ever you want to post it.

iPhone applicationWillResignActive - how to notify current UIView

I want to pause a timer on my game screen when the iPhone is locked etc. My question is what is the best method to notify the current UIView, which the AppDelegate has no direct access to?
1) Your timer should probably not be managed by the view but by the view's controller. The timer itself is not an inherent part of your UI, only the timer's display is. (What happens if you want to have the timer continue after a view is removed, for example?)
2) Any object (view or controller included) can independently listen for the appropriate notification. For example, in your view controller (or view code, if you choose to go that route):
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(pauseTheTimer:)
name:UIApplicationWillResignActiveNotification
object:nil];
Then implement a pauseTheTimer: method that will handle the notification. (Since there is only one UIApplication object, you can use nil for the object, as shown.)
This approach nicely decouples your app delegate from the views and view controllers.
(Oh, don't forget to stop observing when your view is unloaded or deallocated. Failure to do so can and will lead to crashes.)

What gets called after UIApplicationDidBecomeActiveNotification gets triggered?

In my app I'm trying to make my navigation bar not shrink from 44px to 32px when the phone is rotated to horizontal orientation. I've been able to accomplish this by setting the navigationBars frame when the view is rotated and also in viewDidAppear. However, when I push the home button to exit the app and then I reenter the app, the navigation bar still shrinks. So I implemented a notification to detect UIApplicationDidBecomeActiveNotification, and in that method I reset the navigationBar frame height to 44px. However, it doesn't work because something is getting called which is resetting my views frame. Does anyone know what gets called after a UIApplicationDidBecomeActiveNotification gets triggered that resets the viewcontrollers frame?
In Your application any class can be an "observer" for different notifications. When you create view controller, you can register it as an observer for the UIApplicationDidBecomeActiveNotification and specify which method that you want call when that notification gets sent your your application.
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(myMethod:) name: UIApplicationDidBecomeActiveNotification object:nil];
remove observer in ViewWillDisappear/viewDidDisAppear/Dealloc as per your need:
[[NSNotificationCenter defaultCenter] removeObserver:self];
I'm having the same problem, if you attach an observer via Key Value Observers you can see that something is called after the UIApplicationDidBecomeActiveNotification.

Firing events accross multiple viewcontrollers from a thread in the appdelegate

I have an NSAutoreleasePool thread that is designed to pull information down from a web service, i have the web service code working nicely and i can trigger the thread to start in a view controller without any trouble, in fact its working quite nicely.
I want to:
move the thread instantiation to the appDelegate - easy!
have it run periodically and somehow tell the viewcontrollers under it (5 - 10) if new information is downloaded
have the capacity to manually execute the thread outside of the scheduler
I can fire up a method on the appdelegate using performSelectorOnMainThread but how i can get my child view controllers to "subscribe" to a method on the appdelegate?
Using NSNotificationCenter you can post well, notifications :D
That way without the appDelegate nowing the other classes the other classes can "subscribe" to the notifications they need.
Also, i would keep the thread alive, spawning a new thread everytime is costly, ofc only if it is spawned often. I would recommend using GCD ( iOS 4+ )
Here's what you do:
From the class sending the message, post a notification like :
[[NSNotificationCenter defaultCenter] postNotificationName: #"YOUR_NOTIFICATION_NAME" object: anyobjectyouwanttosendalong(can be nil)];
In the view controllers where you want to be notified of the notification when posted:
In the viewDidLoad do:
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(METHOD_YOU_WANT_TO_INVOKE_ON_NOTIFICATION_RECEIVED) name:#"YOUR_NOTIFICATION_NAME" object:sameasbefore/nil];
Important! Don't forget this in your viewDidUnload():
[[NSNotificationCenter defaultCenter] removeObserver:self name:#"YOUR_NOTIFICATION_NAME" object:sameasbefore/nil];
I'm not very sure about the object associated with notifications but you can look that up here
NOTE: When it's only one object notifying another one, you're better off using protocols :) But in this case since there are multiple view controllers listening, use notifications
Use NSNotificationCenter to send events that your view controllers are observing?

NSNotification in iphone sdk

what is the use of NSNotification in iphone sdk?
Thanks
I know this isn't generally a good way to answer questions, but RTFM.
NSNotification objects encapsulate information so that it can be broadcast to other objects by an NSNotificationCenter object. An NSNotification object (referred to as a notification) contains a name, an object, and an optional dictionary. The name is a tag identifying the notification. The object is any object that the poster of the notification wants to send to observers of that notification (typically, it is the object that posted the notification). The dictionary stores other related objects, if any. NSNotification objects are immutable objects.
You can create a notification object with the class methods notificationWithName:object: or notificationWithName:object:userInfo:. However, you don’t usually create your own notifications directly. The NSNotificationCenter methods postNotificationName:object: and postNotificationName:object:userInfo: allow you to conveniently post a notification without creating it first.
NSNotifications allow you to have a method called when a event occurs.
For example if you have a MPMoviePlayer and you want todo something when it is done you could use the following code:
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(movieFinished:) name:MPMoviePlayerPlaybackDidFinishNotification object:yourMoviePlayer.moviePlayer];
or if you want to do something when the device rotates:
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(orientationChanged:) name:UIDeviceOrientationDidChangeNotification object:self];
You should have a look at the Notifications section in the Cocoa Fundamentals Guide. There's anything you need to know about notifications there : from definition to use cases.
In terms of events NSNotifications are an alternative to delegation. Delegation can be used to notify one single delegate of an event, whereas notifications can be used to notify an arbitrary number of receivers. A notification is sent to the main notification center, which then notifies every object, that has registered for the notification.
One important difference is, that with delegation you can receive the delegates response to the event, whereas with NSNotifications you just send away the notification, but you don't know about the receivers or their response to the notification.
You register a UINotification when you want to receive a alert from iOS. So if you want to do something when a accessory is plugged in or a TV is plugged in you would register a UINotification for it and it would call a method in your app when the event occurs.