防止进入背景状态iOS Swift时WebSocket连接断开 [英] Prevent websocket connection to drop when entering background state ios swift

查看:330
本文介绍了防止进入背景状态iOS Swift时WebSocket连接断开的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

经过大量研究,这似乎是一个灰色区域……

After doing lots of research it seems this is a grey area...

一旦用户进入后台状态(使用Swift库Starscream,但我认为问题通常出在iOS和套接字上),我试图通过websocket发送偶尔的网络请求.用户离开应用程序后不久,套接字连接将自动断开连接.我认为这与苹果公司在后台对第三方网络活动的政策有关.

I am trying to send occasional network requests via websockets once a user has entered the background state (using the Swift library Starscream, but I believe the problem is with iOS and sockets in general). Shortly after the user leaves the app, the socket connection automatically disconnects. I gather this has something to do with Apple's policy on 3rd party network activity in the background.

在我的AppDelegate中,我尝试在用户离开等时重新连接,但这不起作用.我也听说过涉及播放音频的解决方法,但是显然,这将阻止我的应用发布到App Store(如果不是这种情况,那么为什么以及如何运行?).其他人说我需要Apple的某种资助,我该如何申请呢?某人将其清除并提供合法的解决方案将非常好.我觉得这是应用程序应具备的能力,因此我正在等待找到解决方案.

In my AppDelegate, I have tried reconnecting when the user leaves, etc, but this does not work. I have also heard about workarounds involving playing audio, but apparently this will stop my app from being published to the App Store (if that's not the case, then why and how does this work?). Others say I need some sort of grant from Apple, how do I request this? It would be great for someone to clear this up, and provide a legitimate solution. I feel this is something that apps should be capable of, so I'm waiting to find a solution.

推荐答案

我相信没有合法的方法可以真正解决此问题.苹果公司不希望应用程序在后台执行操作,因为后台活动会消耗大量电池,并且可能使iPhone用户感到电池电量不够用(除了无法解释的"网络使用率等其他问题之外) ,因此为了用户体验,它们仅提供有关iOS应用程序中后台活动的非常有限的选项.但是,我们可以通过某些方式使该应用程序保持活动状态:

I believe there is no legitimate way to truly work around this. Apple doesn't want apps doing stuff in the background because background activity is a big battery drain and it could make iPhone users feel their battery doesn't last enough (in addition to other issues such as "unexplained" network usage and so on), so they only provide very limited options regarding background activity in iOS apps for the sake of user experience. However, we can keep the app alive in some manners:

适用于iOS的应用程序编程指南:

当您发现有必要让应用程序在后台运行时,iOS可以帮助您高效地进行操作,而不会浪费系统资源或用户的电池. iOS提供的技术分为三类:

When you find it necessary to keep your app running in the background, iOS helps you do so efficiently and without draining system resources or the user’s battery. The techniques offered by iOS fall into three categories:

  • 在前台启动短期任务的应用程序可以要求有时间在该应用程序移至后台时完成该任务.
  • 在前台启动下载的应用程序可以将对这些下载的管理移交给系统,从而允许在下载继续进行时暂停或终止该应用程序.
  • 需要在后台运行以支持特定类型的应用程序的应用程序 任务可以声明其对一个或多个后台执行的支持 模式.
  • Apps that start a short task in the foreground can ask for time to finish that task when the app moves to the background.
  • Apps that initiate downloads in the foreground can hand off management of those downloads to the system, thereby allowing the app to be suspended or terminated while the download continues.
  • Apps that need to run in the background to support specific types of tasks can declare their support for one or more background execution modes.

因此,除了要求iOS允许应用程序完成简短任务或下载外,似乎要求系统允许应用程序在后台运行的唯一方法是在我们的.这可以在项目的XCode的Capabilities对话框中完成,也可以直接编辑属性列表文件来完成.让我们检查一下我们可以使用哪些后台执行模式:

So it seems that, other than asking iOS to allow the app to finish short tasks or downloads, the only way to request the system to allow the app to run in the background is to specify a background execution mode in our Info.plist. This can be done in XCode's Capabilities dialog for your project, or by editing the property list file directly. Let's check which background execution modes we have available:

在iOS中,只允许特定的应用类型在后台运行:

In iOS, only specific app types are allowed to run in the background:

  • 在后台播放可听内容的应用程序,例如音乐播放器应用程序
  • 在后台录制音频内容的应用
  • 始终使用户知道其位置的应用程序,例如导航应用程序
  • 支持互联网协议语音(VoIP)的应用
  • 需要定期下载和处理新内容的应用
  • 从外部附件接收定期更新的应用
  • 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

使套接字保持活动状态可能属于需要定期下载和处理新内容的应用程序"用例,因此让我们检查一下:

Keeping a socket alive could fall into the "Apps that need to download and process new content regularly" use case, so let's check that:

机会性地获取少量内容

Fetching Small Amounts of Content Opportunistically

需要定期检查新内容的应用程序可以要求系统将其唤醒,以便它们可以启动该内容的提取操作.要支持此模式,请从Xcode项目中功能"选项卡的背景模式"部分启用背景获取"选项. (您也可以通过在应用程序的Info.plist文件中将fback值包含UIBackgroundModes键来启用此支持.)启用此模式并不能保证系统会给您的应用程序任何时间执行后台获取.系统必须在您的应用获取内容的需求与其他应用以及系统本身的需求之间取得平衡.在评估了这些信息之后,系统会在有机会的时候给应用程序一些时间.

Apps that need to check for new content periodically can ask the system to wake them up so that they can initiate a fetch operation for that content. To support this mode, enable the Background fetch option 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 fetch value in your app’s Info.plist file.) Enabling this mode is not a guarantee that the system will give your app any time to perform background fetches. The system must balance your app’s need to fetch content with the needs of other apps and the system itself. After assessing that information, the system gives time to apps when there are good opportunities to do so.

因此,似乎该选项仅可用于通过HTTP请求(或其他网络请求)获取少量内容,而不能用于websocket允许您使用的双向恒定通信方式.实际上,查看其他相关答案,当应用程序进入后台模式时,似乎确实没有合法的方法可以保持套接字打开.

So it seems that this option is only usable for obtaining small amounts of content through HTTP requests (or other network requests), not for the kind of two-way constant communication a websocket would allow you to use. In fact, looking at other related answers, it seems there is indeed no legitimate manner to keep a socket open when the app enters background mode.

这意味着要执行您想做的事情,您不能使用websockets作为您唯一的通信渠道.我建议您使用fetch后台模式(如上所述),以便以比应用程序在后台使用websocket更大的块获取内容,或者如果您希望用户能够要查看是否有新内容可用,您可以实施

This means that, to do what you want, you cannot use websockets as your only communication channel. I'd recommend you to either use the fetch background mode (as described above) in order to fetch content in larger chunks than you would using the websocket while the app is in the background, or if you want the user to be able to see that new content is available, you could implement push notifications.

您不能使用推式通知"直接发送大量内容,但是它们可以用于提示用户打开应用程序时有可用的新内容.无论使用后台获取还是推送通知,都应在App Delegate上实现方法,无论何时从后台状态恢复到应用状态,该方法都会将应用状态与后端状态同步.

You can't use Push Notifications to send large amounts of content directly, but they can be used to prompt the user that there is new content available when they open your app. Regardless of whether you use background fetch or Push Notifications, you should implement methods on your App Delegate which will synchronize the app's state with your backend's state whenever your app is brought back from the background state.

最后,关于使用音频作为解决方法:audio后台状态密钥将允许您的应用在后台无限期地处于活动状态-但是,如果您的应用并未真正使用它来播放音频,则它将被应用商店拒绝.

Finally, regarding using audio as a workaround: The audio background state key will allow your app to stay alive indefinitely in the background - but if your app does not truly use it in order to play audio, it will get rejected by the app store.

这篇关于防止进入背景状态iOS Swift时WebSocket连接断开的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆