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

标签 ios iphone swift

我需要实现以下要求:

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 文件中将“所需后台模式”键设置为“应用程序从网络下载内容”,并将“应用程序不在后台运行”键设置为“否”。

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

最佳答案

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

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”。你已经这样做了吗?

祝你好运!

关于医疗保健应用程序的 iOS 后台执行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36622612/

相关文章:

ios - UISegmentedControl 内部 View 应在显示 View 后调用 API

ios - 这是 Bug 还是正常行为?声明过程 StubProc; cdecl;外部 xxx 名称 'TheHeadOfToto' 在编译时不会崩溃

iphone - "Pushing the same view controller instance more than once is not supported"异常

swift - 从 Swift Vapor Server 内调用 ShazamKit

SwiftUI:如何在字符串子范围上设置不同字体和颜色的 TextView 样式

iOS 11 导航栏滚动

ios - SecKeyCopyKeyExchangeResult 在 iOS 13 中不能用作 nodejs ecdh

iphone - 这两个 NSString 方法之间的区别

ios - 长按 Iphone 会弹出一些矩形放大镜

ios - 通知 Swift3 的 removeObserver