我想一直运行服务,即使应用在Swift IOS中被关闭,杀死或终止 [英] I want to Run a Service all the time Even App is closed,killed or terminated in Swift IOS
本文介绍了我想一直运行服务,即使应用在Swift IOS中被关闭,杀死或终止的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
这是我要一直运行的示例代码服务
Here is sample code service that i want to run all the time
internal func backService()
{
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0), { () -> Void in
let anObject = try! Realm().objects(remindertbl).filter("status = 0")
print("here")
for loop in anObject
{
let dateFormatter = NSDateFormatter()
dateFormatter.dateFormat = "dd/MM/yyyy hh:mm a"
let timestamp = dateFormatter.stringFromDate(NSDate())
let timeStampint = dateFormatter.dateFromString(timestamp)!.timeIntervalSince1970
if(loop.reminderdays == Int64(timeStampint))
{
if(loop.status == 0){
let notification = UILocalNotification()
notification.fireDate = NSDate(timeIntervalSince1970: 0)
notification.alertBody = "\(loop.title)"
notification.alertAction = "swipe to opem VLB Cloud!"
notification.soundName = UILocalNotificationDefaultSoundName
notification.userInfo = ["CustomField1": "w00t"]
UIApplication.sharedApplication().scheduleLocalNotification(notification)
let updateObject = try! Realm().objects(remindertbl).filter("status = 0")
var reminderObject = remindertbl()
for updateStatus in updateObject{
try! Realm().write()
{
print("target")
reminderObject.status = 2
reminderObject.reminderId = updateStatus.reminderId
reminderObject.syncflag = updateStatus.syncflag
reminderObject.vehicleid = updateStatus.vehicleid
reminderObject.syncid = updateStatus.syncid
reminderObject.dateadded = updateStatus.dateadded
reminderObject.datemodified = Int64(Timestamp)!
reminderObject.remindertime = updateStatus.remindertime
reminderObject.reminerDate = updateStatus.reminerDate
reminderObject.reminderdays = updateStatus.reminderdays
reminderObject.title = updateStatus.title
reminderObject.targetdistance = updateStatus.targetdistance
reminderObject.currentdistance = updateStatus.currentdistance
reminderObject.remindertype = updateStatus.remindertype
reminderObject.synctype = updateStatus.synctype
reminderObject.recuringdays = updateStatus.recuringdays
reminderObject.recuringdistance = updateStatus.recuringdistance
try! Realm().add(reminderObject, update: true)
reminderObject = remindertbl()
}
}
}
}
}
})
}
推荐答案
由于多种原因,您无法提出要求:
What you are asking is not possible for multiple reasons:
- 已被终止或终止的任务/应用无法定义 运行一些代码
- Apple在iOS上厌恶了一些背景模式 设备,如果您不是诸如voIP之类的特定任务,则您的应用程序不会 被操作系统授权随时运行
- A task/app that has been killed or terminated cannot by definition run some code
- A few background modes are atuhorized by Apple on iOS devices, if you are not specific task such as voIP, your app won't be authorized to run at all times by the OS
如果您想了解有关在iOS上后台执行某些任务的更多信息,建议您阅读官方文档: iOS的背景模式
If you want to learn more about executing some tasks on the background on iOS, I suggest you read the official documentation: Apple Documentation or this tutorial by the Ray Wenderlich team: Background modes for iOS
这篇关于我想一直运行服务,即使应用在Swift IOS中被关闭,杀死或终止的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文