简体   繁体   中英

How to keep NSTimer alive in Background on iOS 7?

I have created Application which runs NSTimer in Background. I used the Location manager to run the NSTimer in background,

I used below link to run NSTimer in background,

How do I get a background location update every n minutes in my iOS application?

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.

Please let me know if any different approach to run the NSTimer in background .

Thanks in advance.

In iOS7, there is a new mode for periodic data fetch. Add the fetch background mode to your app, and in your application delegate, pass an interval to - [UIApplication setMinimumBackgroundFetchInterval: . Your app's delegate will start receiving calls to application:performFetchWithCompletionHandler: once the app is in the background.

More information here: 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;   
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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