【发布时间】:2015-06-03 21:04:13
【问题描述】:
背景
我创建了一个示例计时器应用程序,该应用程序从特定值倒数到零。一个标签显示还剩多少时间。
每次NSTimer(1 秒间隔)调用countDownSecs() 时,它都会打印还剩多少秒并执行println("countDownSecs()")。当倒计时达到0 秒时,它会发送本地推送通知。
当在工作的模拟器中运行时,它甚至会停留在后台并持续打印剩余的秒数和countDownSecs() 5 分钟。
另一方面,在实际设备(iPhone 4S、iOS 8.1.3 和 iOS 8.3)上运行时,它会在进入后台时立即停止。 (它之前在 iOS 8.3 上运行过。我认为这可能是因为 iPhone 4S 无法处理,但 iPhone 4S 模拟器运行良好。)
代码
在AppDelegate 的applicationDidEnterBackground() 中,UILocalNotification 的代码如下:
var alarmTime: NSDate = NSDate()
var app: UIApplication = UIApplication.sharedApplication()
var notifyAlarm: UILocalNotification = UILocalNotification()
//counterAll contains the total amount of seconds
alarmTime.dateByAddingTimeInterval(NSDate.timeIntervalSinceReferenceDate())
notifyAlarm.fireDate = NSDate(timeIntervalSinceNow: counterAll)
notifyAlarm.timeZone = NSTimeZone.defaultTimeZone()
notifyAlarm.repeatInterval = NSCalendarUnit(0)
notifyAlarm.soundName = "Time's Up.m4a"
notifyAlarm.alertBody = "Time's Up! - Your timer has ended."
app.scheduleLocalNotification(notifyAlarm)
问题
我可以更改任何内容来解决此问题吗?
提前致谢:)
【问题讨论】:
-
alarmTime 不应该是 fireDate 吗?
-
alarmTime始终为 0,所以我使用了NSTimeIntervalcounterAll。通知运行良好。 -
你注册UserNotificationSettings了吗?
-
是的,正如我所说,通知工作正常。
-
我认为你应该使用 NSTimer(fireDate:) 而不是使用倒计时
标签: ios swift timer background-process uilocalnotification