سؤال

I am having NSTimer which calls an method to startUpdatingLocation or StopUpdatingLocation based on my current time. If my current time is less than 8 PM, it will stop updating the location but my timer will run continuously in the background. I am not either deleting my app or removing my app in the background.

I need to run the timer continuously in the background and should start updating location when the current time is 8 AM on next day. Right now my app is running in the background but my location is not start updating at 8 AM on next day. I tested in iOS 7. It starts to update the location only after I launch the app from background.

Can anybody suggest me how to run the timer continuously in the background in iOS 7 ?

 - (void)applicationDidEnterBackground:(UIApplication *)application
      {
            UIDevice *device = [UIDevice currentDevice];
            BOOL backgroundSupported = NO;
            if ([device respondsToSelector:@selector(isMultitaskingSupported)]) {
                backgroundSupported = YES;
            }

            if (backgroundSupported) 
            {
                bgTask = [[UIApplication sharedApplication] beginBackgroundTaskWithExpirationHandler:^{ [[UIApplication sharedApplication] endBackgroundTask:bgTask];
                }];


               NSTimer *preventSleepTimer = [[NSTimer alloc] initWithFireDate:[NSDate          dateWithTimeIntervalSinceNow:0] interval:20.0f target:self selector:@selector(switchingOnLocation) userInfo:nil repeats:YES];
            self.mTimer = preventSleepTimer;
            [preventSleepTimer release];

            // Add the timer to the current run loop.
            [[NSRunLoop currentRunLoop] addTimer:self.mTimer forMode:NSRunLoopCommonModes];
        }
    }
هل كانت مفيدة؟

المحلول

It is not possible to do as you please in the background. You have to conform to protocols by Apple. Unfortunately Apple doesn't let you run NSTimers on the background (there used to be an exploit in iOS 6, but they fixed it), instead you are given a set of instructions you implement to get the location updates.

Read this article by Apple, it thoroughly explains how to run app in the background.

For your sake, I'd recommend implementing APN service on your server and push to the phone when needed - 8AM in your case. It has its cons, but it's the only way I can now think of.

نصائح أخرى

   NSTimer *currentCycleTimer;

UIBackgroundTaskIdentifier bgTask = UIBackgroundTaskInvalid;
UIApplication *app = [UIApplication sharedApplication];
bgTask = [app beginBackgroundTaskWithExpirationHandler:^{
    [app endBackgroundTask:bgTask];
}];
[currentCycleTimer invalidate];
currentCycleTimer=nil;
secondsLeft = 120;
currentCycleTimer = [NSTimer scheduledTimerWithTimeInterval: 1.0 target:self selector:@selector(Countdown) userInfo:nil repeats: YES];

-(void) Countdown
{
  [currentCycleTimer invalidate];
   currentCycleTimer=nil;
}
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top