iOS 应用程序如何在后台无限期保持 TCP 连接?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5840365/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How can an iOS app keep a TCP connection alive indefinitely while in the background?
提问by yehnan
An iPhone app, connecting to a remote server via TCP. The use scenarios are:
一个 iPhone 应用程序,通过 TCP 连接到远程服务器。使用场景是:
- app (user) sends data to server and server responds data back.
- server might send data to app while it does nothing.
- 应用程序(用户)向服务器发送数据,服务器响应数据。
- 服务器可能会在不执行任何操作的情况下向应用程序发送数据。
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.
假设如果应用程序在 30 分钟内没有向服务器发送数据,服务器将关闭连接。即使用户什么都不做,我也想保持连接 120 分钟。
Case 1: if app is in foreground, I can use timer to send some do-nothing data to server. No problem.
案例1:如果应用程序在前台,我可以使用计时器向服务器发送一些无用数据。没问题。
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.
案例 2:如果用户按下 Home 并且应用程序进入后台,我该怎么办?我不想显示警报或其他东西来打扰用户(他不在或在玩游戏)。我只是想让连接保持更长的时间,当用户回到应用程序时,他发现连接仍然有效并且对此感到满意。
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.
我已阅读有关 iphone API 的后台执行、多任务处理和本地通知的文档。我不确定是否可以实现 Case 2。
Only use legal APIs, no jailbreaking.
只使用合法的API,不越狱。
采纳答案by Nick
Tapbots solved this problem with Pastebot by prompting the user to run a silent background audio trackat all times.
Tapbots 使用 Pastebot 解决了这个问题,通过提示用户始终运行无声背景音轨。
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.
请注意,Apple 不赞成使用诸如使用背景音频或 VOIP API 之类的技巧来保持非音频或 VOIP 应用程序运行(如上文文章中描述的“解决方法”所证明),因此涉足这些技术有在提交时被拒绝的风险。
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 requestto voice your support for it.
不幸的是,没有合法的 API 可以在后台保持连接活动。也许他们会在未来的 iOS 更新中引入一个,但你可以考虑提交一个功能请求来表达你对它的支持。
回答by Nick Weaver
Please have a look at Implementing a VoIP Applicationin Apple's iOS Application Programming Guide. This is no tested solution, but I think it can be used to achieve what you are asking for.
请查看Apple 的 iOS 应用程序编程指南中的实现 VoIP应用程序。这不是经过测试的解决方案,但我认为它可以用来实现您的要求。
Implementing a VoIP Application
A Voice over Internet Protocol (VoIP) application allows the user to make phone calls using an Internet connection instead of the device's cellular service. Such an application needs to maintain a persistent network connection to its associated service so that it can receive incoming calls and other relevant data. Rather than keep VoIP applications awake all the time, the system allows them to be suspended and provides facilities for monitoring their sockets for them. When incoming traffic is detected, the system wakes up the VoIP application and returns control of its sockets to it.
实施 VoIP 应用程序
互联网协议语音 (VoIP) 应用程序允许用户使用互联网连接而不是设备的蜂窝服务拨打电话。此类应用程序需要与其关联的服务保持持久的网络连接,以便它可以接收来电和其他相关数据。该系统不是让 VoIP 应用程序一直保持唤醒状态,而是允许它们被暂停,并为它们提供监控套接字的设施。 当检测到传入流量时,系统会唤醒 VoIP 应用程序并将其套接字的控制权返回给它。
I am not sure if you're developing a VoIP app but you'll be able to access the sockets and react on incoming packets. The text after the quote refers to [setKeepAliveTimeout:handler:][2]
which allows you to set a block of code which allows you to keep alive a(your VoIP) connection. Minimum scheduling time is 10 minutes, though.
我不确定您是否正在开发 VoIP 应用程序,但您将能够访问套接字并对传入的数据包做出反应。引号后面的文本指的是[setKeepAliveTimeout:handler:][2]
它允许您设置一个代码块,使您可以保持(您的 VoIP)连接的活动状态。不过,最短调度时间为 10 分钟。
In Implementing a VoIP Application is a paragraph on Installing a Keep-Alive Handler. I am curious if this will help. Please give us a note if this solved your problem.
在实现 VoIP 应用程序中有一段关于安装 Keep-Alive 处理程序。我很好奇这是否会有所帮助。请给我们留言,如果这个解决您的问题。
回答by Gowtham R
1.Create your socket with VOIP property like this
1.像这样创建具有 VOIP 属性的套接字
[inputStream setProperty:NSStreamNetworkServiceTypeVoIP forKey:NSStreamNetworkServiceType] ;
[outputStream setProperty:NSStreamNetworkServiceTypeVoIP forKey:NSStreamNetworkServiceType] ;
2.Add required background mode as "App provides Voice over IP services" in app.plist
2.在app.plist中添加需要的后台模式为“App提供IP语音服务”
If your application in background mode the voip socket will not close. Using this socket with uilocal notification you can get.
如果您的应用程序处于后台模式,则不会关闭 voip 套接字。将此套接字与 uilocal 通知一起使用,您可以获得。
"If your application is not an original voip application app store will reject"
“如果你的应用不是原始的voip应用,应用商店会拒绝”
回答by adam
For when the app is in the foreground:
当应用程序在前台时:
Look at STOMP http://stomp.codehaus.org/Protocol
看看 STOMP http://stomp.codehaus.org/Protocol
The STOMP Framework in Obj-C http://code.google.com/p/stompframework/
Obj-C 中的 STOMP 框架http://code.google.com/p/stompframework/
and the ActiveMQ broker http://activemq.apache.org/
和 ActiveMQ 代理http://activemq.apache.org/
For when the app is in the background I would probably go for using APNS (Notifications) to prompt the user to wake the app back up...
当应用程序在后台时,我可能会使用 APNS(通知)来提示用户唤醒应用程序备份......