Así es como escuchar a este tipo de eventos:
// Register for notification when the app shuts down
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(myFunc) name:UIApplicationWillTerminateNotification object:nil];
// On iOS 4.0+ only, listen for background notification
if(&UIApplicationDidEnterBackgroundNotification != nil)
{
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(myFunc) name:UIApplicationDidEnterBackgroundNotification object:nil];
}
// On iOS 4.0+ only, listen for foreground notification
if(&UIApplicationWillEnterForegroundNotification != nil)
{
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(myFunc) name:UIApplicationWillEnterForegroundNotification object:nil];
}
Nota: Los cheques if(&SomeSymbol)
aseguran que su código funcionará en iOS 4.0 o superior y también en iOS 3.x - si se construye contra un iOS 4.x o 5.x SDK y establezca el destino de implementación en iOS 3.x su aplicación aún se puede ejecutar en dispositivos 3.x pero la dirección de los símbolos relevantes será nula, y por lo tanto no intentará preguntar para las notificaciones que no existen en los dispositivos 3.x (que bloquearían la aplicación).
Actualización: En este caso, los if(&Symbol)
cheques son ahora redundantes (a menos que realmente necesita para apoyar iOS 3, por alguna razón). Sin embargo, es útil conocer esta técnica para verificar si existe una API antes de usarla. Prefiero esta técnica que probar la versión del sistema operativo porque está comprobando si la API específica está presente en lugar de utilizar el conocimiento externo de qué API están presentes en qué versiones del sistema operativo.
gracias por la respuesta rápida! –