Detect UIAlert / Notification - iphone

I'm using Cocos2D to write a game for iOS. I have implemented a pause feature that allows the user to a pause menu on request. I also implement a complete pause (unschedule main game loop) when receiving a applicationWillResignActive. On applicationWillEnterForeground and applicationDidBecomeActive I resume the main game loop, but then throw the player into the pause menu, forcing them to resume when ready. Everything seems to be working well except for one situation. When receiving a calendar event alarm (which I assume is a UIAlert), the game does not throw up a pause menu or pause the game.
My three questions: must I include both willEnterForeground and didBecomeActive, or is one good enough for my purposes? What is the call I must schedule for to receive UIAlerts (if that is in fact what triggers my error in the above case) and how should I handle them? Is there some way to simulate a UIAlert so I can test/debug?
Much appreciated.
Edit -
The problem was in my overall handling of said events. As -clearly- stated in apple's documentation, use willResignActive to pause the game (i.e. throw up a pause menu and stop the game loop), and didEnterForeground to resume the game loop, but keep the pause menu up. By handling the events this way, there was no need to schedule to receive UIAlerts or Local/Remote Notifications.

When ever you will get a calendar event you will get a call of applicationWillResignActive: , this is a place to handle cocos2D pause or any-other thing you want to do at calendar event reception. If calender event ignored applicationDidBecomeActive will be called. If not ignored then you will have to resume from same you are doing.

Related

Pause application when user gets a phone call in iphone

I have a quiz application in which the duration of each question is 20 sec. It works fine, but in case, if a user gets a call while playing, I want to pause the application & set it in background.
Does anyone have any idea?
It depends on the implementation of your app but, broadly, your application should stop any running timers, pause the game, pause any sound playback, etc. in the applicationWillResignActive: method.
When the call is finished, your app is invoked again and the applicationDidBecomeActive: method is called. You should resume timers, sound playback, etc. from that method. If it's a game, you should not resume it automatically; keep it paused and wait for the user to resume it manually to comply with Apple's UX guidelines.
When a call comes in, your application is suspended.
And you'll know this is happening because your "UIApplicationDelegate" will get a message of "applicationWillResignActive:". When it comes back, you can start up where you left off or you can bring up a new question or whatever you want to do, it's up to how you decide to implement your quiz app.
When user gets a phone call in iphone application automatically goes into applicationDidEnterBackground state.for example if you are working on radio app then radio automatically get off when user get any call after call ended application comes into below method(appDelegate.m)
(void)applicationWillResignActive:(UIApplication *)application
{
// Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
// Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game.
}

Multitasking in iphone?

I am developing the Game iPhone application, it has Music player and and few animation. The game is interrupted by a text message or call it is terminated. It should pause the game until you click cancel. But While sound plays if i receive the call or message, audio player is pause, after accept/decline call, it doesn't play continue. how to manage this? actualy the
Have a look at the UIApplicationDelegate reference. There you'll see the various methods that are called as events beyond your control occur (such as a phone call coming in, etc.)
In your case I believe you want to override the applicationWillResignActive and applicationDidBecomeActive methods to handle pausing the game, sounds, etc. and restoring them.
UPDATE: I found this blog post very helpful in understanding all the multitasking delegates: http://www.cocoanetics.com/2010/07/understanding-ios-4-backgrounding-and-delegate-messaging/

Receiving Notifications with App in background mode

I have an app, that will keep track of everything the user do in the iPod app. To do this, I added few observers to NSNotificationCenter, like MPMusicPlayerControllerNowPlayingItemDidChangeNotification. But my problem is, I only get those notifications when my app is in the foreground, if its in the background, the system add the notification to a queue, and then the next time my app becomes active it delivers it to me. I have no interest in this queue, since I want to receive real-time notifications.
Is there any way for me to get those notifications even if my app is in suspended state? I want to run just 3 lines of code everytime I get this NowPlayingItemDidChange notifications for example.
Here is where I add the observer.
MPMusicPlayerController *iPodMediaPlayer = [MPMusicPlayerController iPodMusicPlayer];
NSNotificationCenter *notificationCenter = [NSNotificationCenter defaultCenter];
[notificationCenter addObserver: self selector: #selector(handle_NowPlayingItemChanged:) name:MPMusicPlayerControllerNowPlayingItemDidChangeNotification
object:iPodMediaPlayer];
[iPodMediaPlayer beginGeneratingPlaybackNotifications];
Also, if I add another kind of object to the observer instead of iPodMediaPlayer, the observer won't call the method.
Thanks a lot,
Abras
iOS applications are suspended when they are not in the foreground. There are three exceptions to this rule. You can have code execute in the background if your application is
a) Playing audio. This means the application itself is actually generating audio. My understanding is that the MPMediaPlayerController iPodMusicPlayer object only controls the playback of the external iPod process, rather than playing audio from the app itself. Perhaps you could have some success if you called applicationMusicPlayer instead of iPodMusicPlayer and set the appropriate background flags in your applications Info.plist. This seems like the most legitimate way to get your application to work, but you wouldn't be able to control iPod playback from the iPod app, only your app and the system audio controls.
b) Get your app to monitor the location. If the app is using the GPS it can continue to execute in the background. Downside to this is that the GPS will drain battery, and users might be creeped out that you're requesting their location.
c) Ask UIApplication for extra time. If you use UIApplication's beginBackgroundTask method, your application will continue to run for a finite amount of time in the background. If your users are going to come into your application once every ten minutes or so, this could work as well.
Hope that helps.
Multitasking in iOS is currently a very restricted one. You may start a background task using beginBackgroundTaskWithExpirationHandler: method (of an UIApplication object), but it is intended to finish a finite-length task before going suspended. All background tasks may expire (and get terminated) before it finishes its job. You can check how much longer your application can run by checking backgroundTimeRemaining property of the application object.
As I explained here iOS receive Media Player Notifications when app is in background, there seems no way to get notifications from iPodMusicPlayer.
About Omar Raul Qazi answer:
a) i tried and I had no success. The music kept going down when pressing home button. I think this background flag only works for normal AudioSessions and not for MPMusicPlayer...
b) I am not sure this would work and I don't think Apple would like it when looking for approval
c) You can run in background only synchronous task. You cannot wait there for a notification. Am I wrong?

Multitasking: Stop Background Audio at Specific Time

I am developing an iphone app which uses background audio (on an infinite loop) to continue playing after the app has entered the background.
My problem is I want to implement a "sleep timer" which stops playback after a specified period of time.
Is this possible? I have spent an hour looking for a method to do this with no avail.
EDIT: My current thought is to use a lower level API, the Audio Queue Services, and manually re-fill the queue with another instance of the loop during the AudioQueueOutputCallback. If the timer has expired I do not fill the loop. I'm assuming this should work since the documentation says audio callbacks are still fired when an app is playing multitasking background audio. Can anyone think of a better way or a reason why this wouldn't work?
While you queue sound data on the background your app remains fully functional and running as if it was in the foreground (well almost), so yes, you should just write a timer that stops the playback at a given time and it will be fired as expected.
Now to the second question: once you stop queueing things up, your app will be "frozen" until the user manually brings it to the foreground... So what you should do is start queueing audio data from the second file before the first one is done playing, and if you DO need to pause or stop, maybe a solution is to play 0 bytes (silence)?
I'm not actually sure this would be allowed in the App Store. An app is not allowed to execute at all in the background, with the exception of VoIP apps and push notifications.

Sounds lost after interruption

I'm using the SoundEngine provided with Apple's crash landing example.
After an interruption such as an incoming phone call or an alarm I call to applicationWillResignActive: inside my delegate, in order to pause the game and save the state of it. After the interruption ends I return to my game but the sound is gone. Even if i reinitialize it with SoundEngine_Initialize() the game still wont reproduce any of the sounds, unless i restart my app.
How can I restore my game sounds after the interruption ?
Have you looked at the sound manager class used by 71squared ? You can likely just use their sound manager as is... but if you wanted to role your own, you can look at thier code as I know they have solved this in their code.
http://www.71squared.com/2010/01/latest-sound-manager-class/