Reputation:
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.
Upvotes: 5
Views: 11659
Reputation: 3081
[[UIApplication sharedApplication] beginBackgroundTaskWithExpirationHandler:nil];
loop = [NSTimer scheduledTimerWithTimeInterval:0.25 target:self selector:@selector(Update) userInfo:nil repeats:YES];
[[NSRunLoop currentRunLoop] addTimer:loop forMode:NSRunLoopCommonModes];
Upvotes: 2
Reputation: 3049
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;
}
Upvotes: -1
Reputation: 425
-(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;
}
Upvotes: -2
Reputation: 57040
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:
Upvotes: 5