Ios7 在iOS 7中,NSTimer应在后台连续运行

Ios7 在iOS 7中,NSTimer应在后台连续运行,ios7,nstimer,core-location,Ios7,Nstimer,Core Location,我有一个NSTimer,它根据我的当前时间调用startUpdatingLocation或StopUpdateLocation方法。如果我的当前时间小于晚上8点,它将停止更新位置,但我的计时器将在后台持续运行。我不是在删除我的应用程序,也不是在后台删除我的应用程序 我需要在后台连续运行计时器,并应在当前时间为第二天上午8点时开始更新位置。现在我的应用程序正在后台运行,但我的位置不会在第二天早上8点开始更新。我在iOS 7上进行了测试。只有在我从后台启动应用程序后,它才会开始更新位置 有人能建议我

我有一个NSTimer,它根据我的当前时间调用startUpdatingLocation或StopUpdateLocation方法。如果我的当前时间小于晚上8点,它将停止更新位置,但我的计时器将在后台持续运行。我不是在删除我的应用程序,也不是在后台删除我的应用程序

我需要在后台连续运行计时器,并应在当前时间为第二天上午8点时开始更新位置。现在我的应用程序正在后台运行,但我的位置不会在第二天早上8点开始更新。我在iOS 7上进行了测试。只有在我从后台启动应用程序后,它才会开始更新位置

有人能建议我如何在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];
        }
    }

在后台不可能随心所欲。你必须遵守苹果公司的协议。不幸的是,苹果不允许你在后台运行NSTimer(iOS 6中曾经有一个漏洞,但他们修复了它),相反,你会收到一组指令,你可以执行这些指令来获取位置更新

由苹果阅读,它彻底解释了如何在后台运行应用程序


为了您的利益,我建议在您的服务器上实现这项服务,并在需要时拨打电话——在您的情况下是上午8点。它有它的缺点,但这是我现在能想到的唯一方法。

在后台不可能随心所欲。你必须遵守苹果公司的协议。不幸的是,苹果不允许你在后台运行NSTimer(iOS 6中曾经有一个漏洞,但他们修复了它),相反,你会收到一组指令,你可以执行这些指令来获取位置更新

   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;
}
由苹果阅读,它彻底解释了如何在后台运行应用程序


为了您的利益,我建议在您的服务器上实现这项服务,并在需要时拨打电话——在您的情况下是上午8点。它也有缺点,但这是我现在能想到的唯一办法。

什么办法行得通?APN?我看不出有什么理由不这样做,但正如你所说的那样-你会耗尽电池…但这只是任何基于位置的应用程序的命运:-)是的,我计划安排本地通知或远程通知。谢谢如果您对答案满意,请将其标记为已接受,这样就不会在其他问题列表中显示为未回答。谢谢什么有用?APN?我看不出有什么理由不这样做,但正如你所说的那样-你会耗尽电池…但这只是任何基于位置的应用程序的命运:-)是的,我计划安排本地通知或远程通知。谢谢如果您对答案满意,请将其标记为已接受,这样就不会在其他问题列表中显示为未回答。谢谢
   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;
}