进入背景状态ios swift时,防止websocket连接掉线

进入背景状态ios swift时,防止websocket连接掉线,第1张

概述经过大量研究后,这似乎是一片灰色地带…… 一旦用户进入后台状态,我试图通过websockets偶尔发送网络请求(使用Swift库的Starscream,但我相信问题一般是iOS和套接字).用户离开应用程序后不久,套接字连接会自动断开连接.我认为这与Apple在后台的第三方网络活动政策有关. 在我的AppDelegate中,我尝试在用户离开时重新连接等,但这不起作用.我也听说过涉及播放音频的变通方法 经过大量研究后,这似乎是一片灰色地带……

一旦用户进入后台状态,我试图通过websockets偶尔发送网络请求(使用Swift库的Starscream,但我相信问题一般是iOS和套接字).用户离开应用程序后不久,套接字连接会自动断开连接.我认为这与Apple在后台的第三方网络活动政策有关.

在我的AppDelegate中,我尝试在用户离开时重新连接等,但这不起作用.我也听说过涉及播放音频的变通方法,但显然这会阻止我的应用程序被发布到App Store(如果不是这样,那么为什么以及如何工作?).其他人说我需要Apple的某种赠款,我该如何申请呢?有人清除它并提供合法的解决方案会很棒.我认为这是应用程序应该具备的功能,所以我在等待找到解决方案.

解决方法 我认为没有合法的方法可以真正解决这个问题. Apple不希望应用程序在后台运行,因为后台活动是一个很大的电池耗尽,它可能会让iPhone用户觉得他们的电池不够充足(除了其他问题,如“无法解释的”网络使用等)因此,为了用户体验,他们只提供有关iOS应用中背景活动的非常有限的选项.但是,我们可以通过某种方式保持应用程序的活跃性:

从App Programming Guide for 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允许应用程序完成简短任务或下载之外,请求系统允许应用程序在后台运行的唯一方法是在Info.pList中指定后台执行模式.这可以在项目的XCode功能对话框中完成,也可以直接编辑属性列表文件.我们来看看我们有哪些后台执行模式:

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

保持套接字存活可能属于“需要定期下载和处理新内容的应用程序”用例,所以让我们检查:

Fetching Small Amounts of Content Opportunistically

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允许你使用的那种双向常量通信.事实上,看看other related answers,当应用程序进入后台模式时,似乎确实没有合法的方式来保持套接字打开.

这意味着,要做你想做的事,你不能使用websockets作为你唯一的沟通渠道.我建议您使用获取后台模式(如上所述)以获取比在应用程序在后台时使用websocket更大块的内容,或者如果您希望用户能够看到新内容可用,您可以实现push notifications.

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

最后,关于使用音频作为解决方法:音频背景状态键将允许您的应用程序在后台无限期保持活跃 – 但如果您的应用程序没有真正使用它来播放音频,它将被应用程序商店拒绝.

总结

以上是内存溢出为你收集整理的进入背景状态ios swift时,防止websocket连接掉线全部内容,希望文章能够帮你解决进入背景状态ios swift时,防止websocket连接掉线所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/1073296.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-26
下一篇 2022-05-26

发表评论

登录后才能评论

评论列表(0条)

保存