0

I'd like to use a NSTimer as a background task. I've written this code:

UIBackgroundTaskIdentifier bgTask;
        UIApplication  *app = [UIApplication sharedApplication];
        bgTask = [app beginBackgroundTaskWithExpirationHandler:^{
            [app endBackgroundTask:bgTask];
        }];
        self.silenceTimer = [NSTimer scheduledTimerWithTimeInterval:30 target:self selector:@selector(findlocation:) userInfo:nil repeats:NO];

But it doesn't fire the findlocation method after 30 seconds. Please guide me how to do this.

Jano
  • 62,815
  • 21
  • 164
  • 192
Kevin Char
  • 49
  • 1
  • 8
  • possible duplicate of [How do I make my App run an NSTimer in the background?](http://stackoverflow.com/questions/9220494/how-do-i-make-my-app-run-an-nstimer-in-the-background) – Jano Jan 23 '13 at 12:09
  • Try this http://stackoverflow.com/questions/4656214/iphone-backgrounding-to-poll-for-events – yen Jan 24 '13 at 12:39

4 Answers4

0

NSTimer are paused when the app is in background state.

You'll have to start some background task to do what you want. But even with that, you will be limited to a certain amount of time after the app was put in background.

Real backgrounding behavior is only granted for location tracking, VoIP or Audio apps. Other apps must face limitations: once in background, you are given an amount of time to complete tasks you start with beginBackgroundTaskWithExpirationHandler: (backgroundTimeRemaining).

Check this for more information http://developer.apple.com/library/ios/#documentation/iphone/conceptual/iphoneosprogrammingguide/BackgroundExecution/BackgroundExecution.html

For a clean solution, check the accepted answer of this question How do I make my App run an NSTimer in the background?

Community
  • 1
  • 1
DD_
  • 7,230
  • 11
  • 38
  • 59
0

This code to play the sound when application enter into background you can customize it for your own use

- (void)applicationDidEnterBackground:(UIApplication *)application{
    backgroundTask = [application beginBackgroundTaskWithExpirationHandler: ^{
        dispatch_async(dispatch_get_main_queue(), ^{
            if (backgroundTask != UIBackgroundTaskInvalid)
            {
                [application endBackgroundTask:backgroundTask];
                backgroundTask = UIBackgroundTaskInvalid;
            }
        });
    }];
    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{

        [NSThread sleepForTimeInterval:3];
        [self startPlayingInBackground:@"default.aif"];
        NSLog(@"Time remaining: %f",[application backgroundTimeRemaining]);

        dispatch_async(dispatch_get_main_queue(), ^{
            if (backgroundTask != UIBackgroundTaskInvalid)
            {
                // if you don't call endBackgroundTask, the OS will exit your app.
                [application endBackgroundTask:backgroundTask];
                backgroundTask = UIBackgroundTaskInvalid;
            }
        });
    });
}

The line below is the code with help of which m playing the sound it's my objective c function all

[self startPlayingInBackground:@"default.aif"];

reference from

Community
  • 1
  • 1
iPatel
  • 46,010
  • 16
  • 115
  • 137
0
UIBackgroundTaskIdentifier bgTask;
    UIApplication  *app = [UIApplication sharedApplication];
    bgTask = [app beginBackgroundTaskWithExpirationHandler:^{
        [app endBackgroundTask:bgTask];
    }];
    self.silenceTimer = [NSTimer scheduledTimerWithTimeInterval:30 target:self selector:@selector(findlocation:) userInfo:nil repeats:NO];

[[NSRunloop mainRunLoop] addTimer:self.silenceTimer forMode:NSDEfaultRunLoopMode];

Abdul Yasin
  • 3,480
  • 1
  • 28
  • 42
-1

Try this code:

dispatch_queue_t backgroundQueue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
 dispatch_source_t timerSource = dispatch_source_create(DISPATCH_SOURCE_TYPE_TIMER, 0, 0, backgroundQueue);
 dispatch_source_set_timer(timerSource, dispatch_time(DISPATCH_TIME_NOW, 0), 10.0*NSEC_PER_SEC, 0*NSEC_PER_SEC);
 dispatch_source_set_event_handler(timerSource, ^{
     if(queue)
         [self findlocation];
 });
 dispatch_resume(timerSource);
Rox
  • 909
  • 11
  • 31