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.
*/
}
Related
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.
}
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.
i establish a connection between two iOS devices with GameKit and GKMatch for a synchronous game. When one player enter the background the connection will disconnect. How can I avoid this behavior? Is it possible to keep the connection via GKMatch alive while one user did suspend the app to the background?
According to Apple's documentation:
As soon as your game moves to the background, the value of the local player object’s authenticated property becomes and remains invalid until your game moves back to the foreground. You cannot read the value to determine if the player is still authenticated until Game Kit reauthenticates the player and calls your authentication handler. Your game must act as though there is not an authenticated player until your completion handler is called. Once your handler is called, value stored in the authenticated property is valid again.
Source: Game Center Programming Guide, pg. 39.
Unless your App employs VOIP or special circumstances apply, you shouldn't really be keeping a network connection open in the background. (If you are interested though, check out this SO question for a hacker-ish way of going about that).
As your player authentication is invalidated on entering the background, another device (if it does manage to receive network data from the background App) will not (reliably) know which GKPlayer sent the information.
So while Apple don't come out and say it explicitly, all Game Center features should normally be cancelled when the Application enters the background, and resumed once the player is re-authenticated upon relaunch.
As a side note, when an App withdraws to the background it is (usually, depending on background tasks) deemed "eligible for suspension". This is one reason there are such restrictions on multitasking execution - your App could be killed at any moment to free memory and you don't really want a heavy duty data transfer going on when that happens!
See TN227 if you'd like to know more about networking and multitasking.
EDIT: An alternate method:
While it is not possible to keep a GKMatch alive while an App is running in the background, an alternative way to emulate this behaviour would be to save game state to your own server. Ask the user to interact with some UI (press a button maybe) before she/he leaves the game, which saves the current game state to your own server and notifies the other player that you are about to leave the game. Upon relaunch, query your server to see if you have any saved games, and if so, load the game state and send a notification to the other player. This doesn't strictly maintain the same game, but is one way of approximating the behaviour you're after.
This is not possible
you can do like when one player enter in background
in this event
- (void) applicationDidEnterBackground:(UIApplication *)application { }
send the message to the second player in packet data that first player goes in background
then in
- (void) receiveData:(NSData *)data fromPeer:(NSString *)peer inSession: (GKSession *)session context:(void *)context;
{
}
when perticuler that data get stop your application and pop to previous view
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/
While going through the iOS-4 Multitasking for fast context switching, I have a doubt regarding save last state of application.
Do applications have to manually save the last state in "- (void)applicationDidEnterBackground:(UIApplication *)application"? Or iOS-4 will take care of it?
In the video it's mentioned as follows:
-(void)applicationDidEnterBackground:(UIApplication *)application {
// save app state
[self saveState];
// reduce memory usages
....
// prepare UI
....
// close listening sockets
....
}
Thanks in advance,
Sunil
Once your application has entered background, there's no guarantee it will ever come back to the foreground. It may at any point in time be terminated, without notification of any kind. Thus, entering background, you want to save the state or risk losing it.
To quote Apple (source: http://developer.apple.com/iphone/library/documentation/iphone/conceptual/iphoneosprogrammingguide/BackgroundExecution/BackgroundExecution.html),
Save your application state before moving to the background. During low-memory conditions, background applications are purged from memory to free up space. Suspended applications are purged first, and no notice is given to the application before it is purged. As a result, before moving to the background, an application should always save enough state information to reconstitute itself later if necessary. Restoring your application to its previous state also provides consistency for the user, who will see a snapshot of your application’s main window briefly when it is relaunched.
Do applications have to manually save the last state in "- (void)applicationDidEnterBackground:(UIApplication *)application"? Or iOS-4 will take care of it?
Yes, if you want your app to restore after it's been killed, you need to manually save state here.
you should use these 2 methods in your app delegate to save current state before entering background/terminate.
- (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.
*/
}
- (void)applicationWillTerminate:(UIApplication *)application {
/*
Called when the application is about to terminate.
See also applicationDidEnterBackground:.
*/
}