iOS 应用程序如何在后台无限期地保持 TCP 连接处于活动状态? [英] How can an iOS app keep a TCP connection alive indefinitely while in the background?

查看:24
本文介绍了iOS 应用程序如何在后台无限期地保持 TCP 连接处于活动状态?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

一个 iPhone 应用程序,通过 TCP 连接到远程服务器.使用场景有:

An iPhone app, connecting to a remote server via TCP. The use scenarios are:

  1. 应用(用户)向服务器发送数据,服务器回复数据.
  2. 服务器可能会在不执行任何操作的情况下向应用发送数据.

假设应用程序在 30 分钟内没有向服务器发送数据,服务器将关闭连接.即使用户什么都不做,我也想让连接保持 120 分钟.

Assume that if app does not send data to server for 30 minutes, server will close the connection. I want to keep the connection alive for 120 minutes even if user does nothing.

案例 1:如果应用程序在前台,我可以使用计时器向服务器发送一些无操作数据.没问题.

Case 1: if app is in foreground, I can use timer to send some do-nothing data to server. No problem.

案例 2:如果用户按下 Home 并且应用程序进入后台,我该怎么办?我不想显示警报或打断用户的东西(他不在或玩游戏).我只是想让连接保持更长时间,当用户回到应用程序时,他发现连接仍然存在并且对此感到满意.

Case 2: if user pressed Home and app went to background, what could I do? I don't wan to show alert or something to interrupt user(he is away or playing games). I just wanna keep the connection alive for a longer period and when user comes back to the app, he found out that the connection will be still alive and be happy with that.

我已阅读有关 iphone API 的后台执行、多任务处理和本地通知的文档.我不确定是否可以实现案例 2.

I have read documentations about background execution, multitasking, and local notifications of iphone APIs. I'm not sure whether if I can achieve Case 2.

只使用合法的API,不越狱.

Only use legal APIs, no jailbreaking.

推荐答案

Tapbots 用 Pastebot 解决了这个问题,提示用户运行 无声背景音轨.

Tapbots solved this problem with Pastebot by prompting the user to run a silent background audio track at all times.

请注意,Apple 不赞成使用诸如使用背景音频或 VOIP API 来保持非音频或 VOIP 应用程序运行(如上文中描述的解决方法"所证明的那样)的黑客行为,因此涉足这些技术可能会被拒绝提交.

Note that Apple frowns on using hacks like employing the background audio or VOIP APIs to keep non audio or VOIP apps running (as evidenced by the 'workaround' described in the article above) so dabbling with these techniques risks rejection at the point of submission.

不幸的是,没有合法的 API 可以在后台保持连接处于活动状态.也许他们会在未来的 iOS 更新中引入一个,但您可以考虑提交 功能请求 来表达您的意见支持一下.

Unfortunately, though, there is no legal API to keep a connection alive in the background. Perhaps they'll introduce one in a future update to iOS, but you might consider submitting a feature request to voice your support for it.

这篇关于iOS 应用程序如何在后台无限期地保持 TCP 连接处于活动状态?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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