2016-04-14 24 views
2

我需要实现以下要求:iOS的后台执行的医疗保健应用

iOS应用应当定期(例如: - 每1小时)收集来自健康应用程序中的数据和数据时,发送到服务器背景模式。

我试图实现此方案象下面这样:

我实现以下代码: -

FUNC applicationDidEnterBackground(应用:UIApplication的){

self.bgTask = application.beginBackgroundTaskWithName("updateMedicalData") { 
     self.endBackgroundUpdateTask() 

    } 
    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT,0)) { 
     print("Time remaining ::",application.backgroundTimeRemaining) 
     NSTimer.scheduledTimerWithTimeInterval(25, target:self, selector:#selector(self.performBackgroundTask), userInfo:nil, repeats:true) 
    } 


} 

我已经实现读取的逻辑来自健康应用程序和发送服务器的数据在“performBackgroundTask()”方法中“

但问题是,应用程序仅在后台运行3分钟,并呼叫过期处理程序。

我已经设置了关键的“所需的背景模式”到“从网络应用软件下载的内容”,并为重点的plist文件的“应用程序在后台不运行”到“NO”。

任何人都可以告诉我,如果有可能实现上述要求?

+0

您可能会更好地观察数据何时更新,然后将该数据发送到服务器而不是每小时检查一次。 HKObserverQuery将让你做到这一点 – bolnad

+0

你已经实现了这个?这可能吗?请帮助我。任何想法都会有所帮助。 –

回答

1

只有某些类型的应用程序可能有较长的后台执行时间。更具体:

For tasks that require more execution time to implement, you must request specific permissions to run them in the background without their being suspended. In iOS, only specific app types are allowed to run in the background: 

Apps that play audible content to the user while in the background, such as a music player app 
Apps that record audio content while in the background 
Apps that keep users informed of their location at all times, such as a navigation app 
Apps that support Voice over Internet Protocol (VoIP) 
Apps that need to download and process new content regularly 
Apps that receive regular updates from external accessories 

https://developer.apple.com/library/ios/documentation/iPhone/Conceptual/iPhoneOSProgrammingGuide/BackgroundExecution/BackgroundExecution.html

请看看这个链接查看此意愿是否适合你。我猜“后台抓取”可能适合您的需求。在这种情况下,您需要将您的UIBackgroundModes值设置为“fetch”。你已经这样做了吗?

祝你好运!

1

添加到Robski18已回答的内容。您无法定期将数据发送到服务器。要做到这一点的唯一方法是让服务器周期性地向您的应用程序发送一个静默推送通知,并在该回答中调用后台模式“需要定期下载和处理新内容的应用程序”。除非你会推送内容而不提取它。

即使服务器周期性地发送推送,并不意味着应用程序会定期收到它们,但如果服务器每小时发送一次,应用程序可能会在几个小时内收不到一个,然后它们会聚集在一起。

此外,如果用户杀死应用程序,那么应用程序将不会收到推送。