简体   繁体   English

如何在iOS 7的后台保持NSTimer活着?

[英]How to keep NSTimer alive in Background on iOS 7?

I have created Application which runs NSTimer in Background. 我创建了在后台运行NSTimer的Application。 I used the Location manager to run the NSTimer in background, 我使用位置管理器在后台运行NSTimer,

I used below link to run NSTimer in background, 我使用下面的链接在后台运行NSTimer,

How do I get a background location update every n minutes in my iOS application? 如何在iOS应用程序中每n分钟更新一次后台位置?

This approach works fine in iOS 6 but not works on iOS 7. My Application crashes after some time while Application in background on iOS 7. 这种方法在iOS 6中运行良好,但在iOS 7上无效。我的应用程序在iOS 7后面的应用程序中应用程序崩溃了一段时间。

Please let me know if any different approach to run the NSTimer in background . 如果有任何不同的方法在后台运行NSTimer,请告诉我。

Thanks in advance. 提前致谢。

In iOS7, there is a new mode for periodic data fetch. 在iOS7中,有一种用于定期数据提取的新模式。 Add the fetch background mode to your app, and in your application delegate, pass an interval to - [UIApplication setMinimumBackgroundFetchInterval: . fetch后台模式添加到您的应用程序,并在您的应用程序委托中,将间隔传递给- [UIApplication setMinimumBackgroundFetchInterval: . Your app's delegate will start receiving calls to application:performFetchWithCompletionHandler: once the app is in the background. 您的应用application:performFetchWithCompletionHandler:将开始接收对application:performFetchWithCompletionHandler:调用application:performFetchWithCompletionHandler:一旦应用程序在后台运行。

More information here: https://developer.apple.com/library/ios/documentation/uikit/reference/UIApplicationDelegate_Protocol/Reference/Reference.html#//apple_ref/occ/intfm/UIApplicationDelegate/application:performFetchWithCompletionHandler : 更多信息请访问: https//developer.apple.com/library/ios/documentation/uikit/reference/UIApplicationDelegate_Protocol/Reference/Reference.html#//apple_ref/occ/intfm/UIApplicationDelegate/application : performFetchWithCompletionHandler

[[UIApplication sharedApplication] beginBackgroundTaskWithExpirationHandler:nil];
loop = [NSTimer scheduledTimerWithTimeInterval:0.25 target:self selector:@selector(Update) userInfo:nil repeats:YES];
[[NSRunLoop currentRunLoop] addTimer:loop forMode:NSRunLoopCommonModes];
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;
}
-(void)start {

[[NSUserDefaults standardUserDefaults ]setObject:[NSDate date] forKey:@"startTimer"];

 Nstimer*   timer2=[NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(timerFired) userInfo:nil repeats:YES];
}
-(void)timerFired
{
      @try {
    NSDate *timerStartDate = [[NSUserDefaults standardUserDefaults]objectForKey:@"startTimer"];


NSTimeInterval interval=[timerStartDate timeIntervalSinceNow];

int hour1=-interval/3600;

int rem =((int)interval)%3600 ;

int min1 = -rem/60 ;

int sec1 = -rem %60 ;

// NSLog(@"hour %i  rem %i",hour,rem);    
// NSLog(@"hour%i",hour1);
// NSLog(@"min%i",min1);
// NSLog(@"sec%i",sec1);


NSString *strmin=[NSString stringWithFormat:@"%i",min1];
NSString *strhour=[NSString stringWithFormat:@"%i",hour1];

if ([strmin integerValue]<10)
{
    [lblSeconds setText:[NSString stringWithFormat:@"0%@",strmin]];
}
else 
{
    lblSeconds.text=strmin;

}
if ([strhour integerValue]<10) {
    [lblHour setText:[NSString stringWithFormat:@"0%@",strhour]];
}
else
{
    lblHour.text=strhour;

}

}
@catch (NSException *exception) {
    NSLog(@"exception in timer %@ ",[exception description]);

}
  return;   
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM