- Home /
iOS: how to detect when the game moves into background/foreground
I am reading http://developer.apple.com/library/ios/#documentation/NetworkingInternet/Conceptual/GameKit_Guide/Users/Users.html. It mentions, "Important: Games that support multitasking should take special note of this behavior. When your game moves into the background, the player may launch the Game Center application and sign out. "
How to detect when your game/app moves into background/foreground?
Thanks in advance for your help.
Answer by Peter G · Sep 03, 2011 at 07:53 PM
I think that you just need a quick modification to the player. You need a class that receives messages from UIApplicationDelegate (it will probably make you login to view that page). You can use the AppController
class that the Unity player creates if you want. You need to respond to some of these messages:
- (void)applicationWillEnterForeground:(UIApplication *)application
- (void)applicationDidBecomeActive:(UIApplication *)application
- (void)applicationDidEnterBackground:(UIApplication *)application
These are called when the application goes from the background to active and vice-versa. What you do is up to you, but there are some different options. If you want to communicate with unity, you can pass some info with:
UnitySendMessage();
//I think the signature is ("Message" , "Object" , "Parameter")
//But you can check the manual to make sure.
Thanks. I cant find UnitySend$$anonymous$$essage(). I found GameObject.Send$$anonymous$$essage(). Also, your solution seems to deal with Objective-C directly. Does that mean I have to write plugin?
It is sort of a plugin. You are really just modifying an existing class in Xcode. This is really the simplest plugin you could write. You are just adding to methods to an existing class. UnitySend$$anonymous$$essage() is a function for calling methods in the Unity engine from native code. Check out the page on iOS plugins for more information
I found UnitySend$$anonymous$$essage("GameObjectName1", "$$anonymous$$ethodName1", "$$anonymous$$essage to send"); on http://unity3d.com/support/documentation/$$anonymous$$anual/Plugins.html. Thanks.
Answer by Bampf · Apr 13, 2012 at 04:34 PM
Unity on iOS will send OnApplicationPause to game objects when the application is put into the background, and when it comes back-- a boolean tells you which one just happened.
Here's a C# example from one of my projects:
For example:
void OnApplicationPause(bool paused)
{
if (!paused)
{
// Application came back to the fore; double-check authentication
Social.localUser.Authenticate (ProcessAuthentication);
}
}
Nice catch. This definitely needs to be the new correct answer.
Hey $$anonymous$$att,
When you say boolean, what/where is that value, or how is it represented, so that we would know how to access it on iOS?
In C#, it's of type "bool". Unity passes it to you as an argument of OnApplicationPause. I'll edit my answer to add a simple example.
Hey Bampf, thank you. I don't know how I overlooked that OnApplicationPause takes a bool parameter. I think it may have to do with Unity always posting their documentation in JavaScript, but this helped me out a lot. Thanks again.
Excellent Answer!, going to xCode and check applicationDidEnterBackground is not required as Unity already provided OnApplicationPause() $$anonymous$$ethod which is called each time application enters background
Your answer
Follow this Question
Related Questions
The name 'Joystick' does not denote a valid type ('not found') 2 Answers
add is not a member of UnityEngine.Component 5 Answers
Invalid PBX project Unity 5 0 Answers
[Network] Damage Not receive ? 1 Answer
Problem with WWW with Unity 5 0 Answers