2

I hope there's an expert out there who can help me with this:

I googled it for hours, but could not find any information if there is any way to keep a running NSTimer active when the app is running in the background ?

The problem scenario is I need to set a countdown timer on button click. Time left is shown (02:34:45) as title of button. how do my timer is keep on running when application enters into background or it is suspended . Please help me how i can maintain the time left for alarm, what are the values selected for alarm ( like 2 hours , 3 minutes )

Thanks for any help!

4

1 回答 1

4

如果您将其用于警报,最好的办法是使用本地通知。这样您就不需要使用任何后台处理模式。

当您的应用程序即将进入后台(在您的应用程序委托中applicationWillResignActiveapplicationDidEnterBackground在您的应用程序委托中)时,获取您的计时器剩余的时间。然后在未来安排一个本地通知:

NSTimeInterval timeUntilNotification = ...

UILocalNotification *localNotif = [[UILocalNotification alloc] init];
localNotif.fireDate = [[NSDate date] dateByAddingTimeInterval:secondsUntilNotification];
localNotif.soundName = UILocalNotificationDefaultSoundName;

[[UIApplication sharedApplication] scheduleLocalNotification:localNotif];

当您的应用再次激活时,请务必使用 取消所有通知cancelAllLocalNotifications

于 2012-05-14T09:58:59.780 回答