How to keep iphone ios xmpp connection alive while in the background?

偶尔善良 提交于 2020-02-01 05:42:06

问题


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.

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.

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!

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.
    }];
}}

回答1:


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).

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.



来源:https://stackoverflow.com/questions/6223768/xmpp-gets-disconnected-after-iphone-goes-idle-for-more-than-10-mins

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!