如果您将它用于闹钟,最好的方法是使用本地通知。这样你就不需要使用任何后台处理模式。
当您的应用程序即将进入后台(在您的应用程序委托中的applicationWillResignActive
或applicationDidEnterBackground
)时,获取您的计时器剩余多少时间。然后计划为本地通知在不远的将来:
NSTimeInterval timeUntilNotification = ...
UILocalNotification *localNotif = [[UILocalNotification alloc] init];
localNotif.fireDate = [[NSDate date] dateByAddingTimeInterval:secondsUntilNotification];
localNotif.soundName = UILocalNotificationDefaultSoundName;
[[UIApplication sharedApplication] scheduleLocalNotification:localNotif];
当你的应用再次变为活动,确保取消所有通知与cancelAllLocalNotifications
。
我认为这里有一些关于后台执行的内容......但是,我认为如果将日期时间保存在applicationWillResignActive:app中会更好。委托的方法,然后重新计算applicationDidBecomeActive:方法中的剩余时间。 祝你好运! ;) –
谢谢男人..我认为它会工作:)。你是否认为它是唯一的解决方案,或者我们可以挖掘其他替代品..像背景胎面..等等 – Gaurav