Pause application when user gets a phone call in iphone - 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.
}

Related

Detect UIAlert / Notification

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.

Marking incoming calls and text messages

I am trying to mark each time an incoming call or a text message arrives while using my app. This is for documentation purposes, since my app deals with car safety.
Any way to keep track of this ? I only need to know when a call / text came in, not any additional data.
Yes This Method in your AppDelegate gets called when there is such an interruption
- (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.
*/
}

Is a Timer disabled automatically when app resigns active on the iphone?

Playing around with Timers, and trying to make them behave right when app is sent to background, killed etc.
Judging by the generated comments in xcode, you should disable/invalidate timers on resignActive/enterBackground events.
However, i havent done that yet, but checked what happens to my timer when i click the home button and then re-enter the app.
I basically have a method in my view that the timer triggers every second:
NSLog(#"workedTimTimerTick workedTime; %#", timeString);
And when i exit the app, the output stops, when i re-enter the app, the output starts again...
Since i'm not doing anything code-wise to the timer on those lifecycle events, how come it stops getting called?
Input appreciated!
Your app is suspended when it enters background mode (Application States and Transitions).
Your timer won't fire when the app is in background and the time spent in background isn't taken into account for the timer delay.

Is it possible to run an NSThread or NSTimer on iOS 4 device when it enters the background state?

I am writing an audio application which plays media items from the iPod music library using the iPod music player.
Now I know it is possible for the iPod to carry on playing when it is in the background, but I want to also run either a Thread or a Timer in the background where a selector method periodically checks the state of the music player so that the app can apply rules in order to apply delays between tracks, and automatically load up new media collections into the player all while in the background.
Is this possible?
If not, is there any other method for achieving this functionality?
The trick is to not use MPMusicPlayerController as when your app enters the background it actually relinquishes control to the iPod app (you can check this by closing your app and opening the iPod app).
For the music app I'm currently developing I've rolled my own music player using AVAudioSession and AVPlayer (not AVAudioPlayer). I've implemented a queue on the player, and a datasource (similar to UITableViewDatasource) which feeds the queue. All of this works eloquently whilst running in the background and has no association what ever with the iPod app (and as an added bonus you apps icon appears next to the remote controls on mulitasking bar).
If you developed something similar then when you ask your datasource for the next item in the queue, you could query your rule set and take a decision on what to do next.
Sure. You can do this in the application delegate's applicationDidEnterBackground:.
From the template:
If your application supports background execution, this method is
called instead of applicationWillTerminate: when the user quits.
Well yes and now, you can't use NSTimer because they are invalidated.
You can start a block of code to run via GCD to run in background which will have the max. execute time of 10 min.

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/