如何在后台保持 iphone ios xmpp 连接处于活动状态? [英] How to keep iphone ios xmpp connection alive while in the background?

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

问题描述

适用于 iPhone 的 XMPPFramework 功能强大.我已经启动并运行了它,但是你如何在后台保持连接超过 10 分钟?我将不胜感激有关如何完成此操作的更多文档/操作方法.

The XMPPFramework for iPhone is powerful. I have it up and running, but how do you keep the connection alive while in the background for more than 10 minutes? I would appreciate some more documentation/how-tos on how to accomplish this.

所以用例简单而常见:Joe 在线并在 iPhone 聊天应用程序中.他离开聊天应用程序,去 Safari,玩游戏,看电影,做其他事情 3 小时(或更长时间).乔想在那段时间继续接收消息.

So the use case is simple and common: Joe is online and in the iPhone chat app. He leave the chat app and goes to Safari, plays a game, streams a movie, and does other stuff for 3 hours (or more). Joe wants to keep receiving messages during that time.

示例应用程序允许 Joe 在后台接收本地通知,但似乎只有 10 分钟.这是相关的代码(我认为).谢谢!

The example app allows Joe to receive local notifications in the background, but for only 10 minutes it seems. Here's the relevant code (I think). Thanks!

为了帮助其他人,甚至达到这一点,您需要将 xmppstream 属性 enableBackgroundingSocket 设置为 YES(它在您应该复制的 iphoneXMPP 示例项目中为您完成)和 appname-info.plist(即, iosChat-info-plist) 文件,您需要添加一个新的键/值对.您应该右键单击某处并添加行".您应该选择键所需的背景模式",然后输入voip".Xcode 会在您按下回车后检测到您的意思是App 提供 Voice over IP 服务".这让你有 10 分钟的时间让聊天应用程序在后台保持打开状态(我认为).但是我们想要无限期,我怀疑答案就在下面的方法中.我只是应该在这个方法中重新连接"还是什么,例如,[self connect](我有一个连接方法)?

To help out other folks, to even get to this point, you need to the xmppstream property enableBackgroundingSocket to YES (it's done for you in the iphoneXMPP example project which you should copy) and in the appname-info.plist (i.e., iosChat-info-plist) file you need to add a new key/value pair. You should right click somewhere and "add row". You should choose for the key "required background modes" and then type in "voip". Xcode will detect that you mean "App provides Voice over IP services" after you press enter. This gets you 10 minutes of keeping the chat app open in the background (I think). But we want indefinitely, and I suspect that the answer lies in the method below. Am I just supposed to "reconnect" within this method or something,e.g., [self connect] (I have a connect method)?

- (void)applicationDidEnterBackground:(UIApplication *)application{

DDLogVerbose(@"%@: %@", THIS_FILE, THIS_METHOD);

if ([application respondsToSelector:@selector(setKeepAliveTimeout:handler:)]) 
{
    [application setKeepAliveTimeout:600 handler:^{

        DDLogVerbose(@"KeepAliveHandler");

        // Do other keep alive stuff here.
    }];
}}

推荐答案

iOS 允许您保持连接的大致时间为 10 分钟.你可以另请查看 [app beginBackgroundTaskWithExpirationHandler],这将允许您请求更多时间来完成一项任务.为了保持 100% 的连接,您将需要向 info.plist(即 UIBackgroundModes)添加 voip、音频或位置标签.

10 minutes is the approximate time iOS allows you to stay connected. You can also look at [app beginBackgroundTaskWithExpirationHandler], which will allow you to request more time to finish a task. In order to remain connected 100%, you will need to either add a voip, audio or location tag to info.plist (that is UIBackgroundModes).

除非您添加这些标签之一,否则背景将不起作用.另外,添加标签将允许您保持连接,但如果您提交到 App Store,实际标签必须有效.如果没有真正的用例,Apple 将拒绝该应用程序.

Backgrounding will not work unless you add one of those tags. In addition, adding a tag will allow you to remain connected, but the actual tag must be valid if you're submitting to the App Store. Apple will reject the app if there's not a real use-case.

要在没有标签的情况下保持更长时间的连接,您将需要使用某种类型的服务器,该服务器会维持连接,然后使用推送通知来传递消息.

To remain connected longer without a tag, you will need to resort to using some type of server, which maintains the connection and then uses push notifications to deliver messages.

这篇关于如何在后台保持 iphone ios xmpp 连接处于活动状态?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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