How to keep iphone ios xmpp connection alive while in the background?
Xmpp gets disconnected automatically if the app is in the background for more than 3-4 minutes. Is there anyway to keep the connection alive indefinitely or atleast for 30minutes or so?
In the XMPPStreamDelegate, add next method:
/**
- This method is called after the tcp socket has connected to the remote host.
- It may be used as a hook for various things, such as updating the UI or extracting the server's IP address.
- If developing an iOS app that runs in the background,
- please use XMPPStream's enableBackgroundingOnSocket property as opposed to doing it directly on the socket here.
**/
- (void)xmppStream:(XMPPStream *)sender socketDidConnect:(GCDAsyncSocket *)socket
Simply set the enableBackgroundingOnSocket property on the socket and it should work.
Thanks a lot.. It worked :)
Please share how you did it?
It is not working for me by using enableBackgroundingOnSocket. Please help me. I am stuck.
You need an push notification once the user is offline to enable app which is in background and immediately make an connect request to XMPP to get the app alive from background. In my case I used the same mechanism.
If app in background and when we take the app in foreground after 30 minutes then it autoreconnect event doesn't fire, Should XMPP library do this or should we do something else to make connect automatically?
Thanks.
Auto reconnect only happens when there is network issue. Incase you need application to wake up from background mode you need to enable push notification through which the application would become active and send a connect request within 30 secs of the whole procedure. Although this is not a full proof solution due to restriction provided by Apple however will work.
Thanks, Anything else that i can do to keep active connection in BG as well?
Use iOS default background modes enabled if you have no restrictions with respect to your app requirements :)