어떤 이유에서든 내 타이머는 포 그라운드에서만 실행됩니다. 나는 여기에서 해결책을 찾았지만 어떤 좋은 것을 찾을 수 없었다. 내 타이머 코어 데이터를 사용하고 모든 timeInterval 감소 후 저장됩니다. 또한 UILocalNotification을 보내고 있지만 작동하지 않습니다.타이머를 백그라운드에서 실행 한 다음 로컬 알림을 보내려면 어떻게해야합니까?
-(IBAction)startTimer:(id)sender{
if (timer == nil) {
[startButton setTitle:@"Pause" forState:UIControlStateNormal];
timer = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(timerAction:) userInfo:nil repeats:YES];
} else {
[startButton setTitle:@"Resume" forState:UIControlStateNormal];
[timer invalidate];
timer = nil;
}
}
-(void)timerAction:(NSTimer *)t
{
if(testTask.timeInterval == 0)
{
if (self.timer)
{
[self timerExpired];
[self.timer invalidate];
self.timer = nil;
}
}
else
{
testTask.timeInterval--;
NSError *error;
if (![self.context save:&error]) {
NSLog(@"couldn't save: %@", [error localizedDescription]);
}
}
NSUInteger seconds = (NSUInteger)round(testTask.timeInterval);
NSString *string = [NSString stringWithFormat:@"%02u:%02u:%02u",
seconds/3600, (seconds/60) % 60, seconds % 60];
timerLabel.text = string;
NSLog(@"%f", testTask.timeInterval);
}
-(void)timerExpired{
UILocalNotification* localNotification = [[UILocalNotification alloc] init];
localNotification.alertBody = @"Time is up";
localNotification.alertAction = @"Ok";
localNotification.timeZone = [NSTimeZone defaultTimeZone];
[[UIApplication sharedApplication]presentLocalNotificationNow:localNotification];
}
내가 (단지 백그라운드에서 내 타이머 작업을 만들기 위해 몇 가지 지침을 주셔서 감사합니다 정말 것이다 :이 내가 지금 무엇을 가지고 그것 foreground..Well의 경우 경고보기를 보내지 않습니다 그것을 믿고있어 애플의 타이머처럼). 나는 또한 백그라운드에서 응용 프로그램 ..
가능한 [iPhone의 배경 스레드 문제] (http://stackoverflow.com/questions/7187967/problem-in-background-thread-in-iphone); 또한 [배경 및 nstimer에서 작업하는 동안 문제가] 참조 (http://stackoverflow.com/q/7190919) –
귀하의 응용 프로그램이 백그라운드로 동안 타이머를 실행할 수 없습니다. 카운트 다운을 표시하려는 경우 앱이 백그라운드로 들어간 시간을 저장하고 다시 활성화 될 때 다시 표시해야합니다. 이 문제에 대한 여러 게시물이 있습니다. –
그걸 시도 했니? performSelector : @selector (presentLocalNotificationNow :) withObject : localNotification afterDelay : 1.0];'여기서 1.0은 기다리고 싶은 시간 (초)입니다. – lucaslt89