[英]Executing code, when the application is in background not working on physical device (SWIFT)
我正在制作一个应用程序,它依赖于在后台执行一些次要代码,但遇到了一个特殊的问题。
该应用程序有一个计时器NSTimer()
。 所有这些背后的原理与计时器(在所有iOS设备上安装的Clock应用程序中)相似,也就是说,当计时器结束时会显示UILocalNotification
。
当我在Xcode的模拟设备上运行它时,一切都按预期运行,但是在iPhone上测试时,没有任何通知。 它遵循以下原则:
var end = timerHasEnded()
if end == true{
println("the timer has ended")
}
而且它没有用。 因此,我检查了应用程序是否通过执行此操作甚至检测到后台UIApplicationState
,并且它不在物理设备上。 有趣的是,它是在模拟设备上执行的。
我尝试在后台线程上运行计时器,使用QOS_CLASS_BACKGROUND
和dispatch_async
无济于事。 有谁能够帮助我?
当延迟是警报中剩余的时间时,您是否可以安排UILocalNotification在延迟后出现?
var date: NSDate = /*time until your timer expires*/
let app = UIApplication.sharedApplication()
let oldNotifications = app.scheduledLocalNotifications
if oldNotifications.count > 0 {
app.cancelAllLocalNotifications()
}
let alarm = UILocalNotification()
alarm.fireDate = date
alarm.timeZone = NSTimeZone.defaultTimeZone()
alarm.repeatInterval = 0
alarm.soundName = "myAlarmSound.caf"
alarm.alertBody = "Do something!"
app.scheduleLocalNotification(alarm)
我是从Apple提供的Obj-C示例中编写此代码的。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.