1

I have a seemingly simple task that I am trying to do. When a user sends my application to the background, I would like to conduct an NSLog to say "app sent to background". I am trying to work with the NSNotificationCenter like What's the best way to detect when the app is entering the background for my view?, but I am unable to get it to work.

Is it not possible to perform an action right when the user sends the app to the background? Any help would be great!

Thanks!

Community
  • 1
  • 1
user2492064
  • 591
  • 1
  • 8
  • 21

3 Answers3

2

You may use NSLog in method of

- (void)applicationDidEnterBackground:(UIApplication *)application

Your AppDelegate.m file.

Where you can send NSNotification for any task. You can not perform any task when app is in background.

Pradhyuman sinh
  • 3,936
  • 1
  • 23
  • 38
Himanshu Gupta
  • 207
  • 1
  • 6
1

You need to implement the following method in AppDelegate:

- (void)applicationDidEnterBackground:(UIApplication *)application {
    NSLog(@"app sent to background");
}

Apple's documentation:

Your implementation of this method has approximately five seconds to perform any tasks and return. If you need additional time to perform any final tasks, you can request additional execution time from the system by calling beginBackgroundTaskWithExpirationHandler:.

Vinay Jain
  • 2,644
  • 3
  • 26
  • 44
  • Thanks Parcs, what if I wanted to perform other tasks in this method (i.e. save data to my server). is that possible? – user2492064 Dec 02 '13 at 06:03
  • @user2492064 if you register your app for the extended time privilege when it goes to the background then you can send a server request. I'll look around for instructions as I cannot remember what exactly is required and post back here if I find something – bennythemink Dec 02 '13 at 06:12
  • @user2492064 this post has what I'm talking about http://stackoverflow.com/a/16877394/467608 – bennythemink Dec 02 '13 at 06:15
  • @user2492064 : You have approx. 5 seconds to do whatever you want in this method and if your implementation takes more than 5 seconds then you need to but more time from the OS. – Vinay Jain Dec 02 '13 at 06:38
0

You can do a 10 min background activity in pre iOS7 like save some thing to your server but in order to wind up when background activity is about to be closed you need to use this in your - (void)applicationDidEnterBackground:(UIApplication *)application

     - (void)applicationDidEnterBackground:(UIApplication *)application

{

    bgTask = [application beginBackgroundTaskWithExpirationHandler:^{

        // Clean up any unfinished task business by marking where you

        // stopped or ending the task outright.

        [application endBackgroundTask:bgTask];

        bgTask = UIBackgroundTaskInvalid;

    }];



    // Start the long-running task and return immediately.

    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{



        // Do the work associated with the task, preferably in chunks.



        [application endBackgroundTask:bgTask];

        bgTask = UIBackgroundTaskInvalid;

    });

}

For more detail look here

the summary is

  1. your - (void)applicationDidEnterBackground:(UIApplication *)application method must return within 5 seconds or else your app will be purged.
  2. you can how ever ask for more time using the above method the time allowed in practice is 10 mins
amar
  • 4,285
  • 8
  • 40
  • 52