0

I am working with iphone background process in my application. it is running in background but after 10 min it is terminating my application. And not working for long time.

My application contains 1 counter with timmer when viewdidload at that time my counter will start and when i will click on home button it will run in the background. In background it is running perfect but after 10 min it is stoping my background process. Following is my code for it.

in .h file.

IBOutlet UILabel *thecount;
int count;
NSTimer *theTimer;
UIBackgroundTaskIdentifier counterTask;

in my .m

- (void)viewDidLoad {

    UIBackgroundTaskIdentifier bgTask = nil;
    UIApplication  *app = [UIApplication sharedApplication];


    counterTask = [[UIApplication sharedApplication]
                   beginBackgroundTaskWithExpirationHandler:^{
                       [app endBackgroundTask:counterTask]; 
                       //counterTask = UIBackgroundTaskInvalid;

                       // If you're worried about exceeding 10 minutes, handle it here
                       theTimer=[NSTimer scheduledTimerWithTimeInterval:0.5
                                                                 target:self
                                                               selector:@selector(countUp)
                                                               userInfo:nil
                                                                repeats:YES];
                   }];

    count=0;
    theTimer=[NSTimer scheduledTimerWithTimeInterval:0.5
                         target:self
                         selector:@selector(countUp)
                         userInfo:nil
                         repeats:YES];

    [super viewDidLoad];
}
  • (void)countUp {

     count++;
     NSString *currentCount;
    
     currentCount=[[NSString alloc] initWithFormat:@"%d",count];
     NSLog(@"timer running in background :%@",currentCount);
     thecount.text=currentCount;
     [currentCount release];
    

}

The above code just sample to count number foreground and background. but after 10 min it is not working.

So please help me and provide me some help for it.

Hrushikesh Betai
  • 2,227
  • 5
  • 33
  • 63
  • 1
    from more kind googling i found how to implement the background process and it'll work for all ios except ios 3.0.. http://www.mindsizzlers.com/2011/07/ios-background-location/ – Hrushikesh Betai Nov 09 '12 at 10:21

1 Answers1

4

10 minutes is the maximum you get for long-running background tasks. iOS doesn't promote the idea of long-running background processes and for the sake for your user, you shouldn't try to do them.

In order to have your code running for longer than that, some other external events may make your app be woken up again or be kept running, e.g. GPS location events, playing audio or incoming VoIP connections in your VoIP-type app.

macbirdie
  • 16,086
  • 6
  • 47
  • 54
  • 1
    thanks for your reply. in my application i am sending gps data to server at some time to time. above is just code to work in back ground. will you please provide some detail sample to how to send gps data to my server at some time and the application should run. so i can understand it. – Hrushikesh Betai Feb 22 '12 at 10:33
  • So whenever you get a notification of device's location, you can schedule the task to send that location to the server. You can do that from the `locationManager:didUpdateToLocation:fromLocation` method in your `CLLocationManagerDelegate`-protocol-implementing class. – macbirdie Feb 22 '12 at 11:02
  • ohh, you didn't get my point i am tracking user in background mode at some particular time difference. so i have to implement like background task. [i want to implement like this](http://stackoverflow.com/questions/5323634/app-running-in-background) but i cant't understand the answer properly. would like to guide me? – Hrushikesh Betai Feb 22 '12 at 11:05
  • 1
    Oh, right. As you can see from the thread you linked, it's not possible to arbitrarily run your code, say, an hour from the time of going into background. You can only set up the minimum distance user's device will move for the app to be notified of the change. If you declare that your app is playing background audio or handles VoIP services when it's really not, your app will get rejected by Apple. – macbirdie Feb 22 '12 at 11:12
  • will you explain it deeply if with example than it will be good. i can't get your point. – Hrushikesh Betai Feb 22 '12 at 11:15
  • There's no example to be shown - just look at [`CLLocationManager`](https://developer.apple.com/library/ios/#documentation/CoreLocation/Reference/CLLocationManager_Class/CLLocationManager/CLLocationManager.html#//apple_ref/occ/cl/CLLocationManager)'s documentation or read [Location Awareness Guide](https://developer.apple.com/library/ios/#documentation/UserExperience/Conceptual/LocationAwarenessPG/Introduction/Introduction.html#//apple_ref/doc/uid/TP40009497). – macbirdie Feb 22 '12 at 11:50