简体   繁体   English

iOS 应用程序如何在后台保持 TCP 连接无限期活动?

[英]How can an iOS app keep a TCP connection alive indefinitely while in the background?

An iPhone app, connecting to a remote server via TCP.一个 iPhone 应用程序,通过 TCP 连接到远程服务器。 The use scenarios are:使用场景有:

  1. app (user) sends data to server and server responds data back.应用程序(用户)向服务器发送数据,服务器回复数据。
  2. 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.假设应用程序在 30 分钟内没有向服务器发送数据,服务器将关闭连接。 I want to keep the connection alive for 120 minutes even if user does nothing.即使用户什么都不做,我也想让连接保持 120 分钟。

Case 1: if app is in foreground, I can use timer to send some do-nothing data to server.案例1:如果应用程序在前台,我可以使用计时器向服务器发送一些无操作数据。 No problem.没问题。

Case 2 : if user pressed Home and app went to background, what could I do?案例2 :如果用户按下主页并且应用程序进入后台,我该怎么办? 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.我只想让连接保持更长时间,当用户回到应用程序时,他发现连接仍然存在并且对此感到满意。

I have read documentations about background execution, multitasking, and local notifications of iphone APIs.我已阅读有关 iphone API 的后台执行、多任务处理和本地通知的文档。 I'm not sure whether if I can achieve Case 2 .我不确定是否可以实现 Case 2

Only use legal APIs, no jailbreaking.只使用合法的 API,不越狱。

Tapbots solved this problem with Pastebot by prompting the user to run a silent background audio track at 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.但不幸的是,没有合法的 API 可以在后台保持连接活动。 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 的未来更新中引入一个,但您可以考虑提交功能请求以表达您对它的支持。

Please have a look at Implementing a VoIP Application in Apple's iOS Application Programming Guide.请查看 Apple 的 iOS 应用程序编程指南中的实现 VoIP 应用程序。 This is no tested solution, but I think it can be used to achieve what you are asking for.这不是经过测试的解决方案,但我认为它可以用来实现您的要求。

Implementing a VoIP Application实施 VoIP 应用程序

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. Internet 协议语音 (VoIP) 应用程序允许用户使用 Internet 连接而不是设备的蜂窝服务拨打电话。 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.该系统不是让 VoIP 应用程序一直处于唤醒状态,而是允许它们暂停,并为它们提供监控 sockets 的设施。 When incoming traffic is detected, the system wakes up the VoIP application and returns control of its sockets to it.当检测到传入流量时,系统会唤醒 VoIP 应用程序并将其 sockets 的控制权返回给它。

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.我不确定您是否正在开发 VoIP 应用程序,但您将能够访问 sockets 并对传入的数据包做出反应。 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.引用后的文本是指[setKeepAliveTimeout:handler:][2] ,它允许您设置一个代码块,使您能够保持(您的 VoIP)连接的活动。 Minimum scheduling time is 10 minutes, though.不过,最短调度时间是 10 分钟。

In Implementing a VoIP Application is a paragraph on Installing a Keep-Alive Handler .在实现 VoIP 应用程序中有一段关于安装 Keep-Alive 处理程序。 I am curious if this will help.我很好奇这是否会有所帮助。 Please give us a note if this solved your problem.如果这解决了您的问题,请给我们留言。

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提供Voice over IP服务”

If your application in background mode the voip socket will not close.如果您的应用程序处于后台模式,则 voip 套接字将不会关闭。 Using this socket with uilocal notification you can get.将此套接字与 uilocal 通知一起使用,您可以获得。

"If your application is not an original voip application app store will reject" “如果您的应用程序不是原始 voip 应用程序应用商店将拒绝”

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...当应用程序在后台时,我可能会 go 使用 APNS(通知)提示用户唤醒应用程序备份......

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在后台保持iphone ios websocket连接的活动状态? - How to keep iphone ios websocket connection alive while in the background? 如何在后台保持iphone ios xmpp连接活着? - How to keep iphone ios xmpp connection alive while in the background? 如何在iOS中为套接字连接设置“ KEEP ALIVE”? - How to set “KEEP ALIVE” for socket connection in iOS? 如何在iOS 7的后台保持NSTimer活着? - How to keep NSTimer alive in Background on iOS 7? 使企业应用程序在后台运行 - keep corporate app alive in background 使iOS App保持活动状态以使用MQTT协议 - Keep iOS App alive to work with MQTT protocol 当应用程序在iOS中处于后台时如何调用Method? - How can I call Method while app is in background in iOS? 如何在后台处理TCP连接? - How to handle a TCP connection in background? 如何使用ASIHTTP保持连接活动?我需要发出多个请求,但无法打开,然后关闭请求连接 - How to keep a connection alive using ASIHTTP? I need to make multiple requests, but can't afford to open, then close a request connection 当应用程序处于后台时,如何从ios 5应用程序向本地网络中的服务器发送一些请求? - How can i send some request from ios 5 app to server who is in local network, while app is in background?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM