医疗保健应用的iOS后台执行

时间:2016-04-14 12:06:59

标签: ios iphone swift

我需要实现以下要求:

iOS应用应定期(例如: - 每1小时)从健康应用收集数据,并在后台模式下将数据发送到服务器。

我尝试实现这种情况,如下所示:

我实现了以下代码: -

func applicationDidEnterBackground(application: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()”中发送服务器的逻辑

但问题是,App仅在后台运行3分钟并且调用了到期处理程序。

我已将“必需的后台模式”键设置为“应用程序从网络下载内容”,并将“应用程序不在后台运行”键设置为plist文件中的“NO”。

有谁能告诉我是否可以实施上述要求?

3 个答案:

答案 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 :(得分:1)

添加Robski18的答案。您无法定期将数据发送到服务器。执行此操作的唯一方法是服务器定期向您的应用发送静默推送通知,并具有在该答案“需要定期下载和处理新内容的应用程序”中调用的后台模式。除了你将推送内容而不是取出它。

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

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

答案 2 :(得分:0)

也许 BGTaskScheduler 可以胜任这项工作。虽然如果您的设备是基于蓝牙的,当您的 bt 设备可以唤醒应用程序时,它会有特殊的后台模式。