How do I detect a remote pause (on headphones) vs. incoming call - iphone

I'd like to pause my app when the user pauses music on his headphones.
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(playbackStateDidChange:)
name:MPMusicPlayerControllerPlaybackStateDidChangeNotification
object:[MPMusicPlayerController iPodMusicPlayer]];
works well. However, this is also triggered on an incoming phone call.
I added
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(appLostFocus:)
name:UIApplicationWillResignActiveNotification
object:[UIApplication sharedApplication]];
Unfortunately, this is triggered after the pause is detected.
Any ideas?

There is no documented way of doing this. It is probably doable with private APIs, but you know the song.

Related

How detect incoming and outgoing call end state? iphone

I have a requirement that is the app should disappear after install and when incoming or outgoing call, soon after the call the app should prompt and display the screen.
How disappear app after installation?
How detect incoming and outgoing call?
How display screen after ending of incoming and outgoing call?
The main thing is that i am making this for personal not of app store. So please if you have any idea about then give full suggestion and ideas.
Thanks in advance...
Use following notification to find the call status.Add CoreTelephony.framework
#import <CoreTelephony/CTCall.h>
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(callReceived:) name:CTCallStateIncoming object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(callEnded:) name:CTCallStateDisconnected object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(callConnected:) name:CTCallStateConnected object:nil];

How to monitor NSSystemClockDidChangeNotification in iPhone SDK

I Need to know if the user change the system time in the springboard settings.so my background program can be notified. According to doc, NSSystemClockDidChangeNotification is the one choice.
but I can't receiving anything by this:
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(handleSysTimeChanged:)
name:NSSystemClockDidChangeNotification
object:nil];
Where am I wrong? Any suggestions?
Your handler method must be implemented in the same class you add as observer and needs to look like this:
-(void) handleSysTimeChanged: (NSNotification*) notification
{
// ...
}

How to use NSNotification in following scenario (IPhone SDK)?

Considering real life situation, suppose i have assigned some work to 3 people(say Person A, Person B, Person C), instead of waiting for them to complete a task each, i want that when each Person completes all assigned tasks, he/she will notify me distinctly. So that i can take further decision based on his/her task.
I want to implement this situation in code, with out using separate thread and delegates, i mean using NSNotification.
How can i do this stuff with programming, can u solve above situation using code (iPhone SDK-Objective C)?
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(myMovieFinishedCallback:) name:MPMoviePlayerPlaybackDidFinishNotification object:self.moviePlayer.moviePlayer];
- (void)myMovieFinishedCallback:(NSNotification*)aNotification
{
// Release the movie instance created in playMovieAtURL
MPMoviePlayerController *theMovie = [aNotification object];
[[NSNotificationCenter defaultCenter] removeObserver:self
name:MPMoviePlayerPlaybackDidFinishNotification
object:theMovie];
[[NSNotificationCenter defaultCenter] removeObserver:self
name:UIDeviceOrientationDidChangeNotification
object:theMovie];
[theMovie stop];
[theMovie.view removeFromSuperview];
}
Like this you can use the NSNotification. Hope you understand.
As long as you are not using separate threads (or some kind of simulated asynchronicity) the 3 persons will need to wait on each other and using notifications doesn't really make sense.

NSNotification in iphone

i am sending NSSNotifcation to another view controller in iPhone app but its observer method getting notified two times how its possible can any one guide me
i have use this code to post notification
[[NSNotificationCenter defaultCenter] postNotificationName:#"updateStatusOnFacebook" object:nil userInfo:nil];
and added observer
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(postToWall) name:#"updateStatusOnFacebook" object:nil];
Have you added the observer twice?
Which method are you calling addObserver:selector:object: in? If it's in viewWillAppear then this might be called more than once.
Your method will be called the same number of times that you have added an observer.
Try this:
[[NSNotificationCenter defaultCenter] removeObserver:self name:#"updateStatusOnFacebook" object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(postToWall) name:#"updateStatusOnFacebook" object:nil];
The other reason is that you might just be sending the notification twice :)
I had the same problem crop up, and read this question, but could only find the one call to add the observer anywhere in the project.
In our case, the observer was being added twice because the method the line was in was being called twice.
Make sure you step through your code, breaking on your addObserver:selector:name:object call, to ensure that you don't have an unexpected extra execution path to that call.

Unknown iphone code

I am looking at a project which was provided me by my organization, for study.
The problem is that in this project I found some code which I never saw before.
Please tell me why the following code is written.
-(void)notifications
{
[[NSNotificationCenter defaultCenter] addObserver: self selector:
#selector(hideViews) name: #"Hide" object:nil];
}
This problem arose because this project has only some code for designing.
Sorry if this is a silly question...
You should read up on how notifications work in Cocoa. Consult Apple's documentation for more information: http://developer.apple.com/mac/library/documentation/Cocoa/Reference/Foundation/Classes/NSNotificationCenter_Class/Reference/Reference.html
Basically, NSNotificationCenter is a class that broadcasts NSNotifications from one object to potentially many observing objects. One object can post a notification
[[NSNotificationCenter defaultCenter] postNotificationName:#"NotificationName" object:self];
and other objects can listen for this notification.
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(notificationHandler:) object:theObjectThatPostedTheNotification];
Then, when the first object posts the notification, NSNotificationCenter will notify the other observing object, and notificationHandler: gets called.