ios - 如何在后台共享应用程序之间的数据?

标签 ios background data-sharing

恐怕我已经知道自己问题的答案了,但我还是决定在失去希望之前问一问。

我有以下用例:一个应用程序在“共享”资源中写入一些字节(比方说在一个文件上),另一个应用程序读取数据并在后台通过 BLE 将它们发送到外部设备。不幸的是,我知道像共享资源背景 这样的概念在 iOS 中是有限制的。到目前为止我尝试的是:

使用应用组在两个应用之间共享数据

这即使在后台也能正常工作,但是这两个应用程序需要由同一个开发团队制作(即团队 ID 必须相同)。这是一个问题,因为有一款应用不是我的开发团队制作的。

复制粘贴板中的数据

当应用程序处于后台时,这不起作用。数据将始终为 nil,并且仅当应用再次激活时才会更新。

……当然还有……

背景限制

尽管我选择了 App Group 解决方案,但我能够设法在后台读取数据并发送到 BLE 设备的唯一方法是启动后台任务。这显然是一个警告:该任务有一个到期时间(根据我执行的测试,它会持续大约 3 分钟,然后被操作系统暂停)。我不需要运行一个长时间的任务,但我不能保证它可以在 3 分钟左右完成。

问题是:对于这个用例还有其他解决方案吗?还是我最终应该放弃?

最佳答案

如果您使用 CoreBluetooth 将文件发送到您的蓝牙设备,您可以尝试在您的应用的 Info.plist 中添加 bluetooth-central 作为 UIBackgroundMode 键。

Apple 在其 programming guide 中提到了这一点:

Communicating with a Bluetooth Accessory Apps that work with Bluetooth peripherals can ask to be woken up if the peripheral delivers an update when the app is suspended. This support is important for Bluetooth-LE accessories that deliver data at regular intervals, such as a Bluetooth heart rate belt. You enable support for using bluetooth accessories from the Background modes section of the Capabilities tab in your Xcode project. (You can also enable this support by including the UIBackgroundModes key with the bluetooth-central value in your app’s Info.plist file.) When you enable this mode, the Core Bluetooth framework keeps open any active sessions for the corresponding peripheral. In addition, new data arriving from the peripheral causes the system to wake up the app so that it can process the data. The system also wakes up the app to process accessory connection and disconnection notifications.

In iOS 6, an app can also operate in peripheral mode with Bluetooth accessories. To act as a Bluetooth accessory, you must enable support for that mode from the Background modes section of the Capabilities tab in your Xcode project. (You can also enable this support by including the UIBackgroundModes key with the bluetooth-peripheral value in your app’s Info.plist file.) Enabling this mode lets the Core Bluetooth framework wake the app up briefly in the background so that it can handle accessory-related requests. Apps woken up for these events should process them and return as quickly as possible so that the app can be suspended again.

关于ios - 如何在后台共享应用程序之间的数据?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45052645/

相关文章:

c# - 在应用程序/进程之间传递数据

ios - 弹出 Controller 没有被解雇

ios - Xamarin 在 ios 上形成 zxing ZXingScannerView

ios - 如果 CollectionView 项目集成了另一个 CollectionView,则无法选择该项目

java - 更新 JLabel 图标会弄乱背景

android - 在移动应用程序和网络服务器之间同步大量数据

servlets - 使用特殊的自动启动 servlet 在启动时进行初始化并共享应用程序数据

android - 开启硬件加速 : translate3d

python - 在 Python 上显示背景时出现问题。 Canvas

css - 如何在导航栏上设置背景图片?