如何在iOS应用程序在后台保持一个TCP连接的无限活着时? [英] How can an iOS app keep a TCP connection alive indefinitely while in the background?

查看:359
本文介绍了如何在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 :如果用户pressed 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 href=\"https://web.archive.org/web/20140806111426/http://tapbots.com/blog/pastebot/pastebot-music-in-background\"相对=nofollow>沉默的背景音乐,随时跟踪。

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

注意在使用黑客喜欢使用的背景音乐或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天全站免登陆